-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathFields_Allocates.F90
More file actions
3317 lines (2642 loc) · 114 KB
/
Fields_Allocates.F90
File metadata and controls
3317 lines (2642 loc) · 114 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 fields_allocates
use fldebug
use global_parameters, only: PYTHON_FUNC_LEN, empty_path, empty_name, &
topology_mesh_name, NUM_COLOURINGS
use futils, only: present_and_true
use quadrature
use element_numbering
use elements
use ieee_arithmetic
use halo_data_types
use parallel_tools
use halos_allocates
use memory_diagnostics
use data_structures
use sparse_tools
use shape_functions, only: make_element_shape
use fields_data_types
use fields_base
use halos_repair
use pickers_deallocates
use adjacency_lists
use global_numbering, only: make_global_numbering, make_global_numbering_dg,&
&make_global_numbering_trace
implicit none
private
public :: allocate, deallocate, incref, decref, has_references, add_faces, &
& deallocate_faces, zero
public :: make_element_shape, make_mesh, make_mesh_periodic, make_submesh, &
& create_surface_mesh, make_fake_mesh_linearnonconforming
public :: extract_scalar_field, wrap_mesh, wrap_scalar_field, &
& wrap_tensor_field
public :: add_lists, extract_lists, add_nnlist, extract_nnlist, add_nelist, &
& extract_nelist, add_eelist, extract_eelist, remove_lists, remove_nnlist, &
& remove_nelist, remove_eelist, extract_elements, remove_boundary_conditions
interface allocate
module procedure allocate_scalar_field, allocate_vector_field,&
& allocate_tensor_field, allocate_mesh, &
& allocate_scalar_boundary_condition, &
& allocate_vector_boundary_condition
end interface
interface deallocate
module procedure deallocate_mesh, deallocate_scalar_field,&
& deallocate_vector_field, deallocate_tensor_field, &
& deallocate_scalar_boundary_condition, &
& deallocate_vector_boundary_condition
end interface
interface zero
module procedure zero_scalar, zero_vector, zero_tensor, &
zero_vector_dim, zero_tensor_dim_dim, &
zero_scalar_field_nodes, zero_vector_field_nodes, zero_tensor_field_nodes
end interface
interface deallocate_faces
module procedure deallocate_mesh_faces
end interface
interface add_lists
module procedure add_lists_mesh, add_lists_scalar, add_lists_vector, &
& add_lists_tensor
end interface add_lists
interface extract_lists
module procedure extract_lists_mesh, extract_lists_scalar, &
& extract_lists_vector, extract_lists_tensor
end interface extract_lists
interface add_nnlist
module procedure add_nnlist_mesh, add_nnlist_scalar, add_nnlist_vector, &
& add_nnlist_tensor
end interface add_nnlist
interface extract_nnlist
module procedure extract_nnlist_mesh, extract_nnlist_scalar, &
& extract_nnlist_vector, extract_nnlist_tensor
end interface extract_nnlist
interface add_nelist
module procedure add_nelist_mesh, add_nelist_scalar, add_nelist_vector, &
& add_nelist_tensor
end interface add_nelist
interface extract_nelist
module procedure extract_nelist_mesh, extract_nelist_scalar, &
& extract_nelist_vector, extract_nelist_tensor
end interface extract_nelist
interface add_eelist
module procedure add_eelist_mesh, add_eelist_scalar, add_eelist_vector, &
& add_eelist_tensor
end interface add_eelist
interface extract_eelist
module procedure extract_eelist_mesh, extract_eelist_scalar, &
& extract_eelist_vector, extract_eelist_tensor
end interface extract_eelist
interface remove_lists
module procedure remove_lists_mesh
end interface remove_lists
interface remove_nnlist
module procedure remove_nnlist_mesh
end interface remove_nnlist
interface remove_nelist
module procedure remove_nelist_mesh
end interface remove_nelist
interface remove_eelist
module procedure remove_eelist_mesh
end interface remove_eelist
interface remove_boundary_conditions
module procedure remove_boundary_conditions_scalar, &
remove_boundary_conditions_vector
end interface remove_boundary_conditions
#include "Reference_count_interface_mesh_type.F90"
#include "Reference_count_interface_scalar_field.F90"
#include "Reference_count_interface_vector_field.F90"
#include "Reference_count_interface_tensor_field.F90"
contains
subroutine allocate_mesh(mesh, nodes, elements, shape, name)
type(mesh_type), intent(out) :: mesh
integer, intent(in) :: nodes, elements
type(element_type), target, intent(in) :: shape
character(len=*), intent(in), optional :: name
integer :: i
#ifdef _OPENMP
integer :: j
#endif
mesh%nodes=nodes
mesh%elements=elements
mesh%shape=shape
call incref(shape)
if (present(name)) then
mesh%name=name
else
mesh%name=empty_name
end if
! should happen in derived type initialisation already,
! but just to make sure in case an mesh variable is supplied
! that has previously been used for something else:
nullify(mesh%faces)
nullify(mesh%columns)
nullify(mesh%element_columns)
allocate(mesh%colourings(NUM_COLOURINGS))
do i = 1, NUM_COLOURINGS
nullify(mesh%colourings(i)%sets)
end do
allocate(mesh%ndglno(elements*shape%loc))
#ifdef _OPENMP
! Use first touch policy.
!$OMP PARALLEL DO SCHEDULE(STATIC)
do i=1, mesh%elements
do j=1, shape%loc
mesh%ndglno((i-1)*shape%loc+j)=0
end do
end do
!$OMP END PARALLEL DO
#endif
#ifdef HAVE_MEMORY_STATS
call register_allocation("mesh_type", "integer", elements*shape%loc,&
& name=mesh%name)
#endif
allocate(mesh%adj_lists)
mesh%wrapped=.false.
nullify(mesh%region_ids)
nullify(mesh%subdomain_mesh)
nullify(mesh%refcount) ! Hack for gfortran component initialisation
! bug.
mesh%periodic=.false.
call addref(mesh)
end subroutine allocate_mesh
subroutine allocate_scalar_field(field, mesh, name, field_type, py_func, py_positions)
type(scalar_field), intent(out) :: field
type(mesh_type), intent(in), target :: mesh
character(len=*), intent(in),optional :: name
integer, intent(in), optional :: field_type
character(len=*), intent(in), optional :: py_func
type(vector_field), intent(in), optional, target :: py_positions
integer :: lfield_type
integer :: stat
integer :: toloc, fromloc
if (present(field_type)) then
lfield_type = field_type
else
lfield_type = FIELD_TYPE_NORMAL
end if
field%mesh=mesh
call incref(mesh)
if (present(name)) then
field%name=name
else
field%name=empty_name
end if
field%field_type = lfield_type
select case(lfield_type)
case(FIELD_TYPE_NORMAL)
allocate(field%val(node_count(mesh)))
field%py_dim = mesh_dim(mesh)
field%py_positions_shape => mesh%shape
#ifdef HAVE_MEMORY_STATS
call register_allocation("scalar_field", "real", node_count(mesh), &
name=name)
#endif
case(FIELD_TYPE_CONSTANT)
allocate(field%val(1))
field%py_dim = mesh_dim(mesh)
field%py_positions_shape => mesh%shape
#ifdef HAVE_MEMORY_STATS
call register_allocation("scalar_field", "real", 1, name=name)
#endif
case(FIELD_TYPE_DEFERRED)
allocate(field%val(0))
field%py_dim = mesh_dim(mesh)
field%py_positions_shape => mesh%shape
case(FIELD_TYPE_PYTHON)
if (present(py_func)) then
field%py_func = py_func
else
if (stat /= 0) then
FLAbort("Field specified as FIELD_TYPE_PYTHON, but no func passed!")
end if
end if
if (.not. present(py_positions)) then
FLAbort("Field specified as FIELD_TYPE_PYTHON but no positions field passed!")
end if
field%py_positions => py_positions
field%py_dim = py_positions%dim
field%py_positions_shape => py_positions%mesh%shape
call incref(field%py_positions_shape)
call incref(field%py_positions)
if (associated(py_positions%mesh%refcount, mesh%refcount)) then
field%py_positions_same_mesh = .true.
else
field%py_positions_same_mesh = .false.
allocate(field%py_locweight(mesh%shape%loc, py_positions%mesh%shape%loc))
do toloc=1,size(field%py_locweight,1)
do fromloc=1,size(field%py_locweight,2)
field%py_locweight(toloc,fromloc)=eval_shape(py_positions%mesh%shape, fromloc, &
local_coords(toloc, mesh%shape))
end do
end do
end if
call add_nelist(field%mesh)
end select
field%wrapped=.false.
field%aliased=.false.
field%option_path=empty_path
allocate(field%bc)
nullify(field%refcount) ! Hacks for gfortran component initialisation
! bug.
call addref(field)
call zero(field)
end subroutine allocate_scalar_field
subroutine allocate_vector_field(field, dim, mesh, name, field_type)
type(vector_field), intent(out) :: field
integer, intent(in) :: dim
type(mesh_type), intent(in), target :: mesh
character(len=*), intent(in), optional :: name
integer, intent(in), optional :: field_type
integer :: n_count
integer :: lfield_type
if (present(field_type)) then
lfield_type = field_type
else
lfield_type = FIELD_TYPE_NORMAL
end if
field%dim=dim
field%option_path=empty_path
field%mesh=mesh
call incref(mesh)
if (present(name)) then
field%name=name
else
field%name=empty_name
end if
field%field_type = lfield_type
select case(lfield_type)
case(FIELD_TYPE_NORMAL)
n_count = node_count(mesh)
allocate(field%val(dim,n_count))
#ifdef HAVE_MEMORY_STATS
call register_allocation("vector_field", "real", n_count*dim, &
name=name)
#endif
case(FIELD_TYPE_CONSTANT)
allocate(field%val(dim,1))
#ifdef HAVE_MEMORY_STATS
call register_allocation("vector_field", "real", dim, name=name)
#endif
case(FIELD_TYPE_DEFERRED)
allocate(field%val(0,0))
end select
field%wrapped = .false.
field%aliased = .false.
allocate(field%bc)
nullify(field%refcount) ! Hack for gfortran component initialisation
! bug.
allocate(field%picker)
call addref(field)
call zero(field)
end subroutine allocate_vector_field
subroutine allocate_tensor_field(field, mesh, name, field_type, dim)
type(tensor_field), intent(inout) :: field
type(mesh_type), intent(in), target :: mesh
character(len=*), intent(in), optional :: name
integer, intent(in), optional :: field_type
integer, intent(in), dimension(2), optional :: dim
integer :: lfield_type
if (present(field_type)) then
lfield_type = field_type
else
lfield_type = FIELD_TYPE_NORMAL
end if
if(present(dim)) then
field%dim = dim
else
field%dim=(/mesh_dim(mesh),mesh_dim(mesh)/)
end if
field%option_path=empty_path
field%mesh=mesh
call incref(mesh)
if (present(name)) then
field%name=name
else
field%name=empty_name
end if
field%field_type = lfield_type
select case(lfield_type)
case(FIELD_TYPE_NORMAL)
allocate(field%val(field%dim(1), field%dim(2), node_count(mesh)))
#ifdef HAVE_MEMORY_STATS
call register_allocation("tensor_field", "real", &
node_count(mesh)*field%dim(1)*field%dim(2), name=name)
#endif
case(FIELD_TYPE_CONSTANT)
allocate(field%val(field%dim(1), field%dim(2), 1))
#ifdef HAVE_MEMORY_STATS
call register_allocation("tensor_field", "real", &
field%dim(1)*field%dim(2), name=name)
#endif
case(FIELD_TYPE_DEFERRED)
allocate(field%val(0, 0, 0))
end select
field%wrapped=.false.
field%aliased=.false.
nullify(field%refcount) ! Hack for gfortran component initialisation
! bug.
call addref(field)
call zero(field)
end subroutine allocate_tensor_field
subroutine deallocate_subdomain_mesh(mesh)
type(mesh_type) :: mesh
if (.not.associated(mesh%subdomain_mesh)) return
deallocate(mesh%subdomain_mesh%element_list)
deallocate(mesh%subdomain_mesh%node_list)
deallocate(mesh%subdomain_mesh)
end subroutine deallocate_subdomain_mesh
subroutine deallocate_mesh_faces(mesh)
type(mesh_type) :: mesh
if (.not.associated(mesh%faces)) return
call deallocate(mesh%faces%face_list)
#ifdef HAVE_MEMORY_STATS
call register_deallocation("mesh_type", "integer", &
size(mesh%faces%face_lno), name=mesh%name)
#endif
deallocate(mesh%faces%face_lno)
#ifdef HAVE_MEMORY_STATS
call register_deallocation("mesh_type", "integer", &
size(mesh%faces%face_element_list), &
name=mesh%name)
#endif
deallocate(mesh%faces%face_element_list)
call deallocate(mesh%faces%shape%quadrature)
call deallocate(mesh%faces%shape)
deallocate(mesh%faces%shape)
call deallocate(mesh%faces%surface_mesh)
#ifdef HAVE_MEMORY_STATS
call register_deallocation("mesh_type", "integer", &
size(mesh%faces%surface_node_list), name=trim(mesh%name)//" surface_nodes")
#endif
deallocate(mesh%faces%surface_node_list)
#ifdef HAVE_MEMORY_STATS
call register_deallocation("mesh_type", "integer", &
size(mesh%faces%boundary_ids), &
name=trim(mesh%name)//" boundary_ids")
#endif
deallocate(mesh%faces%boundary_ids)
if (associated(mesh%faces%coplanar_ids)) then
deallocate(mesh%faces%coplanar_ids)
end if
if (associated(mesh%faces%dg_surface_mesh)) then
call deallocate(mesh%faces%dg_surface_mesh)
deallocate(mesh%faces%dg_surface_mesh)
end if
deallocate(mesh%faces)
end subroutine deallocate_mesh_faces
subroutine deallocate_mesh(mesh)
!!< Deallocate the components of mesh. Shape functions are not
!!< deallocated here.
type(mesh_type), intent(inout) :: mesh
integer :: i
call decref(mesh)
if (has_references(mesh)) then
! There are still references to this mesh so we don't deallocate.
return
end if
call deallocate(mesh%shape)
if (.not.mesh%wrapped) then
#ifdef HAVE_MEMORY_STATS
call register_deallocation("mesh_type", "integer", &
size(mesh%ndglno), name=mesh%name)
#endif
deallocate(mesh%ndglno)
end if
if(associated(mesh%region_ids)) then
deallocate(mesh%region_ids)
end if
assert(associated(mesh%adj_lists))
call remove_lists(mesh)
deallocate(mesh%adj_lists)
nullify(mesh%adj_lists)
if(associated(mesh%halos)) then
call deallocate(mesh%halos)
deallocate(mesh%halos)
end if
if(associated(mesh%element_halos)) then
call deallocate(mesh%element_halos)
deallocate(mesh%element_halos)
end if
if (associated(mesh%surface_names)) then
deallocate(mesh%surface_names)
nullify(mesh%surface_names)
end if
call deallocate_faces(mesh)
if(associated(mesh%subdomain_mesh)) then
call deallocate_subdomain_mesh(mesh)
end if
if(associated(mesh%columns)) then
deallocate(mesh%columns)
end if
if(associated(mesh%element_columns)) then
deallocate(mesh%element_columns)
end if
if(associated(mesh%colourings)) then
do i = 1, NUM_COLOURINGS
if(associated(mesh%colourings(i)%sets)) then
call deallocate(mesh%colourings(i)%sets)
deallocate(mesh%colourings(i)%sets)
end if
end do
deallocate(mesh%colourings)
end if
end subroutine deallocate_mesh
recursive subroutine deallocate_scalar_field(field)
!!< Deallocate the storage associated with the field values. Deallocate
!!< is called on the mesh which will delete one reference to it and
!!< deallocate it if the count drops to zero.
type(scalar_field), intent(inout) :: field
call decref(field)
if (has_references(field)) then
! There are still references to this field so we don't deallocate.
return
end if
select case(field%field_type)
case(FIELD_TYPE_NORMAL)
if (.not.field%wrapped) then
#ifdef HAVE_MEMORY_STATS
call register_deallocation("scalar_field", "real", &
size(field%val), name=field%name)
#endif
#ifdef DDEBUG
field%val = ieee_value(0.0, ieee_quiet_nan)
#endif
deallocate(field%val)
end if
case(FIELD_TYPE_CONSTANT)
#ifdef HAVE_MEMORY_STATS
call register_deallocation("scalar_field", "real", &
1, name=field%name)
#endif
#ifdef DDEBUG
field%val = ieee_value(0.0, ieee_quiet_nan)
#endif
deallocate(field%val)
case(FIELD_TYPE_PYTHON)
call deallocate(field%py_positions)
call deallocate(field%py_positions_shape)
if (associated(field%py_locweight)) then
deallocate(field%py_locweight)
end if
case(FIELD_TYPE_DEFERRED)
FLAbort("You were supposed to allocate the deferred field later!")
end select
call deallocate(field%mesh)
call remove_boundary_conditions(field)
deallocate(field%bc)
end subroutine deallocate_scalar_field
subroutine remove_boundary_conditions_scalar(field)
!!< Removes and deallocates all boundary conditions from a field
type(scalar_field), intent(inout):: field
integer:: i
if (associated(field%bc%boundary_condition)) then
do i=1, size(field%bc%boundary_condition)
call deallocate(field%bc%boundary_condition(i))
end do
deallocate(field%bc%boundary_condition)
end if
end subroutine remove_boundary_conditions_scalar
recursive subroutine deallocate_vector_field(field)
!!< Deallocate the storage associated with the field values. Deallocate
!!< is called on the mesh which will delete one reference to it and
!!< deallocate it if the count drops to zero.
type(vector_field), intent(inout) :: field
call decref(field)
if (has_references(field)) then
! There are still references to this field so we don't deallocate.
return
end if
if (.not.field%wrapped) then
select case(field%field_type)
case(FIELD_TYPE_NORMAL,FIELD_TYPE_CONSTANT)
#ifdef DDEBUG
field%val = ieee_value(0.0, ieee_quiet_nan)
#endif
#ifdef HAVE_MEMORY_STATS
call register_deallocation("vector_field", "real", &
size(field%val), name=field%name)
#endif
deallocate(field%val)
case(FIELD_TYPE_DEFERRED)
FLAbort("You were supposed to allocate the deferred field later!")
end select
end if
call deallocate(field%mesh)
call remove_boundary_conditions(field)
deallocate(field%bc)
assert(associated(field%picker))
call remove_picker(field)
deallocate(field%picker)
nullify(field%picker)
end subroutine deallocate_vector_field
subroutine remove_boundary_conditions_vector(field)
!!< Removes and deallocates all boundary conditions from a field
type(vector_field), intent(inout):: field
integer:: i
if (associated(field%bc%boundary_condition)) then
do i=1, size(field%bc%boundary_condition)
call deallocate(field%bc%boundary_condition(i))
end do
deallocate(field%bc%boundary_condition)
end if
end subroutine remove_boundary_conditions_vector
subroutine deallocate_tensor_field(field)
!!< Deallocate the storage associated with the field values. Deallocate
!!< is called on the mesh which will delete one reference to it and
!!< deallocate it if the count drops to zero.
type(tensor_field), intent(inout) :: field
call decref(field)
if (has_references(field)) then
! There are still references to this field so we don't deallocate.
return
end if
if (.not.field%wrapped) then
select case(field%field_type)
case(FIELD_TYPE_NORMAL,FIELD_TYPE_CONSTANT)
#ifdef HAVE_MEMORY_STATS
call register_deallocation("tensor_field", "real", &
size(field%val), field%name)
#endif
#ifdef DDEBUG
field%val = ieee_value(0.0, ieee_quiet_nan)
#endif
deallocate(field%val)
case(FIELD_TYPE_DEFERRED)
FLAbort("You were supposed to allocate the deferred field later!")
end select
end if
call deallocate(field%mesh)
end subroutine deallocate_tensor_field
subroutine allocate_scalar_boundary_condition(bc, mesh, surface_element_list, &
name, type)
!!< Allocate a scalar boundary condition
type(scalar_boundary_condition), intent(out):: bc
type(mesh_type), intent(in):: mesh
!! surface elements to which this b.c. applies (is copied in)
integer, dimension(:), intent(in):: surface_element_list
!! all things should have a name
character(len=*), intent(in):: name
!! type can be any of: ...
character(len=*), intent(in):: type
bc%name=name
bc%type=type
allocate( bc%surface_element_list(1:size(surface_element_list)) )
bc%surface_element_list=surface_element_list
allocate(bc%surface_mesh)
call create_surface_mesh(bc%surface_mesh, bc%surface_node_list, &
mesh, bc%surface_element_list, name=trim(name)//'Mesh')
end subroutine allocate_scalar_boundary_condition
subroutine allocate_vector_boundary_condition(bc, mesh, surface_element_list, &
applies, name, type)
!!< Allocate a vector boundary condition
type(vector_boundary_condition), intent(out):: bc
type(mesh_type), intent(in):: mesh
!! surface elements of this mesh to which this b.c. applies (is copied in):
integer, dimension(:), intent(in):: surface_element_list
!! all things should have a name
character(len=*), intent(in):: name
!! type can be any of: ...
character(len=*), intent(in):: type
!! b.c. only applies for components with applies==.true.
logical, dimension(:), intent(in), optional:: applies
bc%name=name
bc%type=type
allocate( bc%surface_element_list(1:size(surface_element_list)) )
bc%surface_element_list=surface_element_list
allocate(bc%surface_mesh)
call create_surface_mesh(bc%surface_mesh, bc%surface_node_list, &
mesh, bc%surface_element_list, name=trim(name)//'Mesh')
if (present(applies)) then
! size(bc%applies) is always 3! also for dim<3
bc%applies(1:size(applies))=applies
bc%applies(size(applies)+1:)=.false.
else
! default .true. for all components
bc%applies=.true.
end if
end subroutine allocate_vector_boundary_condition
subroutine deallocate_scalar_boundary_condition(bc)
!! deallocate a scalar boundary condition
type(scalar_boundary_condition), intent(inout):: bc
integer i
if (associated(bc%surface_fields)) then
do i=1, size(bc%surface_fields)
call deallocate(bc%surface_fields(i))
end do
deallocate(bc%surface_fields)
end if
call deallocate(bc%surface_mesh)
deallocate(bc%surface_mesh)
deallocate(bc%surface_element_list, bc%surface_node_list)
end subroutine deallocate_scalar_boundary_condition
subroutine deallocate_vector_boundary_condition(bc)
!! deallocate a vector boundary condition
type(vector_boundary_condition), intent(inout):: bc
integer i
if (associated(bc%surface_fields)) then
do i=1, size(bc%surface_fields)
call deallocate(bc%surface_fields(i))
end do
deallocate(bc%surface_fields)
end if
if (associated(bc%scalar_surface_fields)) then
do i=1, size(bc%scalar_surface_fields)
call deallocate(bc%scalar_surface_fields(i))
end do
deallocate(bc%scalar_surface_fields)
end if
call deallocate(bc%surface_mesh)
deallocate(bc%surface_mesh)
deallocate(bc%surface_element_list, bc%surface_node_list)
end subroutine deallocate_vector_boundary_condition
!---------------------------------------------------------------------
! routines for wrapping meshes and fields around provided arrays
!---------------------------------------------------------------------
function wrap_mesh(ndglno, shape, name) result (mesh)
!!< Return a mesh wrapped around the information provided.
type(mesh_type) :: mesh
integer, dimension(:), target, intent(in) :: ndglno
type(element_type), target, intent(in) :: shape
character(len=*), intent(in) :: name
mesh%ndglno=>ndglno
mesh%shape=shape
call incref(shape)
nullify(mesh%faces)
mesh%name=name
mesh%elements=size(ndglno)/shape%loc
allocate(mesh%adj_lists)
mesh%wrapped=.true.
mesh%nodes=maxval(ndglno)
nullify(mesh%refcount) ! Hack for gfortran component initialisation
! bug.
mesh%periodic = .false. ! can only really assume that this is false as
! we have no other information
call addref(mesh)
end function wrap_mesh
function wrap_scalar_field(mesh, val, name, val_stride) result (field)
!!< Return a scalar field wrapped around the arrays provided.
type(scalar_field) :: field
type(mesh_type), target, intent(in) :: mesh
real, dimension(:), target, intent(in) :: val
character(len=*), intent(in) :: name
!! has to be provided if the val array is non-contiguous in memory!
integer, optional:: val_stride
field%val=>val
field%mesh=mesh
field%name=name
if (present(val_stride)) then
field%val_stride=val_stride
else
field%val_stride=1
end if
field%py_dim = mesh_dim(mesh)
field%py_positions_shape => mesh%shape
field%wrapped = .true.
call incref(mesh)
allocate(field%bc)
nullify(field%refcount) ! Hack for gfortran component initialisation
! bug.
call addref(field)
end function wrap_scalar_field
function wrap_tensor_field(mesh, val, name) result (field)
!!< Return a tensor field wrapped around the arrays provided.
type(tensor_field) :: field
type(mesh_type), target, intent(in) :: mesh
real, dimension(mesh_dim(mesh), mesh_dim(mesh), node_count(mesh)),&
& target, intent(in) :: val
character(len=*), intent(in) :: name
field%val=>val
field%mesh=mesh
field%dim=mesh_dim(mesh)
field%name=name
field%wrapped=.true.
call incref(mesh)
nullify(field%refcount) ! Hack for gfortran component initialisation
! bug.
call addref(field)
end function wrap_tensor_field
function make_mesh (model, shape, continuity, name) &
result (mesh)
!!< Produce a mesh based on an old mesh but with a different shape and/or continuity.
type(mesh_type) :: mesh
type(mesh_type), intent(in) :: model
type(element_type), target, intent(in), optional :: shape
integer, intent(in), optional :: continuity
character(len=*), intent(in), optional :: name
integer, dimension(:), allocatable :: ndglno
real, dimension(:), pointer :: val
integer :: i, input_nodes, n_faces
#ifdef _OPENMP
integer :: j
#endif
if (present(continuity)) then
mesh%continuity=continuity
else
mesh%continuity=model%continuity
end if
allocate(mesh%adj_lists)
mesh%elements=model%elements
mesh%periodic=model%periodic
mesh%wrapped=.false.
if (present(shape)) then
mesh%shape=shape
else
mesh%shape=model%shape
end if
call incref(mesh%shape)
! You can't have a CG degree 0 mesh!
if(mesh%shape%degree==0.and.mesh%continuity>=0.and.mesh%shape&
&%numbering%type/=ELEMENT_TRACE) then
FLExit("For a P0 mesh, the 'mesh_continuity' must be Discontinuous.")
end if
if (present(name)) then
mesh%name=name
else
mesh%name=empty_name
end if
if (associated(model%region_ids)) then
allocate(mesh%region_ids(size(model%region_ids)))
mesh%region_ids=model%region_ids
end if
if (mesh%continuity>=0) then
! Make a continuous field.
if (model%continuity<0) then
FLExit("Unable to derive a continuous mesh from a discontinuous mesh")
end if
allocate(ndglno(mesh%shape%numbering%vertices*model%elements), &
mesh%ndglno(mesh%shape%loc*model%elements))
#ifdef _OPENMP
! Use first touch policy.
!$OMP PARALLEL DO SCHEDULE(STATIC)
do i=1, mesh%elements
do j=1, mesh%shape%loc
mesh%ndglno((i-1)*mesh%shape%loc+j)=0
end do
end do
!$OMP END PARALLEL DO
#endif
#ifdef HAVE_MEMORY_STATS
call register_allocation("mesh_type", "integer", &
size(mesh%ndglno), name=name)
#endif
if(model%shape%degree==1 .or. ele_count(model) == 0) then
ndglno=model%ndglno
input_nodes = node_count(model)
else
ndglno=mesh_connectivity(model)
input_nodes = maxval(ndglno)
end if
if (associated(model%halos)) then
assert(element_halo_count(model) > 0)