-
Notifications
You must be signed in to change notification settings - Fork 113
/
Particles.F90
2312 lines (1974 loc) · 91.8 KB
/
Particles.F90
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 Engineering
! 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 PublicS
! 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 particles
use fldebug
use iso_c_binding, only: C_NULL_CHAR, c_ptr, c_f_pointer
use global_parameters, only:FIELD_NAME_LEN,OPTION_PATH_LEN, &
& PYTHON_FUNC_LEN, integer_size, real_size, is_active_process
use futils, only: int2str, free_unit
use elements
use mpi_interfaces
use parallel_tools
use spud
use embed_python, only: set_detectors_from_python, deallocate_c_array
use parallel_fields
use fields
use profiler
use state_module
use field_options
use detector_data_types
use pickers
use detector_tools
use detector_parallel
use detector_move_lagrangian
use time_period
use h5hut
implicit none
private
public :: initialise_particles, move_particles, write_particles_loop, destroy_particles, &
update_particle_attributes_and_fields, checkpoint_particles_loop, &
get_particle_arrays, particle_lists, initialise_constant_particle_attributes, &
initialise_particles_during_simulation
! One particle list for each subgroup
type(detector_linked_list), allocatable, dimension(:), target, save :: particle_lists
! Timing info for group output
type(time_period_type), allocatable, dimension(:), save :: output_CS
!> Derived type to hold the number of scalar, vector and tensor attributes,
!! old attributes and old fields for a particle subgroup
type attr_counts_type
integer, dimension(3) :: attrs, old_attrs, old_fields
end type attr_counts_type
!> Derived type to hold scalar, vector and tensor attributes
type attr_vals_type
real, dimension(:), allocatable :: s
real, dimension(:,:), allocatable :: v
real, dimension(:,:,:), allocatable :: t
end type attr_vals_type
interface allocate
module procedure allocate_attr_vals
end interface allocate
interface deallocate
module procedure deallocate_attr_vals
end interface deallocate
contains
!> Allocate an attr_vals_type structure, with the given number
!! of scalar, vector and tensor attributes, and the geometric dimension
!! of the problem.
subroutine allocate_attr_vals(vals, dim, counts)
!> Structure to allocate
type(attr_vals_type), pointer :: vals
!> Geometric dimension
integer, intent(in) :: dim
!> Counts of each rank of attribute
integer, dimension(3), intent(in) :: counts
allocate(vals)
allocate(vals%s(counts(1)))
allocate(vals%v(dim, counts(2)))
allocate(vals%t(dim, dim, counts(3)))
end subroutine allocate_attr_vals
!> Deallocate an attr_vals_type structure
subroutine deallocate_attr_vals(vals)
type(attr_vals_type), pointer :: vals
deallocate(vals%s)
deallocate(vals%v)
deallocate(vals%t)
deallocate(vals)
end subroutine deallocate_attr_vals
!> Initialise particles and set up particle file headers (per particle array)
subroutine initialise_particles(filename, state, global, setup_output, ignore_analytical, number_of_partitions)
!> Experiment filename to prefix particle output files
character(len=*), intent(in) :: filename
!> Model state structure
type(state_type), dimension(:), intent(in) :: state
!> Use global/parallel picker queries to determine particle elements?
logical, intent(in), optional :: global
!> Whether to set up output files for particle lists
logical, intent(in), optional :: setup_output
!> Whether to ignore analytical particles (i.e. not from file)
logical, intent(in), optional :: ignore_analytical
!> Number of processes to use for reading particle data
integer, intent(in), optional :: number_of_partitions
character(len=FIELD_NAME_LEN) :: subname
character(len=OPTION_PATH_LEN) :: group_path, subgroup_path
type(vector_field), pointer :: xfield
real :: current_time
integer :: sub_particles
integer :: i, j, k
integer :: dim, particle_groups, total_arrays, list_counter
integer, dimension(:), allocatable :: particle_arrays
integer :: totaldet_global
logical :: from_file, do_output, do_analytical, store_old_fields
integer :: n_fields, n_oldfields
integer :: s_field, v_field, t_field ! field index variables
integer :: s_oldfield, v_oldfield, t_oldfield
integer, dimension(3) :: field_counts, old_field_counts
type(attr_names_type) :: attr_names, old_attr_names, field_names, old_field_names
type(attr_write_type) :: attr_write
type(attr_counts_type) :: attr_counts
type(field_phase_type) :: field_phases, old_field_phases
! field pointers to get their names, for old_field_names
type(scalar_field), pointer :: sfield
type(vector_field), pointer :: vfield
type(tensor_field), pointer :: tfield
character(len=*), dimension(3), parameter :: orders = ["scalar", "vector", "tensor"]
character(len=*), dimension(3), parameter :: types = ["prescribed", "diagnostic", "prognostic"]
ewrite(2,*) "In initialise_particles"
do_output = .true.
if (present(setup_output)) do_output = setup_output
do_analytical = .true.
if (present(ignore_analytical)) do_analytical = .not. ignore_analytical
! Check whether there are any particle groups to initialise
particle_groups = option_count("/particles/particle_group")
if (particle_groups == 0) return
! Set up particle lists
allocate(particle_arrays(particle_groups))
allocate(output_CS(particle_groups))
total_arrays = 0
do i = 1, particle_groups
group_path = "/particles/particle_group["//int2str(i-1)//"]"
call init_output_CS(output_CS(i), trim(group_path) // "/particle_io")
! count subgroups for this group
particle_arrays(i) = option_count(trim(group_path) // "/particle_subgroup")
total_arrays = total_arrays + particle_arrays(i)
end do
allocate(particle_lists(total_arrays))
! Allocate parameters from the coordinate field
xfield => extract_vector_field(state(1), "Coordinate")
call get_option("/geometry/dimension", dim)
call get_option("/timestepping/current_time", current_time)
! calculate the number of fields and old fields that would have to be stored
! (each combination of field order and field type)
field_counts(:) = 0
old_field_counts(:) = 0
do i = 1, 3
do j = 1, 3
field_counts(i) = field_counts(i) + &
option_count("/material_phase/"//orders(i)//"_field/"//types(j)//"/particles/include_in_particles")
old_field_counts(i) = old_field_counts(i) + &
option_count("/material_phase/"//orders(i)//"_field/"//types(j)//"/particles/include_in_particles/store_old_field")
end do
end do
n_fields = sum(field_counts)
n_oldfields = sum(old_field_counts)
! allocate arrays to hold field names
call allocate(field_names, field_counts)
call allocate(old_field_names, old_field_counts)
! allocate arrays to hold field phases
call allocate(field_phases, field_counts)
call allocate(old_field_phases, old_field_counts)
! read the names of the fields if there are any
! this is both for fields that should be included in particles, and that
! should have their old values available to particles too
s_field = 0
v_field = 0
t_field = 0
s_oldfield = 0
v_oldfield = 0
t_oldfield = 0
do i = 1, size(state)
if (field_counts(1) > 0) then
do j = 1, size(state(i)%scalar_names)
sfield => extract_scalar_field(state(i), state(i)%scalar_names(j))
if (sfield%option_path == "" .or. aliased(sfield)) then
cycle
else if (have_option(trim(complete_field_path(sfield%option_path)) // "/particles/include_in_particles")) then
s_field = s_field + 1
field_names%s(s_field) = state(i)%scalar_names(j)
field_phases%s(s_field) = i
if (have_option(trim(complete_field_path(sfield%option_path)) // "/particles/include_in_particles/store_old_field")) then
s_oldfield = s_oldfield + 1
old_field_names%s(s_oldfield) = state(i)%scalar_names(j)
old_field_phases%s(s_oldfield) = i
end if
end if
end do
end if
if (field_counts(2) > 0) then
do j = 1, size(state(i)%vector_names)
vfield => extract_vector_field(state(i), state(i)%vector_names(j))
if (vfield%option_path == "" .or. aliased(vfield)) then
cycle
else if (have_option(trim(complete_field_path(vfield%option_path)) // "/particles/include_in_particles")) then
v_field = v_field + 1
field_names%v(v_field) = state(i)%vector_names(j)
field_phases%v(v_field) = i
if (have_option(trim(complete_field_path(vfield%option_path)) // "/particles/include_in_particles/store_old_field")) then
v_oldfield = v_oldfield + 1
old_field_names%v(v_oldfield) = state(i)%vector_names(j)
old_field_phases%v(v_oldfield) = i
end if
end if
end do
end if
if (field_counts(3) > 0) then
do j = 1, size(state(i)%tensor_names)
tfield => extract_tensor_field(state(i), state(i)%tensor_names(j))
if (tfield%option_path == "" .or. aliased(tfield)) then
cycle
else if (have_option(trim(complete_field_path(tfield%option_path)) // "/particles/include_in_particles")) then
t_field = t_field + 1
field_names%t(t_field) = state(i)%tensor_names(j)
field_phases%t(t_field) = i
if (have_option(trim(complete_field_path(tfield%option_path)) // "/particles/include_in_particles/store_old_field")) then
t_oldfield = t_oldfield + 1
old_field_names%t(t_oldfield) = state(i)%tensor_names(j)
old_field_phases%t(t_oldfield) = i
end if
end if
end do
end if
end do
assert(s_field + v_field + t_field == n_fields)
assert(s_oldfield + v_oldfield + t_oldfield == n_oldfields)
list_counter = 1
do i = 1,particle_groups
group_path = "/particles/particle_group["//int2str(i-1)//"]"
do k = 1, particle_arrays(i)
subgroup_path = trim(group_path) // "/particle_subgroup["//int2str(k-1)//"]"
! If the option "from_file" exists, it means we are
! continuing the simulation after checkpointing and the
! reading of the particle positions must be done from a file
from_file = have_option(trim(subgroup_path) // "/initial_position/from_file")
! But if we're flredecomping, we don't want to handle
! particles with analytically-specified positions (i.e. not
! from a file)
if (.not. do_analytical .and. .not. from_file) cycle
! Set up the particle list structure
call get_option(trim(subgroup_path) // "/name", subname)
! Register this I/O list with a global list of detectors/particles
call register_detector_list(particle_lists(list_counter))
!Set list id
particle_lists(list_counter)%id = list_counter
! Find number of attributes, old attributes, and names of each
call attr_names_and_count(trim(subgroup_path) // "/attributes/scalar_attribute", &
attr_names%s, old_attr_names%s, attr_names%sn, old_attr_names%sn, attr_write%s, &
attr_counts%attrs(1), attr_counts%old_attrs(1))
call attr_names_and_count(trim(subgroup_path) // "/attributes/vector_attribute", &
attr_names%v, old_attr_names%v, attr_names%vn, old_attr_names%vn, attr_write%v, &
attr_counts%attrs(2), attr_counts%old_attrs(2))
call attr_names_and_count(trim(subgroup_path) // "/attributes/tensor_attribute", &
attr_names%t, old_attr_names%t, attr_names%tn, old_attr_names%tn, attr_write%t, &
attr_counts%attrs(3), attr_counts%old_attrs(3))
! save names in the detector list -- this will allocate and assign values
! as expected
particle_lists(list_counter)%attr_names = attr_names
particle_lists(list_counter)%old_attr_names = old_attr_names
particle_lists(list_counter)%attr_write = attr_write
! If any attributes are from fields, we'll need to store old fields too
store_old_fields = .false.
if (option_count(trim(subgroup_path) // "/attributes/scalar_attribute/attribute_value/python_fields") > 0 .or. &
option_count(trim(subgroup_path) // "/attributes/scalar_attribute_array/attribute_value/python_fields") > 0 .or. &
option_count(trim(subgroup_path) // "/attributes/vector_attribute/attribute_value/python_fields") > 0 .or. &
option_count(trim(subgroup_path) // "/attributes/vector_attribute_array/attribute_value/python_fields") > 0 .or. &
option_count(trim(subgroup_path) // "/attributes/tensor_attribute/attribute_value/python_fields") > 0 .or. &
option_count(trim(subgroup_path) // "/attributes/tensor_attribute_array/attribute_value/python_fields") > 0) then
store_old_fields = .true.
end if
if (store_old_fields) then
attr_counts%old_fields(:) = old_field_counts(:)
! only copy old field names if they're required
particle_lists(list_counter)%field_names = field_names
particle_lists(list_counter)%old_field_names = old_field_names
! and the field phases so we can look them up later
particle_lists(list_counter)%field_phases = field_phases
particle_lists(list_counter)%old_field_phases = old_field_phases
else
attr_counts%old_fields(:) = 0
! allocate empty arrays for names and phases
call allocate(particle_lists(list_counter)%field_names, [0, 0, 0])
call allocate(particle_lists(list_counter)%old_field_names, [0, 0, 0])
call allocate(particle_lists(list_counter)%field_phases, [0, 0, 0])
call allocate(particle_lists(list_counter)%old_field_phases, [0, 0, 0])
end if
! assign the total number of list slices for each kind of attribute
! this is used mostly for transferring detectors across processes
particle_lists(list_counter)%total_attributes(1) = &
total_attributes(attr_counts%attrs, dim)
particle_lists(list_counter)%total_attributes(2) = &
total_attributes(attr_counts%old_attrs, dim)
particle_lists(list_counter)%total_attributes(3) = &
total_attributes(attr_counts%old_fields, dim)
! Enable particles to drift with the mesh
if (have_option("/particles/move_with_mesh")) then
particle_lists(list_counter)%move_with_mesh = .true.
end if
if (is_active_process) then
! Read particles from options -- only if this process is currently active (as defined in flredecomp)
if (from_file) then
call get_option(trim(subgroup_path) // "/initial_position/from_file/number_of_particles", sub_particles)
call read_particles_from_file(sub_particles, subname, subgroup_path, &
particle_lists(list_counter), xfield, dim, &
attr_counts, attr_names, old_attr_names, old_field_names, &
number_of_partitions)
else
call read_particles_from_python(subname, subgroup_path, &
particle_lists(list_counter), xfield, dim, &
current_time, state, attr_counts, sub_particles, global=global)
end if
end if
particle_lists(list_counter)%total_num_det = sub_particles
if (do_output) then
! Only set up output if we need to (i.e. actually running,
! not flredecomping)
call set_particle_output_file(subname, filename, particle_lists(list_counter))
end if
! Get options for lagrangian particle movement
call read_detector_move_options(particle_lists(list_counter), "/particles")
! Make sure to deallocate attribute names before moving on
call deallocate(attr_names)
call deallocate(old_attr_names)
list_counter = list_counter + 1
end do
end do
! And finally some sanity checks
list_counter=1
do i = 1,particle_groups
group_path = "/particles/particle_group["//int2str(i-1)//"]"
do k = 1,particle_arrays(i)
subgroup_path = trim(group_path) // "/particle_subgroup["//int2str(k-1)//"]"
call get_option(trim(subgroup_path)//"/name",subname)
totaldet_global=particle_lists(list_counter)%length
call allsum(totaldet_global)
ewrite(2,*) "Found", particle_lists(list_counter)%length, "local and ", totaldet_global, "global particles for particle array ", trim(subname)
assert(totaldet_global==particle_lists(list_counter)%total_num_det)
list_counter = list_counter + 1
end do
end do
deallocate(particle_arrays)
call deallocate(field_names)
call deallocate(old_field_names)
end subroutine initialise_particles
!> Initialise particles for times greater than 0
subroutine initialise_particles_during_simulation(state, current_time, dt)
!> Model state structure
type(state_type), dimension(:), intent(in) :: state
!> Current simulation time
real, intent(in) :: current_time
!> Current model timestep
real, intent(in) :: dt
integer :: i, k, j, dim, id_number
integer :: particle_groups, particle_subgroups, list_counter, sub_particles
integer :: nb_part_created
integer, dimension(:), allocatable :: init_check
type(vector_field), pointer :: xfield
type(attr_counts_type) :: attr_counts
type(attr_names_type) :: attr_names, old_attr_names
type(attr_write_type) :: attr_write
type(detector_type), pointer :: first_newly_init_part
integer, dimension(3) :: field_counts, old_field_counts
character(len=OPTION_PATH_LEN) :: group_path, subgroup_path
character(len=FIELD_NAME_LEN) :: subname
character(len=PYTHON_FUNC_LEN) :: script
character(len=*), dimension(3), parameter :: orders = ["scalar", "vector", "tensor"]
character(len=*), dimension(3), parameter :: types = ["prescribed", "diagnostic", "prognostic"]
logical :: store_old_fields
! Check whether there are any particles.
particle_groups = option_count("/particles/particle_group")
if (particle_groups == 0) return
! calculate the number of fields and old fields that would have to be stored
! (each combination of field order and field type)
old_field_counts(:) = 0
do i = 1, 3
do j = 1, 3
old_field_counts(i) = old_field_counts(i) + &
option_count("/material_phase/"//orders(i)//"_field/"//types(j)//"/particles/include_in_particles/store_old_field")
end do
end do
! Allocate parameters from the coordinate field
xfield => extract_vector_field(state(1), "Coordinate")
call get_option("/geometry/dimension", dim)
list_counter = 1
ewrite(2,*) "In initialise_particles_during_simulation"
!Check if initialise_during_simulation is enabled
do i = 1, particle_groups
group_path = "/particles/particle_group["//int2str(i-1)//"]"
particle_subgroups = option_count(trim(group_path) // "/particle_subgroup")
do k = 1, particle_subgroups
subgroup_path = trim(group_path) // "/particle_subgroup["//int2str(k-1)//"]"
if (have_option(trim(subgroup_path)//"/initialise_during_simulation")) then
! Find number of attributes, old attributes, and names of each
call attr_names_and_count(trim(subgroup_path) // "/attributes/scalar_attribute", &
attr_names%s, old_attr_names%s, attr_names%sn, old_attr_names%sn, attr_write%s, &
attr_counts%attrs(1), attr_counts%old_attrs(1))
call attr_names_and_count(trim(subgroup_path) // "/attributes/vector_attribute", &
attr_names%v, old_attr_names%v, attr_names%vn, old_attr_names%vn, attr_write%v, &
attr_counts%attrs(2), attr_counts%old_attrs(2))
call attr_names_and_count(trim(subgroup_path) // "/attributes/tensor_attribute", &
attr_names%t, old_attr_names%t, attr_names%tn, old_attr_names%tn, attr_write%t, &
attr_counts%attrs(3), attr_counts%old_attrs(3))
! If any attributes are from fields, we'll need to store old fields too
store_old_fields = .false.
if (option_count(trim(subgroup_path) // "/attributes/scalar_attribute/attribute_value/python_fields") > 0 .or. &
option_count(trim(subgroup_path) // "/attributes/scalar_attribute_array/attribute_value/python_fields") > 0 .or. &
option_count(trim(subgroup_path) // "/attributes/vector_attribute/attribute_value/python_fields") > 0 .or. &
option_count(trim(subgroup_path) // "/attributes/vector_attribute_array/attribute_value/python_fields") > 0 .or. &
option_count(trim(subgroup_path) // "/attributes/tensor_attribute/attribute_value/python_fields") > 0 .or. &
option_count(trim(subgroup_path) // "/attributes/tensor_attribute_array/attribute_value/python_fields") > 0) then
store_old_fields = .true.
end if
if (store_old_fields) then
attr_counts%old_fields(:) = old_field_counts(:)
else
attr_counts%old_fields(:) = 0
end if
call get_option(trim(subgroup_path)//"/initialise_during_simulation/python", script)
id_number = particle_lists(list_counter)%proc_part_count
call get_option(trim(subgroup_path) // "/name", subname)
first_newly_init_part => particle_lists(list_counter)%last
call read_particles_from_python(subname, subgroup_path, particle_lists(list_counter), xfield, dim, &
current_time, state, attr_counts, sub_particles, global=.true., &
id_number=id_number, script=script, nb_part_created=nb_part_created)
particle_lists(list_counter)%total_num_det = particle_lists(list_counter)%total_num_det + sub_particles
if (nb_part_created > 0) then
if (.not. associated(first_newly_init_part)) then
first_newly_init_part => particle_lists(list_counter)%first
else
first_newly_init_part => first_newly_init_part%next
end if
call update_particle_subgroup_attributes_and_fields( &
state, current_time, dt, subgroup_path, particle_lists(list_counter), initial=.true., &
first_newly_init_part=first_newly_init_part, nb_part_created=nb_part_created)
end if
end if
list_counter = list_counter + 1
end do
end do
end subroutine initialise_particles_during_simulation
!> Get the names and count of all attributes and old attributes for
!! a given attribute rank for a particle subgroup
subroutine attr_names_and_count(key, names, old_names, dims, old_dims, to_write, count, old_count)
!> Prefix key to an attribute rank within a subgroup
character(len=*), intent(in) :: key
!> Output arrays for attribute names
character(len=*), dimension(:), allocatable, intent(out) :: names, old_names
!> Output arrays for attribute dimensions
integer, dimension(:), allocatable, intent(out) :: dims, old_dims
!> Output arrays for whether to write attributes
logical, dimension(:), allocatable, intent(out) :: to_write
!> Output attribute counts
integer, intent(out) :: count, old_count
integer :: i, old_i, single_count, array_count, single_old_count, array_old_count
character(len=FIELD_NAME_LEN) :: array_key, subkey
! array-valued attribute name
array_key = trim(key) // "_array"
! get option count so we can allocate the names array
single_count = option_count(key)
array_count = option_count(array_key)
single_old_count = option_count(key//"/attribute_value/python_fields/store_old_attribute")
array_old_count = option_count(trim(array_key)//"/attribute_value/python_fields/store_old_attribute")
allocate(names(single_count + array_count))
allocate(old_names(single_old_count + array_old_count))
allocate(to_write(single_count + array_count))
allocate(dims(single_count + array_count))
allocate(old_dims(single_old_count + array_old_count))
! names for single-valued attributes
old_i = 1
do i = 1, single_count
! get the attribute's name
write(subkey, "(a,'[',i0,']')") key, i-1
call get_option(trim(subkey)//"/name", names(i))
! we set single-valued attributes to have a dimension of 0 to distinguish from
! a length 1 array attribute
dims(i) = 0
to_write(i) = .not. have_option(trim(subkey)//"/exclude_from_output")
if (have_option(trim(subkey)//"/attribute_value/python_fields/store_old_attribute")) then
! prefix with "old%" to distinguish from current attribute
old_names(old_i) = "old%" // trim(names(i))
old_dims(old_i) = 0
old_i = old_i + 1
end if
end do
! names for array-valued attributes
do i = 1, array_count
write(subkey, "(a,'[',i0,']')") trim(array_key), i-1
call get_option(trim(subkey)//"/name", names(i+single_count))
call get_option(trim(subkey)//"/dimension", dims(i+single_count))
to_write(i+single_count) = .not. have_option(trim(subkey)//"/exclude_from_output")
if (have_option(trim(subkey)//"/attribute_value/python_fields/store_old_attribute")) then
old_names(old_i) = "old%" // trim(names(i+single_count))
old_dims(old_i) = dims(i+single_count)
old_i = old_i + 1
end if
end do
! compute the number of attribute arrays
count = single_count + sum(dims)
old_count = single_old_count + sum(old_dims)
end subroutine attr_names_and_count
!> Initialise particles which are defined by a Python function
subroutine read_particles_from_python(subgroup_name, subgroup_path, &
p_list, xfield, dim, current_time, state, attr_counts, n_particles, &
global, id_number, script, nb_part_created)
!> Name of the particles' subgroup
character(len=FIELD_NAME_LEN), intent(in) :: subgroup_name
!> Path prefix for the subgroup in options
character(len=OPTION_PATH_LEN), intent(in) :: subgroup_path
!> Detector list to hold the particles
type(detector_linked_list), intent(inout) :: p_list
!> Coordinate vector field
type(vector_field), pointer, intent(in) :: xfield
!> Geometry dimension
integer, intent(in) :: dim
!> Current model time, for passing through to Python functions
real, intent(in) :: current_time
!> Model state structure
type(state_type), dimension(:), intent(in) :: state
!> Counts of attributes, old attributes, and old fields
!! for each scalar, vector and tensor
type(attr_counts_type), intent(in) :: attr_counts
!> Number of particles being initialized
integer, intent(out) :: n_particles
!> Whether to consider this particle in a global element search
logical, intent(in), optional :: global
!> ID number of last particle currently in list
integer, optional, intent(in) :: id_number
!> Python script used by initialise_during_simulation
character(len=PYTHON_FUNC_LEN), optional, intent(in) :: script
!> Number of particles created as part of initialise_during_simulation
integer, optional, intent(out) :: nb_part_created
integer :: i, proc_num, stat, offset
character(len=PYTHON_FUNC_LEN) :: func
real, allocatable, dimension(:,:) :: coords ! all particle coordinates, from python
! if we don't know how many particles we're getting, we need a C pointer
type(c_ptr) :: coord_ptr
! and a fortran pointer
real, pointer, dimension(:,:) :: coord_array_ptr
real :: dt
proc_num = getprocno()
ewrite(2,*) "Reading particles from options"
if (present(script)) then
func=script
nb_part_created = 0
else
call get_option(trim(subgroup_path)//"/initial_position/python", func)
end if
call get_option("/timestepping/timestep", dt)
call set_detectors_from_python(func, len(func), dim, current_time, coord_ptr, n_particles, stat)
call c_f_pointer(coord_ptr, coord_array_ptr, [dim, n_particles])
allocate(coords(dim, n_particles))
if (n_particles==0) return
coords = coord_array_ptr
call deallocate_c_array(coord_ptr)
offset = 0
if (present(id_number)) offset = id_number
do i = 1, n_particles
call create_single_particle(p_list, xfield, coords(:,i), i+offset, proc_num, dim, attr_counts, &
global=global, nb_part_created=nb_part_created)
end do
deallocate(coords)
end subroutine read_particles_from_python
!> Read attributes for all ranks from an H5Part file
subroutine read_attrs(h5_id, dim, counts, names, vals, prefix)
!> h5 file to read from
!! it's assumed this has been set up to read from the right place!
integer(kind=8), intent(in) :: h5_id
!> spatial dimension
integer, intent(in) :: dim
!> counts of scalar/vector/tensor attributes
integer, dimension(3), intent(in) :: counts
!> attribute names to read from the file
type(attr_names_type), intent(in) :: names
!> SVT values to hold the output
type(attr_vals_type), intent(inout) :: vals
!> Optional prefix to attribute names
character(len=*), intent(in), optional :: prefix
integer :: i, j, k, ii, val_i
integer(kind=8) :: h5_ierror
character(len=FIELD_NAME_LEN) :: p
p = ""
if (present(prefix)) p = prefix
val_i = 1
scalar_attr_loop: do i = 1, size(names%s)
if (names%sn(i) == 0) then
! single-valued attribute
h5_ierror = h5pt_readdata_r8(h5_id, &
trim(p)//trim(names%s(i)), vals%s(val_i))
val_i = val_i + 1
else
do ii = 1, names%sn(i)
! inner loop for array-valued attribute
h5_ierror = h5pt_readdata_r8(h5_id, &
trim(p)//trim(names%s(i))//int2str(ii), vals%s(val_i))
val_i = val_i + 1
end do
end if
end do scalar_attr_loop
val_i = 1
vector_attr_loop: do i = 1, size(names%v)
if (names%vn(i) == 0) then
! single-valued attribute
do j = 1, dim
h5_ierror = h5pt_readdata_r8(h5_id, &
trim(p)//trim(names%v(i))//"_"//int2str(j-1), vals%v(j,val_i))
end do
val_i = val_i + 1
else
do ii = 1, names%vn(i)
! inner loop for array-valued attribute
do j = 1, dim
h5_ierror = h5pt_readdata_r8(h5_id, &
trim(p)//trim(names%v(i))//int2str(ii)//"_"//int2str(j-1), vals%v(j,val_i))
end do
val_i = val_i + 1
end do
end if
end do vector_attr_loop
val_i = 1
tensor_attr_loop: do i = 1, size(names%t)
if (names%tn(i) == 0) then
! single-valued attribute
do j = 1, dim
do k = 1, dim
h5_ierror = h5pt_readdata_r8(h5_id, &
trim(p)//trim(names%t(i))//"_"//int2str((k-1)*dim + (j-1)), &
vals%t(j,k,val_i))
end do
end do
val_i = val_i + 1
else
do ii = 1, names%tn(i)
! inner loop for array-valued attribute
do j = 1, dim
do k = 1, dim
h5_ierror = h5pt_readdata_r8(h5_id, &
trim(p)//trim(names%t(i))//int2str(ii)//"_"//int2str((k-1)*dim + (j-1)), &
vals%t(j,k,val_i))
end do
end do
val_i = val_i + 1
end do
end if
end do tensor_attr_loop
end subroutine read_attrs
!> Read particles in the given subgroup from a checkpoint file
subroutine read_particles_from_file(n_particles, subgroup_name, subgroup_path, &
p_list, xfield, dim, &
attr_counts, attr_names, old_attr_names, old_field_names, &
n_partitions)
!> Number of particles in this subgroup
integer, intent(in) :: n_particles
!> Name of the particles' subgroup
character(len=FIELD_NAME_LEN), intent(in) :: subgroup_name
!> Path prefix for the subgroup in options
character(len=OPTION_PATH_LEN), intent(in) :: subgroup_path
!> Detector list to hold the particles
type(detector_linked_list), intent(inout) :: p_list
!> Coordinate vector field
type(vector_field), pointer, intent(in) :: xfield
!> Geometry dimension
integer, intent(in) :: dim
!> Counts of attributes, old attributes, and old fields
!! for each scalar, vector and tensor
type(attr_counts_type), intent(in) :: attr_counts
!> Names of attributes to store on this subgroup
type(attr_names_type), intent(in) :: attr_names
!> The attributes for which an old value should be checkpointed
type(attr_names_type), intent(in) :: old_attr_names
!> Names of fields for which old values should be checkpointed
type(attr_names_type), intent(in) :: old_field_names
!> Optional parameter during recomposition to control the
!! processes which are involved in reading from file
integer, intent(in), optional :: n_partitions
integer :: i
integer :: ierr, commsize, rank, id(1), proc_id(1)
integer :: input_comm, world_group, input_group ! opaque MPI pointers
real, allocatable, dimension(:) :: positions ! particle coordinates
character(len=OPTION_PATH_LEN) :: particles_cp_filename
integer(kind=8) :: h5_ierror, h5_id, h5_prop, view_start, view_end ! h5hut state
integer(kind=8), dimension(:), allocatable :: npoints, part_counter ! number of points for each rank to read
type(attr_vals_type), pointer :: attr_vals, old_attr_vals, old_field_vals ! scalar/vector/tensor arrays
ewrite(2,*) "Reading particles from file"
! create new mpi group for active particles only
! non-active processes are already not in this routine,
! so we don't have to worry about them
if (present(n_partitions)) then
call mpi_comm_group(MPI_COMM_FEMTOOLS, world_group, ierr)
call mpi_group_incl(world_group, n_partitions, [(i, i=0, n_partitions-1)], input_group, ierr)
call mpi_comm_create_group(MPI_COMM_FEMTOOLS, input_group, 0, input_comm, ierr)
else
input_comm = MPI_COMM_FEMTOOLS
end if
! allocate arrays to hold positions and attributes for a single particle
allocate(positions(dim))
call allocate(attr_vals, dim, attr_counts%attrs)
call allocate(old_attr_vals, dim, attr_counts%old_attrs)
call allocate(old_field_vals, dim, attr_counts%old_fields)
! set up the checkpoint file for reading
call get_option(trim(subgroup_path) // "/initial_position/from_file/file_name", particles_cp_filename)
h5_prop = h5_createprop_file()
! because we're reading separate particle counts per core
! we can't use collective IO
h5_ierror = h5_setprop_file_mpio_independent(h5_prop, input_comm)
assert(h5_ierror == H5_SUCCESS)
h5_id = h5_openfile(trim(particles_cp_filename), H5_O_RDONLY, h5_prop)
h5_ierror = h5_closeprop(h5_prop)
h5_ierror = h5_setstep(h5_id, int(1, 8))
! determine the number of particles we are to initiliase
call mpi_comm_size(MPI_COMM_FEMTOOLS, commsize, ierr)
call mpi_comm_rank(MPI_COMM_FEMTOOLS, rank, ierr)
allocate(npoints(commsize))
allocate(part_counter(commsize))
h5_ierror = h5_readfileattrib_i8(h5_id, "npoints", npoints)
h5_ierror = h5_readfileattrib_i8(h5_id, "part_counter", part_counter)
h5_ierror = h5pt_setnpoints(h5_id, npoints(rank+1))
! figure out our local offset into the file
h5_ierror = h5pt_getview(h5_id, view_start, view_end)
do i = 1, npoints(rank+1)
! set view to read this particle
h5_ierror = h5pt_setview(h5_id, int(view_start + i - 1, 8), int(view_start + i - 1, 8))
if (dim >= 1) &
h5_ierror = h5pt_readdata_r8(h5_id, "x", positions(1))
if (dim >= 2) &
h5_ierror = h5pt_readdata_r8(h5_id, "y", positions(2))
if (dim >= 3) &
h5_ierror = h5pt_readdata_r8(h5_id, "z", positions(3))
h5_ierror = h5pt_readdata_i4(h5_id, "id", id(1))
h5_ierror = h5pt_readdata_i4(h5_id, "proc_id", proc_id(1))
! batched reads of scalar, vector, tensor values of each kind of attribute
call read_attrs(h5_id, dim, attr_counts%attrs, attr_names, attr_vals)
call read_attrs(h5_id, dim, attr_counts%old_attrs, old_attr_names, old_attr_vals)
call read_attrs(h5_id, dim, attr_counts%old_fields, old_field_names, old_field_vals, prefix="old%")
! don't use a global check for this particle
call create_single_particle(p_list, xfield, &
positions, id(1), proc_id(1), dim, &
attr_counts, attr_vals, old_attr_vals, old_field_vals, global=.false.)
end do
! reset proc_particle_count
p_list%proc_part_count = part_counter(rank+1)
h5_ierror = h5_closefile(h5_id)
deallocate(positions)
deallocate(npoints)
deallocate(part_counter)
deallocate(attr_vals)
deallocate(old_attr_vals)
deallocate(old_field_vals)
if (present(n_partitions)) then
call mpi_comm_free(input_comm, ierr)
call mpi_group_free(input_group, ierr)
end if
end subroutine read_particles_from_file
subroutine set_particle_output_file(subname, filename, p_list)
!! Set up the particle output file for a single subgroup
type(detector_linked_list), intent(inout) :: p_list
character(len=*), intent(in) :: filename
character(len=FIELD_NAME_LEN), intent(in) :: subname
p_list%h5_id = h5_openfile(trim(filename) // '.particles.' // trim(subname) // '.h5part', H5_O_WRONLY, H5_PROP_DEFAULT)
! optionally set any file attributes here?
end subroutine set_particle_output_file
!> Allocate a single particle, populate and insert it into the given list
!! In parallel, first check if the particle would be local and only allocate if it is
subroutine create_single_particle(detector_list, xfield, position, id, proc_id, dim, &
attr_counts, attr_vals, old_attr_vals, old_field_vals, global, nb_part_created)
!> The detector list to hold the particle
type(detector_linked_list), intent(inout) :: detector_list
!> Coordinate vector field
type(vector_field), pointer, intent(in) :: xfield
!> Spatial position of the particle
real, dimension(xfield%dim), intent(in) :: position
!> Unique ID number for this particle
integer, intent(in) :: id
!> Procces ID on which this particle was created
integer, intent(in) :: proc_id
!> Geometry dimension
integer, intent(in) :: dim
!> Counts of scalar, vector and tensor attributes, old attributes
!! and old fields to store on the particle
type(attr_counts_type), intent(in) :: attr_counts
!> If provided, initialise the particle's attributes directly
type(attr_vals_type), intent(in), optional :: attr_vals, old_attr_vals, old_field_vals
!> Whether to create this particle in a collective operation (true)
!! or for the local processor only (false).
!! This affects the inquiry of the element owning the particle
logical, intent(in), optional :: global
!> Number of particles created as part of initialise_during_simulation
integer, intent(inout), optional :: nb_part_created
type(detector_type), pointer :: detector
type(element_type), pointer :: shape
real, dimension(xfield%dim+1) :: lcoords
integer :: element
real :: dt
logical :: picker_global = .true.
if (present(global)) picker_global = global
shape => ele_shape(xfield,1)
assert(xfield%dim+1==local_coord_count(shape))
! Determine element and local_coords from position
call picker_inquire(xfield, position, element, local_coord=lcoords, global=picker_global)
call get_option("/timestepping/timestep", dt)
! If we're in parallel and don't own the element, skip this particle
if (isparallel()) then
if (element<0) return
if (.not.element_owned(xfield,element)) return
else
! In serial make sure the particle is in the domain
! unless we have the write_nan_outside override
if (element<0 .and. .not.detector_list%write_nan_outside) then
ewrite(-1,*) "Dealing with particle ", id, " proc_id:", proc_id
FLExit("Trying to initialise particle outside of computational domain")
end if
end if
! Otherwise, allocate and insert particle
allocate(detector)
allocate(detector%position(xfield%dim))
allocate(detector%local_coords(local_coord_count(shape)))
call insert(detector, detector_list)
if (present(nb_part_created)) then
nb_part_created = nb_part_created + 1
end if
! Populate particle
detector%position = position
detector%element = element
detector%local_coords = lcoords
detector%id_number = id
detector%proc_id = proc_id
detector%list_id = detector_list%id
detector_list%proc_part_count = max(detector_list%proc_part_count,id)
! allocate space to store all attributes on the particle
allocate(detector%attributes(total_attributes(attr_counts%attrs, dim)))
allocate(detector%old_attributes(total_attributes(attr_counts%old_attrs, dim)))
allocate(detector%old_fields(total_attributes(attr_counts%old_fields, dim)))
! copy attributes if they're present, otherwise initialise to zero