-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathField_Options.F90
More file actions
1365 lines (1113 loc) · 54.1 KB
/
Field_Options.F90
File metadata and controls
1365 lines (1113 loc) · 54.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! Copyright (C) 2006 Imperial College London and others.
!
! Please see the AUTHORS file in the main source directory for a full list
! of copyright holders.
!
! Prof. C Pain
! Applied Modelling and Computation Group
! Department of Earth Science and Engineeringp
! Imperial College London
!
! amcgsoftware@imperial.ac.uk
!
! This library is free software; you can redistribute it and/or
! modify it under the terms of the GNU Lesser General Public
! License as published by the Free Software Foundation,
! version 2.1 of the License.
!
! This library is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
! Lesser General Public License for more details.
!
! You should have received a copy of the GNU Lesser General Public
! License along with this library; if not, write to the Free Software
! Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
! USA
#include "fdebug.h"
module field_options
!!< This module contains code that uses scalar/vector/tensor_fields
!!< in combination with floptions. Anything schema specific.
use fldebug
use global_parameters
use quadrature
use futils
use elements
use spud
use data_structures
use fields_data_types
use fields_base
use fields_allocates
use fields_manipulation
use metric_tools
use transform_elements
use fields_calculations
use state_module
implicit none
interface adaptivity_options
module procedure adaptivity_options_scalar, adaptivity_options_vector
end interface
interface interpolate_field
module procedure interpolate_field_scalar, interpolate_field_vector, &
& interpolate_field_tensor, interpolate_options
end interface
interface needs_initial_mesh
module procedure needs_initial_mesh_scalar, needs_initial_mesh_vector, &
& needs_initial_mesh_tensor, interpolate_options
end interface
interface constant_field
module procedure constant_field_scalar, constant_field_vector, &
& constant_field_tensor
end interface
interface isotropic_field
module procedure isotropic_field_tensor
end interface
interface diagonal_field
module procedure diagonal_field_tensor
end interface
interface extract_pressure_mesh
module procedure extract_pressure_mesh_from_state, extract_pressure_mesh_from_any_state
end interface extract_pressure_mesh
interface extract_velocity_mesh
module procedure extract_velocity_mesh_from_state, extract_velocity_mesh_from_any_state
end interface extract_velocity_mesh
private
public :: complete_mesh_path, complete_field_path, &
& get_external_mesh, adaptivity_options, print_children, &
& get_coordinate_field, select_fields_to_interpolate, &
& find_mesh_to_adapt, &
& adaptivity_bounds, find_linear_parent_mesh, &
& interpolate_field, convergence_norm_integer, &
& do_not_recalculate, needs_initial_mesh, &
& get_external_coordinate_field, collect_fields_by_mesh, &
& equation_type_index, field_options_check_options, &
& constant_field, isotropic_field, diagonal_field, &
& extract_pressure_mesh, extract_velocity_mesh, &
& postprocess_periodic_mesh, get_diagnostic_coordinate_field, &
& get_nodal_coordinate_field, extract_prognostic_pressure, &
& extract_prognostic_velocity, get_surface_ids
integer, parameter, public :: FIELD_EQUATION_UNKNOWN = 0, &
FIELD_EQUATION_ADVECTIONDIFFUSION = 1, &
FIELD_EQUATION_CONSERVATIONOFMASS = 2, &
FIELD_EQUATION_REDUCEDCONSERVATIONOFMASS = 3, &
FIELD_EQUATION_INTERNALENERGY = 4, &
FIELD_EQUATION_HEATTRANSFER = 5, &
FIELD_EQUATION_ELECTRICALPOTENTIAL = 6, &
FIELD_EQUATION_KEPSILON = 7
contains
recursive subroutine print_children(path)
implicit none
character(len=*), intent(in)::path
character(len=OPTION_PATH_LEN)::name, child_name
integer :: i, nchildren
name = " "
child_name = " "
call get_number_of_children(path, nchildren)
print *, trim(path), ": number of children: ", nchildren
if(nchildren==0) return
do i=0, nchildren-1
call get_child_name(path, i, name)
print*, trim(path), " contains ", trim(name)
child_name=path//"/"//trim(name)
call print_children(trim(child_name))
end do
end subroutine print_children
function complete_mesh_path(path, stat)
!!< Auxillary function to add from_file/from_mesh to meshion path.
character(len = *), intent(in) :: path
integer, optional, intent(out) :: stat
character(len = OPTION_PATH_LEN) :: complete_mesh_path
if(present(stat)) then
stat = 0
end if
if(have_option(trim(path) // "/from_mesh")) then
complete_mesh_path = trim(path) // "/from_mesh"
else if(have_option(trim(path) // "/from_file")) then
complete_mesh_path = trim(path) // "/from_file"
else if(present(stat)) then
stat = 1
else
ewrite(-1, *) "For mesh option path: " // trim(path)
FLAbort("Unknown mesh type or wrong mesh option path")
end if
end function complete_mesh_path
character(len=OPTION_PATH_LEN) function complete_field_path(path, name, stat)
!!< Auxillary function to add prognostic/diagnostic/prescribed
!!< to field option path. This version flaborts as opposed to the one
!!< in populate_state_module.
character(len=*), intent(in) :: path
character(len=*), intent(in), optional :: name
integer, intent(out), optional :: stat
if (present(stat)) then
stat = 0
end if
if (have_option(trim(path) // "/prognostic")) then
complete_field_path=trim(path) // "/prognostic"
else if (have_option(trim(path) // "/diagnostic")) then
complete_field_path=trim(path) // "/diagnostic"
else if (have_option(trim(path) // "/prescribed")) then
complete_field_path=trim(path) // "/prescribed"
else if (have_option(trim(path) // "/aliased")) then
complete_field_path=trim(path) // "/aliased"
else
if (present(stat)) then
stat = 1
complete_field_path=trim(path)
else
ewrite(0,*) "Error completing field path given the option path:", trim(path)
if (present(name)) then
ewrite(0,*) "for field name:", trim(name)
else
ewrite(0,*) "Note field name not available. Modify the call to include this for a more informative error message."
end if
FLAbort("Error: unknown field type or wrong field option path.")
end if
end if
end function complete_field_path
function make_coordinate_field(state, target_mesh) result (to_position)
!!< Creates a coordinate field interpolated on the target_mesh
!!< If we're on the sphere and the super parametric option is selected
!!< the coordinate field is "bended" onto the sphere. This routine
!!< always creates a field with a new reference (even if we simply return
!!< a coordinate field that is already in state), so it has to be deallocated
!!< after its use.
type(vector_field):: to_position
type(state_type), intent(inout):: state
type(mesh_type), intent(in):: target_mesh
integer, parameter:: kloc(1:6)=(/ 2, 4, 5, 7, 8, 9 /)
integer, parameter:: iloc(1:6)=(/ 1, 1, 3, 1, 3, 6 /)
integer, parameter:: jloc(1:6)=(/ 3, 6, 6, 10, 10, 10 /)
type(vector_field), pointer:: position
type(element_type), pointer:: target_shape
real radi, radj
integer ele, k
position => extract_vector_field(state, "Coordinate")
if (position%mesh==target_mesh) then
to_position=position
! make this a new reference
call incref(to_position)
else
! if position is not on the same mesh as the target_mesh
call allocate(to_position, position%dim, target_mesh, &
name=trim(target_mesh%name)//'Coordinate')
call remap_field(position, to_position)
end if
target_shape => ele_shape(target_mesh, 1)
if (target_shape%degree==2 .and. have_option( &
'/geometry/spherical_earth/quadratic_superparametric_mapping')) then
do ele=1, element_count(to_position)
do k=1, 6
radi=sqrt(sum(ele_val(to_position, iloc(k))**2))
radj=sqrt(sum(ele_val(to_position, jloc(k))**2))
end do
end do
end if
end function make_coordinate_field
subroutine adaptivity_bounds(state, minch, maxch, name)
type(state_type), intent(inout) :: state
real, intent(in) :: minch, maxch
type(tensor_field) :: min_eigen, max_eigen
type(mesh_type), pointer :: mesh
character(len=*), intent(in), optional :: name
integer :: dim
if (present(name)) then
mesh => extract_mesh(state, trim(name))
else
mesh => extract_mesh(state, "Mesh")
end if
dim = mesh_dim(mesh)
call allocate(min_eigen, mesh, "MinMetricEigenbound", field_type=FIELD_TYPE_CONSTANT)
call allocate(max_eigen, mesh, "MaxMetricEigenbound", field_type=FIELD_TYPE_CONSTANT)
call set(min_eigen, get_matrix_identity(dim) * eigenvalue_from_edge_length(maxch))
call set(max_eigen, get_matrix_identity(dim) * eigenvalue_from_edge_length(minch))
call insert(state, min_eigen, "MinMetricEigenbound")
call insert(state, max_eigen, "MaxMetricEigenbound")
call deallocate(min_eigen)
call deallocate(max_eigen)
end subroutine adaptivity_bounds
subroutine adaptivity_options_scalar(state, field, weight, relative, min_psi)
type(state_type), intent(inout) :: state
type(scalar_field), intent(inout) :: field
real, intent(in) :: weight
logical, intent(in) :: relative
type(scalar_field) :: adaptivity_weight
real, intent(in), optional :: min_psi
integer :: stat
field%option_path = "/material_phase[0]/" // field%name
if (relative) then
call add_option(trim(field%option_path) // "/virtual/adaptivity_options/relative_measure", stat=stat)
call set_option(trim(field%option_path) // "/virtual/adaptivity_option&
&s/relative_measure/tolerance", min_psi, stat=stat)
else
call add_option(trim(field%option_path) // "/virtual/adaptivity_options/absolute_measure", stat=stat)
end if
call allocate(adaptivity_weight, field%mesh, trim(field%name) // "InterpolationErrorBound", FIELD_TYPE_CONSTANT)
call set(adaptivity_weight, weight)
call insert(state, adaptivity_weight, trim(field%name) // "InterpolationErrorBound")
call deallocate(adaptivity_weight)
end subroutine adaptivity_options_scalar
subroutine adaptivity_options_vector(state, field, weight, relative, min_psi)
type(state_type), intent(inout) :: state
type(vector_field), intent(inout) :: field
real, intent(in), dimension(:) :: weight
logical, intent(in) :: relative
type(vector_field) :: adaptivity_weight
real, intent(in), optional, dimension(:) :: min_psi
integer :: stat
field%option_path = "/material_phase[0]/" // field%name
if (relative) then
call add_option(trim(field%option_path) // "/virtual/adaptivity_options/relative_measure", stat=stat)
call set_option(trim(field%option_path) // "/virtual/adaptivity_options/relative_measure/tolerance", min_psi, stat=stat)
else
call add_option(trim(field%option_path) // "/virtual/adaptivity_options/absolute_measure", stat=stat)
end if
call allocate(adaptivity_weight, field%dim, field%mesh, trim(field%name) // "InterpolationErrorBound", FIELD_TYPE_CONSTANT)
call set(adaptivity_weight, weight)
call insert(state, adaptivity_weight, trim(field%name) // "InterpolationErrorBound")
call deallocate(adaptivity_weight)
end subroutine adaptivity_options_vector
function get_external_mesh(states, external_mesh_name) result(mesh)
type(state_type), dimension(:), intent(in) :: states
type(mesh_type), pointer:: mesh
integer :: nmeshes, i
character(len=OPTION_PATH_LEN) :: mesh_path
character(len=FIELD_NAME_LEN) linear_mesh_name
character(len=FIELD_NAME_LEN), intent(in), optional :: external_mesh_name
if (present(external_mesh_name)) then
mesh => extract_mesh(states(1), trim(external_mesh_name))
else
nmeshes=option_count("/geometry/mesh")
do i = 0, nmeshes-1
mesh_path="/geometry/mesh["//int2str(i)//"]"
if(have_option(trim(mesh_path)//"/from_file")) exit
end do
if (i>nmeshes-1) then
FLExit("Options tree does not have external mesh")
end if
call get_option(trim(mesh_path)//"/name", linear_mesh_name)
! We'll assume this one is the linear mesh
ewrite(2,*) "Assuming linear mesh is: " // trim(linear_mesh_name)
mesh => extract_mesh(states(1), linear_mesh_name)
end if
end function get_external_mesh
function get_external_coordinate_field(state, mesh) result (positions)
!!< returns a coordinate field called trim(mesh%name)//"Coordinate"
!!< pulled from state if present, otherwise it returns the "Coordinate" field
type(state_type), intent(in):: state
type(mesh_type), intent(in):: mesh
type(vector_field), pointer :: positions
if (has_vector_field(state, trim(mesh%name)//"Coordinate")) then
positions=>extract_vector_field(state, trim(mesh%name)//"Coordinate")
elseif (has_vector_field(state, "IteratedCoordinate")) then
! if the mesh is moving it's necessary to evaluate diagnostics on the most
! up to date coordinate.
! if the mesh is not moving this is just aliased to Coordinate anyway.
positions=>extract_vector_field(state, "IteratedCoordinate")
else
positions=>extract_vector_field(state, "Coordinate")
end if
end function get_external_coordinate_field
function get_diagnostic_coordinate_field(state, mesh) result (positions)
!!< Returns a coordinate field suitable for finite element integration.
!!< In most cases this will be the "Coordinate" field. In case of
!!< external meshes with the exclude_from_mesh_adaptivity_option, or in
!!< the case of the horizontal mesh in an extruded mesh setup, it will
!!< return the positions field stored for that mesh in state.
type(state_type), intent(in):: state
type(mesh_type), intent(in):: mesh
type(vector_field) positions
type(vector_field), pointer:: coordinate_field
type(mesh_type), pointer:: mesh_i
character(len=FIELD_NAME_LEN):: mesh_name
integer:: i
if (have_option(trim(mesh%option_path)//"/exclude_from_mesh_adaptivity")) then
positions=extract_vector_field(state, trim(mesh%name)//"Coordinate")
call incref(positions)
else
coordinate_field => extract_vector_field(state, "Coordinate")
if (mesh_dim(mesh)+1==mesh_dim(coordinate_field)) then
! coordinate is extruded, mesh is a horizontal mesh
if (mesh_periodic(mesh)) then
! search for the unperiodic mesh derived from it
do i=1, mesh_count(state)
mesh_i => extract_mesh(state, i)
if (have_option(trim(mesh_i%option_path)//"/from_mesh/mesh[0]/name") &
.and. have_option(trim(mesh_i%option_path)// &
"/from_mesh/periodic_boundary_conditions/remove_periodicity")) then
call get_option(trim(mesh_i%option_path)//"/name", mesh_name)
positions=extract_vector_field(state, trim(mesh%name)//"Coordinate")
call incref(positions)
return
end if
end do
! no mesh derived from it
ewrite(0,*) "Can't find a suitable unperiodic coordinate field for periodic mesh ", &
trim(mesh%name)
ewrite(0,*) "This probably means the operation you want to do on this field "//&
&"is not supported for a horizontal periodic mesh"
FLExit("No suitable coordinate field found.")
else
positions=extract_vector_field(state, trim(mesh%name)//"Coordinate")
call incref(positions)
end if
else if (element_count(coordinate_field)==element_count(mesh) .and. &
mesh_dim(coordinate_field)==mesh_dim(mesh)) then
positions=coordinate_field
call incref(positions)
else
ewrite(0,*) "Can't find a suitable coordinate field for mesh ", &
trim(mesh%name)
ewrite(0,*) "This probably means the operation you want to do is not supported for this mesh"
FLExit("No suitable coordinate field found.")
end if
end if
end function get_diagnostic_coordinate_field
function get_coordinate_field(state, mesh) result (positions)
!!< Returns a coordinate field for the given mesh, that has the same
!!< shape (and thus number of nodes) in each element. If the mesh is
!!< discontinuous or periodic however, the positions%mesh is not
!!< necessarily the same as 'mesh'. The returned positions field is
!!< never periodic and thus provides valid coordinates for each element
!!< individually.
!!< As the polynomial degree of the returned coordinates is the same as
!!< that of the input mesh, it won't return a superparametric
!!< "Coordinate" field. This routine should therefore only be used if
!!< there is an algorithmic reason to have the positions on the same
!!< nodes as 'mesh' in each element.
!!< NOTE: The returned vector_field should always be deallocated
type(state_type), intent(in):: state
type(mesh_type), intent(in):: mesh
type(vector_field) positions
type(vector_field), pointer:: coordinate_field
type(mesh_type) :: unperiodic_mesh
logical:: can_remap_from_coordinate_field
if (have_option(trim(mesh%option_path)//"/exclude_from_mesh_adaptivity").and.&
has_vector_field(state, trim(mesh%name)//"Coordinate")) then
positions=extract_vector_field(state, trim(mesh%name)//"Coordinate")
call incref(positions)
else
coordinate_field => extract_vector_field(state, "Coordinate")
can_remap_from_coordinate_field = &
element_count(coordinate_field)==element_count(mesh) .and. &
mesh_dim(coordinate_field)==mesh_dim(mesh)
if (mesh_dim(mesh)+1==mesh_dim(coordinate_field)) then
! coordinate is extruded, mesh is a horizontal mesh
if (mesh_periodic(mesh)) call give_up
positions=extract_vector_field(state, trim(mesh%name)//"Coordinate")
call incref(positions)
else if (mesh_periodic(mesh)) then
! never return periodic coordinates
if((coordinate_field%mesh%shape==mesh%shape).and.&
(coordinate_field%mesh%elements==mesh%elements).and.&
(coordinate_field%mesh%continuity==mesh%continuity)) then
! meshes are sufficiently similar that you shouldn't
! need to remake the mesh (hopefully)
positions=coordinate_field
call incref(positions)
else if (can_remap_from_coordinate_field) then
! meshes are different in an important way so
! remake the mesh using the shape and continuity
! of the desired mesh (but not periodic)
unperiodic_mesh=make_mesh(coordinate_field%mesh, mesh%shape, mesh%continuity, &
name="UnPeriodicCoordinateMesh")
call allocate(positions, coordinate_field%dim, unperiodic_mesh, &
name="Coordinate")
call remap_field(coordinate_field, positions)
call deallocate(unperiodic_mesh)
else
call give_up()
end if
elseif (has_vector_field(state, trim(mesh%name)//"Coordinate")) then
positions=extract_vector_field(state, trim(mesh%name)//"Coordinate")
call incref(positions)
elseif (coordinate_field%mesh==mesh) then
positions=coordinate_field
call incref(positions)
else if (can_remap_from_coordinate_field) then
call allocate(positions, coordinate_field%dim, mesh, name="Coordinate")
call remap_field(coordinate_field, positions)
else
call give_up()
end if
end if
contains
subroutine give_up()
ewrite(0,*) "Can't find a suitable coordinate field for mesh ", &
trim(mesh%name)
ewrite(0,*) "This probably means the operation you want to do is not "//&
&"supported for this mesh"
FLExit("No suitable coordinate field found.")
end subroutine give_up
end function get_coordinate_field
function get_nodal_coordinate_field(state, mesh) result (positions)
!!< Returns a coordinate field on the mesh provided. For periodic
!!< meshes the nodes on the periodic boundary will get the original
!!< position of the aliased nodes. The returned field is not suitable
!!< for finite element integration.
!!< NOTE: The returned vector_field should always be deallocated
type(state_type), intent(in):: state
type(mesh_type), intent(in):: mesh
type(vector_field) positions
type(vector_field), pointer:: coordinate_field
integer:: stat
if (has_vector_field(state, trim(mesh%name)//"Coordinate")) then
positions=extract_vector_field(state, trim(mesh%name)//"Coordinate")
call incref(positions)
else
coordinate_field => extract_vector_field(state, "Coordinate")
if (coordinate_field%mesh==mesh) then
positions=coordinate_field
call incref(positions)
else if (element_count(coordinate_field)==element_count(mesh) .and. &
mesh_dim(coordinate_field)==mesh_dim(mesh)) then
! can remap from coordinate field
call allocate(positions, coordinate_field%dim, mesh, name="Coordinate")
if (mesh_periodic(mesh) .and. continuity(mesh)>=0) then
call remap_field(coordinate_field, positions, stat=stat)
if (stat/=REMAP_ERR_UNPERIODIC_PERIODIC) then
FLAbort("Error remapping coordinate field")
end if
! make sure the remapped coordinates adhere to the convention
! of storing the aliased from position in the periodic boundary nodes
call postprocess_periodic_mesh(coordinate_field%mesh, coordinate_field, &
mesh, positions)
else
call remap_field(coordinate_field, positions)
end if
else
ewrite(0,*) "Can't find a suitable coordinate field for mesh ", &
trim(mesh%name)
ewrite(0,*) "This probably means the operation you want to do is not&
&supported for this mesh"
FLExit("No suitable coordinate field found.")
end if
end if
end function get_nodal_coordinate_field
function extract_pressure_mesh_from_state(state, stat)
type(state_type), intent(in):: state
type(mesh_type), pointer:: extract_pressure_mesh_from_state
integer, optional, intent(out):: stat
type(scalar_field), pointer:: p
p => extract_scalar_field(state, "Pressure", stat=stat)
if (associated(p)) then
extract_pressure_mesh_from_state => p%mesh
else
nullify(extract_pressure_mesh_from_state)
end if
end function extract_pressure_mesh_from_state
function extract_pressure_mesh_from_any_state(states, stat)
type(state_type), dimension(:), intent(in):: states
type(mesh_type), pointer:: extract_pressure_mesh_from_any_state
integer, optional, intent(out):: stat
type(scalar_field), pointer:: p
p => extract_scalar_field(states, "Pressure", stat=stat)
if (associated(p)) then
extract_pressure_mesh_from_any_state => p%mesh
else
nullify(extract_pressure_mesh_from_any_state)
end if
end function extract_pressure_mesh_from_any_state
function extract_velocity_mesh_from_state(state, stat)
type(state_type), intent(in):: state
type(mesh_type), pointer:: extract_velocity_mesh_from_state
integer, optional, intent(out):: stat
type(vector_field), pointer:: u
u => extract_vector_field(state, "Velocity", stat=stat)
if (associated(u)) then
extract_velocity_mesh_from_state => u%mesh
else
nullify(extract_velocity_mesh_from_state)
end if
end function extract_velocity_mesh_from_state
function extract_velocity_mesh_from_any_state(states, stat)
type(state_type), dimension(:), intent(in):: states
type(mesh_type), pointer:: extract_velocity_mesh_from_any_state
integer, optional, intent(out):: stat
type(vector_field), pointer:: u
u => extract_vector_field(states, "Velocity", stat=stat)
if (associated(u)) then
extract_velocity_mesh_from_any_state => u%mesh
else
nullify(extract_velocity_mesh_from_any_state)
end if
end function extract_velocity_mesh_from_any_state
subroutine select_fields_to_interpolate(state, interpolate_state, no_positions, &
first_time_step)
!!< This routine returns a state that is a selection of the fields
!!< of the input state that should be interpolated after adaptivity
!!< from the old to the new mesh.
!!< Diagnostic are never interpolated, although this could
!!< be useful for diagnostic purposes - again an option may be implemented.
!!< Aliased fields are obviously excluded.
type(state_type), intent(in):: state
type(state_type), intent(out):: interpolate_state
!! leave out the "Coordinate" field
logical, optional, intent(in) :: no_positions
!! exclude prognostic fields that can be re-initialised (not from_file)
logical, optional, intent(in) :: first_time_step
integer :: i
type(mesh_type), pointer :: mesh
type(scalar_field), pointer:: sfield => null()
type(vector_field), pointer:: vfield => null()
type(tensor_field), pointer:: tfield => null()
integer :: stat
interpolate_state%name = state%name
do i=1, mesh_count(state)
mesh => extract_mesh(state, i)
call insert(interpolate_state, mesh, trim(mesh%name))
if (.not. present_and_true(no_positions)) then
vfield => extract_vector_field(state, trim(mesh%name) // 'Coordinate', stat=stat)
if (stat == 0) then
call insert(interpolate_state, vfield, trim(mesh%name) // 'Coordinate')
end if
end if
end do
! Select all prognostic and prescribed scalar fields that do not have interpolation
! disabled. In addition, select diagnostic scalar fields that have interpolation enabled.
do i = 1, scalar_field_count(state)
sfield => extract_scalar_field(state, i)
if (interpolate_field(sfield, first_time_step=first_time_step)) then
ewrite(2,*) 'selecting to interpolate ', trim(sfield%name)
call insert(interpolate_state, sfield, sfield%name)
end if
end do
! Select all prognostic and prescribed vector fields that do not have interpolation
! disabled. In addition, select diagnostic vector fields that have interpolation enabled.
do i = 1, vector_field_count(state)
vfield => extract_vector_field(state, i)
if (interpolate_field(vfield, first_time_step=first_time_step)) then
ewrite(2,*) 'selecting to interpolate ', trim(vfield%name)
call insert(interpolate_state, vfield, vfield%name)
end if
end do
! Select all prognostic and prescribed tensor fields that do not have interpolation
! disabled. In addition, select diagnostic tensor fields that have interpolation enabled.
do i = 1, tensor_field_count(state)
tfield => extract_tensor_field(state, i)
if (interpolate_field(tfield, first_time_step=first_time_step)) then
ewrite(2,*) 'selecting to interpolate ', trim(tfield%name)
call insert(interpolate_state, tfield, tfield%name)
end if
end do
if (.not. present_and_true(no_positions)) then
! Need coordinate for interpolation
vfield => extract_vector_field(state, "Coordinate")
call insert(interpolate_state, vfield, vfield%name)
end if
end subroutine select_fields_to_interpolate
function interpolate_field_scalar(field, first_time_step) result (interpolate_field)
!!< Does this field want to be interpolated?
logical :: interpolate_field
type(scalar_field), intent(in) :: field
!! at first_time_step skip those prognostice fields that can be reinitialised
logical, optional, intent(in):: first_time_step
interpolate_field = (.not.aliased(field)) .and. &
interpolate_options(trim(field%option_path), first_time_step=first_time_step)
end function interpolate_field_scalar
function interpolate_field_vector(field, first_time_step) result (interpolate_field)
!!< Does this field want to be interpolated?
logical :: interpolate_field
type(vector_field), intent(in) :: field
!! at first_time_step skip those prognostice fields that can be reinitialised
logical, optional, intent(in):: first_time_step
interpolate_field = (.not.aliased(field)) .and. &
interpolate_options(trim(field%option_path), first_time_step=first_time_step)
end function interpolate_field_vector
function interpolate_field_tensor(field, first_time_step) result (interpolate_field)
!!< Does this field want to be interpolated?
logical :: interpolate_field
type(tensor_field), intent(in) :: field
!! at first_time_step skip those prognostice fields that can be reinitialised
logical, optional, intent(in):: first_time_step
interpolate_field = (.not.aliased(field)) .and. &
interpolate_options(trim(field%option_path), first_time_step=first_time_step)
end function interpolate_field_tensor
logical function interpolate_options(option_path, first_time_step)
!!< Does this option path make the associated field want to be interpolated?
character(len=*) :: option_path
!! at first_time_step skip those prognostice fields that can be reinitialised
logical, optional, intent(in):: first_time_step
if (present(first_time_step)) then
if (first_time_step) then
! if the field is prognostic/prescribed and needs its initial mesh
! it can't be reinitialised, so we need to interpolate
interpolate_options = needs_initial_mesh_options(option_path) .and. &
(have_option(trim(option_path) // "/prognostic") .or. &
have_option(trim(option_path) // "/prescribed"))
return
end if
end if
interpolate_options = (have_option(trim(option_path) // "/prognostic") .and. &
& .not. have_option(trim(option_path) // "/prognostic/no_interpolation")) &
.or. have_option(trim(option_path)//"/prescribed/galerkin_projection") &
.or. have_option(trim(option_path)//"/prescribed/consistent_interpolation") &
.or. have_option(trim(option_path)//"/prescribed/pseudo_consistent_interpolation") &
.or. have_option(trim(option_path)//"/prescribed/grandy_interpolation") &
.or. have_option(trim(option_path)//"/diagnostic/galerkin_projection") &
.or. have_option(trim(option_path)//"/diagnostic/consistent_interpolation") &
.or. have_option(trim(option_path)//"/diagnostic/pseudo_consistent_interpolation") &
.or. have_option(trim(option_path)//"/diagnostic/grandy_interpolation")
end function interpolate_options
function needs_initial_mesh_scalar(field) result (needs_initial_mesh)
!!< Does the field need the initial mesh for (re)initialisation or prescribing
!!< this is the case if any of its initial conditions are from_file
logical :: needs_initial_mesh
type(scalar_field), intent(in) :: field
needs_initial_mesh = needs_initial_mesh_options(field%option_path)
end function needs_initial_mesh_scalar
function needs_initial_mesh_vector(field) result (needs_initial_mesh)
!!< Does the field need the initial mesh for (re)initialisation or prescribing
!!< this is the case if any of its initial conditions are from_file
logical :: needs_initial_mesh
type(vector_field), intent(in) :: field
needs_initial_mesh = needs_initial_mesh_options(field%option_path)
end function needs_initial_mesh_vector
function needs_initial_mesh_tensor(field) result (needs_initial_mesh)
!!< Does the field need the initial mesh for (re)initialisation or prescribing
!!< this is the case if any of its initial conditions are from_file
logical :: needs_initial_mesh
type(tensor_field), intent(in) :: field
needs_initial_mesh = needs_initial_mesh_options(field%option_path)
end function needs_initial_mesh_tensor
logical function needs_initial_mesh_options(option_path)
!!< Does the field need the initial mesh for initialisation or prescribing
!!< this is the case if any of its initial conditions are from_file
character(len=*) :: option_path
character(len=OPTION_PATH_LEN):: initialisation_path
integer:: i
if (have_option(trim(option_path)//'/prognostic')) then
initialisation_path=trim(option_path)//'/prognostic/initial_condition'
else if (have_option(trim(option_path)//'/prescribed')) then
initialisation_path=trim(option_path)//'/prescribed/value'
else
! diagnostic fields are not initialised/prescribed anyway
if(.not. have_option(trim(option_path)//'/diagnostic/output/checkpoint')) then
needs_initial_mesh_options=.false.
return
end if
end if
! return .true. if any of the regions are initialised from file
do i=0, option_count(initialisation_path)-1
if (have_option(trim(initialisation_path)// &
'['//int2str(i)//']/from_file')) then
needs_initial_mesh_options=.true.
return
end if
end do
needs_initial_mesh_options=.false.
end function needs_initial_mesh_options
logical function do_not_recalculate(option_path)
!!< Does this option path tell us not to represcribe
character(len=*) :: option_path
integer :: stat
do_not_recalculate = have_option(trim(complete_field_path(option_path, stat=stat))//"/do_not_recalculate")
end function do_not_recalculate
subroutine find_linear_parent_mesh(state, mesh, parent_mesh, stat)
!!< Tries to find the parent mesh (possibly grand...parent mesh)
!!< that is linear and continuous by trawling through
!!< the options tree. It will have the same periodicity.
type(state_type), intent(in):: state
type(mesh_type), target, intent(in):: mesh
type(mesh_type), pointer:: parent_mesh
integer, optional, intent(out) :: stat
character(len=FIELD_NAME_LEN) parent_name
integer lstat
if(present(stat)) stat = 0
! start with mesh itself:
parent_mesh => mesh
do
! this is what we're looking for:
if (parent_mesh%shape%degree==1 .and. parent_mesh%continuity>=0) then
return
end if
call get_option(trim(parent_mesh%option_path)// &
'/from_mesh/mesh[0]/name', parent_name, stat=lstat)
if (lstat/=0) then
if (present(stat)) then
stat = 1
return
else
! this fails if parent is not derived, i.e. external, and not
! meeting our criteria (currently not possible as higher order
! meshes are always derived directly from a P1 mesh - can't
! extrude or periodise higher order meshes)
ewrite(-1,*) "Trying to find linear and continuous parent mesh of ", &
trim(mesh%name)
ewrite(-1,*) "External mesh ", trim(parent_mesh%name), &
&" does not meet these criteria"
FLExit("Linear input mesh required")
end if
end if
parent_mesh => extract_mesh(state, parent_name)
end do
end subroutine find_linear_parent_mesh
subroutine find_mesh_to_adapt(state, mesh)
!!< Finds the external mesh used as basis for adaptivity
!!< This has to be a continuous linear mesh. It has to be the
!!< external mesh as all other meshes will be derived from it.
type(state_type), intent(in):: state
type(mesh_type), pointer:: mesh
mesh => extract_mesh(state, adaptivity_mesh_name)
if (mesh%shape%degree/=1 .or. mesh%continuity<0) then
FLExit("For adaptivity external mesh needs to be linear and continuous.")
end if
end subroutine find_mesh_to_adapt
function convergence_norm_integer(option_path) result(norm)
!!< Return the integer index for the norm for the convergence index
!!< defaults to the infinity norm if no option found
integer :: norm
character(len=*), intent(in) :: option_path
if(have_option(trim(option_path)//"/l2_norm")) then
norm = CONVERGENCE_L2_NORM
else if(have_option(trim(option_path)//"/cv_l2_norm")) then
norm = CONVERGENCE_CV_L2_NORM
else
norm = CONVERGENCE_INFINITY_NORM
end if
end function convergence_norm_integer
integer function equation_type_index(option_path)
character(len=*) :: option_path
character(len=FIELD_NAME_LEN) :: equation_type
! find out equation type
call get_option(trim(option_path)//'/prognostic/equation[0]/name', &
equation_type, default="Unknown")
select case(trim(equation_type))
case ("AdvectionDiffusion")
equation_type_index = FIELD_EQUATION_ADVECTIONDIFFUSION
case ("HeatTransfer")
equation_type_index = FIELD_EQUATION_HEATTRANSFER
case ("ConservationOfMass")
equation_type_index = FIELD_EQUATION_CONSERVATIONOFMASS
case ("ReducedConservationOfMass")
equation_type_index = FIELD_EQUATION_REDUCEDCONSERVATIONOFMASS
case ( "InternalEnergy" )
equation_type_index = FIELD_EQUATION_INTERNALENERGY
case ( "ElectricalPotential" )
equation_type_index = FIELD_EQUATION_ELECTRICALPOTENTIAL
case ( "KEpsilon" )
equation_type_index = FIELD_EQUATION_KEPSILON
case default
equation_type_index = FIELD_EQUATION_UNKNOWN
end select
end function equation_type_index
subroutine collect_fields_by_mesh(states, mesh_names, mesh_states)
!! For each mesh_names(i) returns a mesh_states(i) that contains the corresponding mesh
!! and all fields in all states defined on that mesh. To distinguish between different fields
!! with the same name from different states, each field coming from states(j) gets inserted
!! as "State<j>FieldName", the actual field name is not changed.
type(state_type), dimension(:), intent(in) :: states
character(len=*), dimension(:), intent(in) :: mesh_names
type(state_type), dimension(:), intent(out) :: mesh_states
type(integer_hash_table) :: refcount_to_meshno
integer :: i, j, mesh_no
type(mesh_type), pointer :: mesh
type(scalar_field), pointer :: sfield
type(vector_field), pointer :: vfield
type(tensor_field), pointer :: tfield
assert( size(mesh_names)==size(mesh_states) )
call allocate(refcount_to_meshno)
do i=1, size(mesh_names)
mesh => extract_mesh(states(1), mesh_names(i))
call insert(mesh_states(i), mesh, mesh_names(i))
call insert(refcount_to_meshno, mesh%refcount%id, i)
end do
do i=1,size(states)
do j=1,scalar_field_count(states(i))
sfield => extract_scalar_field(states(i), j)
if(has_key(refcount_to_meshno, sfield%mesh%refcount%id)) then
mesh_no = fetch(refcount_to_meshno, sfield%mesh%refcount%id)
call insert(mesh_states(mesh_no), sfield, "State" // int2str(i) // trim(sfield%name))
end if
end do
sfield => null()
do j=1,vector_field_count(states(i))
vfield => extract_vector_field(states(i), j)
if(has_key(refcount_to_meshno, vfield%mesh%refcount%id)) then
mesh_no = fetch(refcount_to_meshno, vfield%mesh%refcount%id)
call insert(mesh_states(mesh_no), vfield, "State" // int2str(i) // trim(vfield%name))
end if
end do