Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
probes.F90
Go to the documentation of this file.
1! Copyright (c) 2020-2026, The Neko Authors
2! All rights reserved.
3!
4! Redistribution and use in source and binary forms, with or without
5! modification, are permitted provided that the following conditions
6! are met:
7!
8! * Redistributions of source code must retain the above copyright
9! notice, this list of conditions and the following disclaimer.
10!
11! * Redistributions in binary form must reproduce the above
12! copyright notice, this list of conditions and the following
13! disclaimer in the documentation and/or other materials provided
14! with the distribution.
15!
16! * Neither the name of the authors nor the names of its
17! contributors may be used to endorse or promote products derived
18! from this software without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31! POSSIBILITY OF SUCH DAMAGE.
32!
33!> Implements probes.
34!! @note This modules uses functions from `gslib`, namely `findpts_setup`,
35!! `findpts`, and `findpts_eval`. A full description of these subroutines can
36!! be found at https://github.com/Nek5000/gslib/blob/master/src/findpts.c
37module probes
38 use num_types, only : rp, dp
39 use matrix, only : matrix_t
40 use host_array, only : host_array_t
43 use field_list, only : field_list_t
44 use time_state, only : time_state_t
46 use registry, only : neko_registry
47 use dofmap, only : dofmap_t
48 use json_module, only : json_file, json_value, json_core
53 use tensor, only : trsp
54 use point_zone, only : point_zone_t
56 use file, only : file_t, file_free
57 use csv_file, only : csv_file_t
59 use math, only : copy
60 use device_math, only : device_copy
61 use case, only : case_t
62 use, intrinsic :: iso_c_binding
66 use mpi_f08, only : mpi_allreduce, mpi_integer, mpi_sum, &
67 mpi_double_precision, mpi_gatherv, mpi_gather, mpi_exscan
68 implicit none
69 private
70
71 type, public, extends(simulation_component_t) :: probes_t
72 !> Time after which to start collecting probes.
73 real(kind=rp) :: start_time
74 !> Number of output fields
75 integer :: n_fields = 0
76 type(global_interpolation_t) :: global_interp
77 !> Global number of probes (needed for i/o)
78 integer :: n_global_probes
79 !> global offset for writing
80 integer :: n_probes_offset
81 !> x,y,z coordinates
82 real(kind=rp), allocatable :: xyz(:,:)
83 !> Interpolated values
84 real(kind=rp), allocatable :: out_values(:,:)
85 type(c_ptr), allocatable :: out_values_d(:)
86 real(kind=rp), allocatable :: out_vals_trsp(:,:)
87 !> Number of local elements per rank
88 integer :: n_local_probes
89 !> Fields to be probed
90 type(field_list_t) :: sampled_fields
91 character(len=NEKO_VARNAME_LEN), allocatable :: which_fields(:)
92 !> Allocated on rank 0
93 integer, allocatable :: n_local_probes_tot_offset(:)
94 integer, allocatable :: n_local_probes_tot(:)
95 !> For output on rank 0
96 logical :: seq_io
97 real(kind=rp), allocatable :: global_output_values(:,:)
98 !> Output variables
99 type(file_t) :: fout
100 type(matrix_t) :: mat_out
101 !> Fixed distributions for field values and the root-owned timestamp.
102 type(hdf5_layout_t) :: probe_layout, time_layout
103 !> Persistent CPU-only timestamp buffer.
104 type(host_array_t) :: time_out
105 logical :: append_out = .true.
106 !> Whether this component owns an open HDF5 output file.
107 logical :: output_file_open = .false.
108 !> Number of samples in the current HDF5 file, for either layout.
109 integer :: n_samples = 0
110 contains
111 !> Initialize from json.
112 procedure, pass(this) :: init => probes_init_from_json
113 !> Initialize from parameters.
114 procedure, pass(this) :: init_from_components => &
116 !> Common constructor.
117 procedure, private, pass(this) :: init_common => probes_init_common
118 !> Destructor
119 procedure, pass(this) :: free => probes_free
120 !> Setup offset for I/O when using sequential write/read from rank 0
121 procedure, pass(this) :: setup_offset => probes_setup_offset
122 !> Interpolate each probe from its `r,s,t` coordinates.
123 procedure, pass(this) :: compute_ => probes_evaluate_and_write
124
125 ! ----------------------------------------------------------------------- !
126 ! Private methods
127
128 !> Reader for file type points
129 procedure, private, pass(this) :: read_file
130 !> Reader for point type points
131 procedure, private, pass(this) :: read_point
132 !> Reader for line type points
133 procedure, private, pass(this) :: read_line
134 !> Reader for circle type points
135 procedure, private, pass(this) :: read_circle
136 !> Reader for point zone type points
137 procedure, private, pass(this) :: read_point_zone
138
139 !> Append a new list of points to the exsiting list.
140 procedure, private, pass(this) :: add_points
141 !> Open a new numbered HDF5 file and write its probe coordinates.
142 procedure, private, pass(this) :: open_output => probes_open_output
143 end type probes_t
144
145contains
146
147 !> Constructor from json.
148 subroutine probes_init_from_json(this, json, case)
149 class(probes_t), intent(inout), target :: this
150 type(json_file), intent(inout) :: json
151 class(case_t), intent(inout), target :: case
152 character(len=:), allocatable :: output_file
153 character(len=:), allocatable :: input_file
154 integer :: i, ierr
155
156 ! JSON variables
157 character(len=:), allocatable :: point_type
158 type(json_value), pointer :: json_point_list
159 type(json_file) :: json_point
160 type(json_core) :: core
161 integer :: idx, n_point_children
162
163 character(len=:), allocatable :: name
164
165 ! Initialize the base class
166 call this%free()
167
168 call json_get_or_default(json, "name", name, "probes")
169 call this%init_base(json, case)
170
171 ! The base defaults the output schedule to the compute schedule.
172 ! Matching schedules keep all samples in one file for this run.
173 if (this%output_controller%control_mode == &
174 this%compute_controller%control_mode .and. &
175 this%output_controller%control_value == &
176 this%compute_controller%control_value) then
177 this%output_controller%never = .true.
178 this%output_controller%control_mode = 'never'
179 call json%add('output_control', 'never')
180 end if
181
182 !> Read from case file
183 call json%info('fields', n_children = this%n_fields)
184 call json_get(json, 'fields', this%which_fields)
185 call json_get(json, 'output_file', output_file)
186 call json_get_or_lookup_or_default(json, 'start_time', this%start_time, &
187 -1.0_rp)
188 call json_get_or_default(json, 'append_output', this%append_out, .true.)
189
190 call this%sampled_fields%init(this%n_fields)
191 do i = 1, this%n_fields
192
193 call this%sampled_fields%assign(i, &
194 & neko_registry%get_field(trim(this%which_fields(i))))
195 end do
196
197 ! Setup the required arrays and initialize variables.
198 this%n_local_probes = 0
199 this%n_global_probes = 0
200
201 ! Read the legacy point specification from the points file.
202 if (json%valid_path('points_file')) then
203
204 ! Todo: We should add a deprecation notice here
205 call json_get(json, 'points_file', input_file)
206
207 ! This is distributed as to make it similar to parallel file
208 ! formats later, Reads all into rank 0
209 call read_probe_locations(this, this%xyz, this%n_local_probes, &
210 this%n_global_probes, input_file)
211 end if
212
213 if (json%valid_path('points')) then
214
215 ! Go through the points list and construct the probe list
216 call json%get('points', json_point_list)
217 call json%info('points', n_children = n_point_children)
218
219 do idx = 1, n_point_children
220 call json_extract_item(core, json_point_list, idx, json_point)
221
222 call json_get_or_default(json_point, 'type', point_type, 'none')
223 select case (point_type)
224
225 case ('file')
226 call this%read_file(json_point)
227 case ('points')
228 call this%read_point(json_point)
229 case ('line')
230 call this%read_line(json_point)
231 case ('plane')
232 call neko_error('Plane probes not implemented yet.')
233 case ('circle')
234 call this%read_circle(json_point)
235 case ('point_zone')
236 call this%read_point_zone(json_point, case%fluid%dm_Xh)
237 case ('none')
238 call json_point%print()
239 call neko_error('No point type specified.')
240 case default
241 call neko_error('Unknown region type ' // point_type)
242 end select
243 end do
244
245 end if
246
247 call mpi_allreduce(this%n_local_probes, this%n_global_probes, 1, &
248 mpi_integer, mpi_sum, neko_comm, ierr)
249
250 call probes_show(this)
251
252 ! Get interpolation parameters from json
253 block
254 type(json_file) :: interp_subdict
255 call json_get_subdict_or_empty(json, "interpolation", interp_subdict)
256 call this%global_interp%init(case%fluid%dm_Xh, &
257 params_subdict = interp_subdict)
258
259 call this%init_common(case%fluid%dm_Xh, output_file, name)
260
261 end block
262
263 end subroutine probes_init_from_json
264
265 !> Initialize based on individual parameters.
266 !! @param dof Dofmap to probe
267 !! @param output_file Name of output file, current must be CSV
268 !! @param name Name of the probes simcomp.
269 !! @param tolerance Tolerance for finding the probe coordinates.
270 !! @param padding Padding for finding the probe coordinates.
271 subroutine probes_init_from_components(this, dof, output_file, name, &
272 tolerance, padding)
273 class(probes_t), intent(inout) :: this
274 type(dofmap_t), intent(in) :: dof
275 character(len=:), allocatable, intent(inout) :: output_file
276 character(len=*), intent(in) :: name
277 real(kind=dp), intent(in), optional :: tolerance, padding
278
279 character(len=1024) :: header_line
280 real(kind=rp), allocatable :: global_output_coords(:,:)
281 integer :: i, ierr
282 type(matrix_t) :: mat_coords
283
284 call this%global_interp%init(dof, tol = tolerance, pad = padding)
285
286 call this%init_common(dof, output_file, name)
287
288 end subroutine probes_init_from_components
289
290 ! ========================================================================== !
291 ! Readers for different point types
292
293 !> Read a list of points from a csv file.
294 !! @note The points are expected to be in the form of a list of coordinates.
295 !! @param[inout] this The probes object.
296 !! @param[inout] json The json file object.
297 subroutine read_file(this, json)
298 class(probes_t), intent(inout) :: this
299 type(json_file), intent(inout) :: json
300 character(len=:), allocatable :: input_file
301 real(kind=rp), dimension(:,:), allocatable :: point_list
302
303 integer :: n_local, n_global
304
305 if (pe_rank .ne. 0) return
306
307 call json_get(json, 'file_name', input_file)
308
309 call read_probe_locations(this, point_list, n_local, n_global, input_file)
310
311 call this%add_points(point_list)
312 end subroutine read_file
313
314 !> Read a list of points from the json file.
315 !! @note The points are expected to be in the form of a list of coordinates.
316 !! @param[inout] this The probes object.
317 !! @param[inout] json The json file object.
318 subroutine read_point(this, json)
319 class(probes_t), intent(inout) :: this
320 type(json_file), intent(inout) :: json
321
322 real(kind=rp), dimension(:,:), allocatable :: point_list
323 real(kind=rp), dimension(:), allocatable :: rp_list_reader
324
325 ! Ensure only rank 0 reads the coordinates.
326 if (pe_rank .ne. 0) return
327 call json_get_or_lookup(json, 'coordinates', rp_list_reader)
328
329 if (mod(size(rp_list_reader), 3) /= 0) then
330 call neko_error('Invalid number of coordinates.')
331 end if
332
333 ! Allocate list of points and reshape the input array
334 allocate(point_list(3, size(rp_list_reader)/3))
335 point_list = reshape(rp_list_reader, [3, size(rp_list_reader)/3])
336
337 call this%add_points(point_list)
338 end subroutine read_point
339
340 !> Construct a list of points from a line.
341 !! @param[inout] this The probes object.
342 !! @param[inout] json The json file object.
343 subroutine read_line(this, json)
344 class(probes_t), intent(inout) :: this
345 type(json_file), intent(inout) :: json
346
347 real(kind=rp), dimension(:,:), allocatable :: point_list
348 real(kind=rp), dimension(:), allocatable :: start, end
349 real(kind=rp), dimension(3) :: direction
350 real(kind=rp) :: t
351
352 integer :: n_points, i
353
354 ! Ensure only rank 0 reads the coordinates.
355 if (pe_rank .ne. 0) return
356 call json_get_or_lookup(json, "start", start)
357 call json_get_or_lookup(json, "end", end)
358 call json_get_or_lookup(json, "amount", n_points)
359
360 ! If either start or end is not of length 3, error out
361 if (size(start) /= 3 .or. size(end) /= 3) then
362 call neko_error('Invalid start or end coordinates.')
363 end if
364
365 ! Calculate the number of points
366 allocate(point_list(3, n_points))
367
368 ! Calculate the direction vector
369 direction = end - start
370 do i = 1, n_points
371 t = real(i - 1, kind = rp) / real(n_points - 1, kind = rp)
372 point_list(:, i) = start + direction * t
373 end do
374
375 call this%add_points(point_list)
376 end subroutine read_line
377
378 !> Construct a list of points from a circle.
379 !! @details The general structure of the circle is defined by a center point,
380 !! a radius and a normal to the plane it lies on. The circle is then
381 !! discretized into a number of points, based on the `amount` parameter.
382 !! The points are added clockwise starting from a chosen axis, which is
383 !! defined by the `axis` parameter.
384 !! @note The axis must be one of the following: `x`, `y`, or `z`.
385 !! @param[inout] this The probes object.
386 !! @param[inout] json The json file object.
387 subroutine read_circle(this, json)
388 class(probes_t), intent(inout) :: this
389 type(json_file), intent(inout) :: json
390
391 real(kind=rp), dimension(:,:), allocatable :: point_list
392 real(kind=rp), dimension(:), allocatable :: center, normal
393 real(kind=rp) :: radius
394 real(kind=rp) :: angle
395 integer :: n_points, i
396 character(len=:), allocatable :: axis
397
398 real(kind=rp), dimension(3) :: zero_line, cross_line, temp
399 real(kind=rp) :: pi
400
401 ! Ensure only rank 0 reads the coordinates.
402 if (pe_rank .ne. 0) return
403 call json_get_or_lookup(json, "center", center)
404 call json_get_or_lookup(json, "normal", normal)
405 call json_get_or_lookup(json, "radius", radius)
406 call json_get_or_lookup(json, "amount", n_points)
407 call json_get(json, "axis", axis)
408
409 ! If either center or normal is not of length 3, error out
410 if (size(center) /= 3 .or. size(normal) /= 3) then
411 call neko_error('Invalid center or normal coordinates.')
412 end if
413 if (axis /= 'x' .and. axis /= 'y' .and. axis /= 'z') then
414 call neko_error('Invalid axis.')
415 end if
416 if (radius <= 0) then
417 call neko_error('Invalid radius.')
418 end if
419 if (n_points <= 0) then
420 call neko_error('Invalid number of points.')
421 end if
422
423 ! Normalize the normal vector
424 normal = normal / norm2(normal)
425
426 ! Set the zero line
427 if (axis .eq. 'x') zero_line = [1.0, 0.0, 0.0]
428 if (axis .eq. 'y') zero_line = [0.0, 1.0, 0.0]
429 if (axis .eq. 'z') zero_line = [0.0, 0.0, 1.0]
430
431 if (1.0_rp - dot_product(zero_line, normal) .le. 1e-6) then
432 call neko_error('Invalid axis and normal.')
433 end if
434
435 zero_line = zero_line - dot_product(zero_line, normal) * normal
436 zero_line = zero_line / norm2(zero_line)
437
438 cross_line(1) = normal(2) * zero_line(3) - normal(3) * zero_line(2)
439 cross_line(2) = normal(3) * zero_line(1) - normal(1) * zero_line(3)
440 cross_line(3) = normal(1) * zero_line(2) - normal(2) * zero_line(1)
441
442 ! Calculate the number of points
443 allocate(point_list(3, n_points))
444
445 pi = 4.0_rp * atan(1.0_rp)
446
447 ! Calculate the points
448 do i = 1, n_points
449 angle = 2.0_rp * pi * real(i - 1, kind = rp) / real(n_points, kind = rp)
450 temp = cos(angle) * zero_line + sin(angle) * cross_line
451
452 point_list(:, i) = center + radius * temp
453 end do
454
455 call this%add_points(point_list)
456 end subroutine read_circle
457
458 !> Construct a list of points from a point zone.
459 !! @details The GLL points are read from the point zone and added to the
460 !! probe list.
461 !! @param[inout] this The probes object.
462 !! @param[inout] json The json file object.
463 subroutine read_point_zone(this, json, dof)
464 class(probes_t), intent(inout) :: this
465 type(json_file), intent(inout) :: json
466 type(dofmap_t), intent(in) :: dof
467
468 real(kind=rp), dimension(:,:), allocatable :: point_list
469 character(len=:), allocatable :: point_zone_name
470 class(point_zone_t), pointer :: zone
471 integer :: i, idx, lx, nlindex(4)
472 real(kind=rp) :: x, y, z
473
474 ! Ensure only rank 0 reads the coordinates.
475 if (pe_rank .ne. 0) return
476
477 call json_get(json, "name", point_zone_name)
478 zone => neko_point_zone_registry%get_point_zone(point_zone_name)
479
480 ! Allocate list of points and reshape the input array
481 allocate(point_list(3, zone%size))
482
483 lx = dof%Xh%lx
484 do i = 1, zone%size
485 idx = zone%mask%get(i)
486
487 nlindex = nonlinear_index(idx, lx, lx, lx)
488 x = dof%x%x(nlindex(1), nlindex(2), nlindex(3), nlindex(4))
489 y = dof%y%x(nlindex(1), nlindex(2), nlindex(3), nlindex(4))
490 z = dof%z%x(nlindex(1), nlindex(2), nlindex(3), nlindex(4))
491
492 point_list(:, i) = [x, y, z]
493 end do
494
495 call this%add_points(point_list)
496 end subroutine read_point_zone
497
498 ! ========================================================================== !
499 ! Supporting routines
500
501 !> Append a new list of points to the exsiting list.
502 !! @param[inout] this The probes object.
503 !! @param[in] new_points The new points to be appended.
504 subroutine add_points(this, new_points)
505 class(probes_t), intent(inout) :: this
506 real(kind=rp), dimension(:,:), intent(in) :: new_points
507
508 real(kind=rp), dimension(:,:), allocatable :: temp
509 integer :: n_old, n_new
510
511 ! Get the current number of points
512 n_old = this%n_local_probes
513 n_new = size(new_points, 2)
514
515 ! Move current points to a temporary array
516 if (allocated(this%xyz)) then
517 call move_alloc(this%xyz, temp)
518 end if
519
520 ! Allocate the new array and copy the full list of points
521 allocate(this%xyz(3, n_old + n_new))
522 if (allocated(temp)) then
523 this%xyz(:, 1:n_old) = temp
524 end if
525 this%xyz(:, n_old+1:n_old+n_new) = new_points
526
527 this%n_local_probes = this%n_local_probes + n_new
528 end subroutine add_points
529
530 ! ========================================================================== !
531 ! General initialization routine
532
533 !> Common constructor.
534 !! @param dof Dofmap to probe
535 !! @param output_file Name of output file, current must be CSV
536 !! @param name Name of the probes simcomp.
537 subroutine probes_init_common(this, dof, output_file, name)
538 class(probes_t), intent(inout) :: this
539 type(dofmap_t), intent(in) :: dof
540 character(len=:), allocatable, intent(inout) :: output_file
541 character(len=*), intent(in) :: name
542
543 character(len=1024) :: header_line
544 real(kind=rp), allocatable :: global_output_coords(:,:)
545 integer :: i, ierr
546 type(matrix_t) :: mat_coords
547
548 this%name = name
549
550 !> find probes and redistribute them
551 call this%global_interp%find_points_and_redist(this%xyz, &
552 this%n_local_probes)
553
554 !> Allocate output array
555 allocate(this%out_values(this%n_local_probes, this%n_fields))
556 allocate(this%out_values_d(this%n_fields))
557 allocate(this%out_vals_trsp(this%n_fields, this%n_local_probes))
558
559 if (neko_bcknd_device .eq. 1) then
560 do i = 1, this%n_fields
561 this%out_values_d(i) = c_null_ptr
562 call device_map(this%out_values(:,i), this%out_values_d(i), &
563 this%n_local_probes)
564 end do
565 end if
566
567 !> Initialize the output file
568 call this%fout%init(this%case%output_directory // trim(output_file))
569
570 select type (ft => this%fout%file_type)
571 type is (csv_file_t)
572
573 this%seq_io = .true.
574
575 ! Build the header
576 write(header_line, '(I0, A, I0)') this%n_global_probes, achar(44), &
577 this%n_fields
578 do i = 1, this%n_fields
579 header_line = trim(header_line) // achar(44) // &
580 trim(this%which_fields(i))
581 end do
582 call this%fout%set_header(header_line)
583
584 !> Necessary for not-parallel csv format...
585 !! offsets and n points per pe
586 !! Needed at root for sequential csv i/o
587 allocate(this%n_local_probes_tot(pe_size))
588 allocate(this%n_local_probes_tot_offset(pe_size))
589 call this%setup_offset()
590 if (pe_rank .eq. 0) then
591 allocate(global_output_coords(3, this%n_global_probes))
592 call this%mat_out%init(this%n_global_probes, this%n_fields)
593 allocate(this%global_output_values(this%n_fields, &
594 this%n_global_probes))
595 call mat_coords%init(this%n_global_probes,3)
596 end if
597 call mpi_gatherv(this%xyz, 3*this%n_local_probes, &
598 mpi_real_precision, global_output_coords, &
599 3*this%n_local_probes_tot, &
600 3*this%n_local_probes_tot_offset, &
601 mpi_real_precision, 0, neko_comm, ierr)
602 if (pe_rank .eq. 0) then
603 call trsp(mat_coords%x, this%n_global_probes, &
604 global_output_coords, 3)
605 !! Write the data to the file
606 call this%fout%write(mat_coords)
607 call mat_coords%free()
608 end if
609 class is (hdf5_file_t)
610 ! Probe ownership is fixed after redistribution. Reuse its layout
611 ! for coordinates and every field instead of repeating collectives.
612 call this%probe_layout%init(this%n_local_probes)
613
614 !> Set up the timestamp buffer for appended output
615 this%seq_io = .false.
616 if (this%append_out) then
617 ! Only rank zero owns the timestamp, but all ranks join the write.
618 if (pe_rank == 0) then
619 call this%time_out%init(1)
620 else
621 call this%time_out%init(0)
622 end if
623 call this%time_layout%init(this%time_out%size())
624 end if
625 call this%open_output()
626
627 class default
628 call neko_error("Invalid data. Expected csv_file_t or hdf5_file_t.")
629 end select
630
631 end subroutine probes_init_common
632
633 !> Open a new numbered file and initialize its probe metadata.
634 subroutine probes_open_output(this)
635 class(probes_t), intent(inout) :: this
636 type(matrix_t) :: mat_coords
637
638 select type (ft => this%fout%file_type)
639 type is (hdf5_file_t)
640 call ft%set_overwrite(.not. this%append_out)
641 call ft%open_new()
642 call ft%set_active_group("probes")
643 this%output_file_open = .true.
644
645 this%n_samples = 0
646 call mat_coords%init(3, this%n_local_probes, "coordinates")
647 call copy(mat_coords%x, this%xyz, 3*this%n_local_probes)
648 call ft%write_dataset(mat_coords, layout = this%probe_layout)
649 call ft%write_attribute("NProbes", this%n_global_probes)
650 call ft%write_attribute("NSteps", this%n_samples)
651 call mat_coords%free()
652 end select
653 end subroutine probes_open_output
654
655 !> Destructor
656 subroutine probes_free(this)
657 class(probes_t), intent(inout) :: this
658 integer :: i
659
660 ! Constructors also call free before opening a file. Only close a file
661 ! retained by this component, and reset ownership for repeated free calls.
662 if (this%output_file_open) then
663 select type (ft => this%fout%file_type)
664 class is (hdf5_file_t)
665 call ft%close()
666 end select
667 this%output_file_open = .false.
668 end if
669 this%n_samples = 0
670 call this%fout%free()
671
672 if (allocated(this%xyz)) then
673 deallocate(this%xyz)
674 end if
675
676 if (allocated(this%out_vals_trsp)) then
677 deallocate(this%out_vals_trsp)
678 end if
679
680 call this%sampled_fields%free()
681
682 if (allocated(this%n_local_probes_tot)) then
683 deallocate(this%n_local_probes_tot)
684 end if
685
686 if (allocated(this%n_local_probes_tot_offset)) then
687 deallocate(this%n_local_probes_tot_offset)
688 end if
689
690 if (allocated(this%global_output_values)) then
691 deallocate(this%global_output_values)
692 end if
693
694 if (allocated(this%which_fields)) then
695 deallocate(this%which_fields)
696 end if
697
698 if (allocated(this%out_values)) then
699 if (neko_bcknd_device .eq. 1) then
700 do i = 1, this%n_fields
701 call device_unmap(this%out_values(:,i), this%out_values_d(i))
702 end do
703 end if
704 if (allocated(this%out_values_d)) deallocate(this%out_values_d)
705 deallocate(this%out_values)
706 end if
707
708 call this%global_interp%free()
709 call this%mat_out%free()
710 call this%time_out%free()
711
712 end subroutine probes_free
713
714 !> Print current probe status, with number of probes and coordinates
715 subroutine probes_show(this)
716 class(probes_t), intent(in) :: this
717 character(len=LOG_SIZE) :: log_buf ! For logging status
718 integer :: i
719
720 ! Probes summary
721 call neko_log%section('Probes')
722 write(log_buf, '(A,I6)') "Number of probes: ", this%n_global_probes
723 call neko_log%message(log_buf)
724
725 call neko_log%message("xyz-coordinates:", lvl = neko_log_debug)
726 do i = 1, this%n_local_probes
727 write(log_buf, '("(",F10.6,",",F10.6,",",F10.6,")")') this%xyz(:,i)
728 call neko_log%message(log_buf, lvl = neko_log_debug)
729 end do
730
731 ! Field summary
732 write(log_buf, '(A,I6)') "Number of fields: ", this%n_fields
733 call neko_log%message(log_buf)
734 do i = 1, this%n_fields
735 write(log_buf, '(A,I6,A,A)') &
736 "Field: ", i, " ", trim(this%which_fields(i))
737 call neko_log%message(log_buf, lvl = neko_log_debug)
738 end do
739 call neko_log%end_section()
740 call neko_log%newline()
741
742 end subroutine probes_show
743
744 !> Show the status of processor/element owner and error code for each point
745 subroutine probes_debug(this)
746 class(probes_t) :: this
747
748 character(len=LOG_SIZE) :: log_buf ! For logging status
749 integer :: i
750
751 do i = 1, this%n_local_probes
752 write (log_buf, *) pe_rank, "/", this%global_interp%pe_owner(i), &
753 "/" , this%global_interp%el_owner0(i)
754 call neko_log%message(log_buf)
755 write(log_buf, '(A5, "(", F10.6, ",", F10.6, ",", F10.6, ")")') &
756 "rst: ", this%global_interp%rst(:, i)
757 call neko_log%message(log_buf)
758 end do
759 end subroutine probes_debug
760
761 !> Setup offset for rank 0
762 subroutine probes_setup_offset(this)
763 class(probes_t) :: this
764 integer :: ierr
765 this%n_local_probes_tot = 0
766 this%n_local_probes_tot_offset = 0
767 this%n_probes_offset = 0
768 call mpi_gather(this%n_local_probes, 1, mpi_integer, &
769 this%n_local_probes_tot, 1, mpi_integer, &
770 0, neko_comm, ierr)
771
772 call mpi_exscan(this%n_local_probes, this%n_probes_offset, 1, &
773 mpi_integer, mpi_sum, neko_comm, ierr)
774 call mpi_gather(this%n_probes_offset, 1, mpi_integer, &
775 this%n_local_probes_tot_offset, 1, mpi_integer, &
776 0, neko_comm, ierr)
777
778
779
780 end subroutine probes_setup_offset
781
782 !> Interpolate each probe from its `r,s,t` coordinates.
783 !! @note The final interpolated field is only available on rank 0.
784 !! @param t Current simulation time.
785 !! @param tstep Current time step.
786 subroutine probes_evaluate_and_write(this, time)
787 class(probes_t), intent(inout) :: this
788 type(time_state_t), intent(in) :: time
789 integer :: i, ierr
790 logical :: do_interp_on_host = .false.
791 character(len=1000) :: group_name
792 real(kind=rp) :: time_
793 integer :: out_int
794
795 !> Do not execute if we are below the start_time
796 if (time%t .lt. this%start_time) return
797
798 ! The inherited compute controller schedules evaluation and HDF5 writes.
799 do i = 1, this%n_fields
800 call this%global_interp%evaluate(this%out_values(:,i), &
801 this%sampled_fields%items(i)%ptr%x, &
802 do_interp_on_host)
803 end do
804
805 if (neko_bcknd_device .eq. 1) then
806 do i = 1, this%n_fields
807 call device_memcpy(this%out_values(:,i), this%out_values_d(i), &
808 this%n_local_probes, device_to_host, sync = .true.)
809 end do
810 end if
811
812 select type (ft => this%fout%file_type)
813 type is (csv_file_t)
814 ! Gather all values to rank 0
815 ! If io is only done at root
816 if (this%seq_io) then
817 call trsp(this%out_vals_trsp, this%n_fields, &
818 this%out_values, this%n_local_probes)
819 call mpi_gatherv(this%out_vals_trsp, &
820 this%n_fields*this%n_local_probes, &
821 mpi_real_precision, this%global_output_values, &
822 this%n_fields*this%n_local_probes_tot, &
823 this%n_fields*this%n_local_probes_tot_offset, &
824 mpi_real_precision, 0, neko_comm, ierr)
825 if (pe_rank .eq. 0) then
826 call trsp(this%mat_out%x, this%n_global_probes, &
827 this%global_output_values, this%n_fields)
828 call this%fout%write(this%mat_out, time%t)
829 end if
830 else
831 call neko_error("CSV outputs only works sequentially")
832 end if
833 call this%output_controller%register_execution(time)
834
835 type is (hdf5_file_t)
836
837 if (this%seq_io) then
838 call neko_error("HDF5 outputs only works in parallel currently.")
839
840 else
841
842 this%n_samples = this%n_samples + 1
843
844 ! Append output format
845 if (this%append_out) then
846
847 ! The file and probes group remain open from initialisation.
848 ! Write Nsteps in root
849 out_int = this%n_samples
850 call ft%write_attribute("NSteps", out_int)
851 ! Write out the data
852 do i = 1, this%n_fields
853 ! Write out_values directly, after the device-to-host
854 ! copy above on GPU builds.
855 call ft%write_array(this%out_values(:,i), &
856 trim(this%which_fields(i)), this%probe_layout)
857 end do
858
859 if (pe_rank == 0) this%time_out%x(1) = time%t
860 call ft%write_dataset(this%time_out, name = "time", &
861 layout = this%time_layout)
862
863 ! Write data in different steps
864 else
865
866 out_int = this%n_samples
867 ! Set up the name
868 write(group_name, '(A,I0)') "probes/Step_", out_int
869 call ft%set_active_group("probes")
870 ! Write Nsteps in root
871 call ft%write_attribute("NSteps", out_int)
872 ! Write out the data
873 call ft%set_active_group(trim(group_name))
874 do i = 1, this%n_fields
875 ! Write out_values directly, after the device-to-host
876 ! copy above on GPU builds.
877 call ft%write_array(this%out_values(:,i), &
878 trim(this%which_fields(i)), this%probe_layout)
879 end do
880 ! Write the time as an attribute
881 time_ = time%t
882 call ft%write_attribute("time", time_)
883 end if
884
885 end if
886 ! Every computed sample has already been written. The inherited
887 ! output controller only schedules closing and opening files.
888 if (this%output_controller%check(time)) then
889 call ft%close()
890 this%output_file_open = .false.
891 call this%open_output()
892 call this%output_controller%register_execution(time)
893 end if
894 class default
895 call neko_error("Invalid data. Expected csv_file_t or hdf5_file_t.")
896 end select
897
898 end subroutine probes_evaluate_and_write
899
900 !> Initialize the physical coordinates from a `csv` input file
901 !! @param points_file A csv file containing probes.
902 subroutine read_probe_locations(this, xyz, n_local_probes, n_global_probes, &
903 points_file)
904 class(probes_t), intent(inout) :: this
905 character(len=:), allocatable :: points_file
906 real(kind=rp), allocatable :: xyz(:,:)
907 integer, intent(inout) :: n_local_probes, n_global_probes
908 type(matrix_t) :: mat_in
909 integer :: ierr
910
911 !> Supporting variables
912 type(file_t) :: file_in
913
914 call file_in%init(trim(points_file))
915 !> Reads on rank 0 and distributes the probes across the different ranks
916 select type (ft => file_in%file_type)
917 type is (csv_file_t)
918 call read_xyz_from_csv(xyz, n_local_probes, n_global_probes, ft)
919 this%seq_io = .true.
920 type is (hdf5_file_t)
921 call ft%open("r")
922 call ft%read_dataset("xyz", mat_in, "rank_0")
923 call ft%close()
924
925 ! Copy the data to the xyz location
926 n_local_probes = mat_in%get_ncols()
927 call mpi_allreduce(n_local_probes, n_global_probes, 1, mpi_integer, &
928 mpi_sum, neko_comm, ierr)
929
930 allocate(xyz(3, n_local_probes)) ! We asume that axis 1 has 3 entries
931 call copy(xyz, mat_in%x, 3*n_local_probes)
932 call mat_in%free()
933 class default
934 call neko_error("Invalid data. Expected csv_file_t.")
935 end select
936
937 !> Close the file
938 call file_free(file_in)
939
940 end subroutine read_probe_locations
941
942 !> Read and initialize the number of probes from a `csv` input file
943 !! @param xyz xyz coordinates of the probes
944 !! @param n_local_probes The number of probes local to this process
945 !! @param n_global_probes The number of total probes on all processes
946 !! @param f The csv file we read from
947 subroutine read_xyz_from_csv(xyz, n_local_probes, n_global_probes, f)
948 type(csv_file_t), intent(inout) :: f
949 real(kind=rp), allocatable :: xyz(:,:)
950 integer, intent(inout) :: n_local_probes, n_global_probes
951 type(matrix_t) :: mat_in, mat_in2
952 integer :: n_lines
953
954 n_lines = f%count_lines()
955
956 ! Update the number of probes
957 n_global_probes = n_lines
958
959 ! Initialize the temporal array
960 if (pe_rank .eq. 0) then
961 n_local_probes = n_global_probes
962 allocate(xyz(3, n_local_probes))
963 call mat_in%init(n_global_probes,3)
964 call mat_in2%init(3, n_global_probes)
965 call f%read(mat_in)
966 call trsp(xyz, 3, mat_in%x, n_global_probes)
967 call mat_in%free()
968 call mat_in2%free()
969 else
970 n_local_probes = 0
971 allocate(xyz(3, n_local_probes))
972 end if
973
974 end subroutine read_xyz_from_csv
975end module probes
__inline__ __device__ void nonlinear_index(const int idx, const int lx, int *index)
Definition bc_utils.h:44
double real
Map a Fortran array to a device (allocate and associate).
Definition device.F90:83
Copy data between host and device (or device and device).
Definition device.F90:72
Unmap a Fortran array from a device (deassociate and free).
Definition device.F90:89
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
Retrieves a parameter by name or throws an error.
Defines a simulation case.
Definition case.f90:34
Definition comm.F90:1
type(mpi_datatype), public mpi_real_precision
MPI type for working precision of REAL types.
Definition comm.F90:54
integer, public pe_size
MPI size of communicator.
Definition comm.F90:62
integer, public pe_rank
MPI rank.
Definition comm.F90:59
type(mpi_comm), public neko_comm
MPI communicator.
Definition comm.F90:46
File format for .csv files, used for any read/write operations involving floating point data.
Definition csv_file.f90:35
subroutine, public device_copy(a_d, b_d, n, strm)
Copy a vector .
Device abstraction, common interface for various accelerators.
Definition device.F90:34
integer, parameter, public device_to_host
Definition device.F90:48
Defines a mapping of the degrees of freedom.
Definition dofmap.f90:35
Module for file I/O operations.
Definition file.f90:34
subroutine file_free(this)
File operation destructor.
Definition file.f90:158
Implements global_interpolation given a dofmap.
HDF5 file format.
Definition hdf5_file.F90:34
Module containing host-only array type.
Utilities for retrieving parameters from the case files.
subroutine, public json_get_subdict_or_empty(json, key, output)
Extract a sub-object from a json object and returns an empty object if the key is missing.
Logging routines.
Definition log.f90:34
integer, parameter, public neko_log_debug
Debug.
Definition log.f90:56
type(log_t), public neko_log
Global log stream.
Definition log.f90:92
integer, parameter, public log_size
Definition log.f90:46
Definition math.F90:60
subroutine, public copy(a, b, n)
Copy a vector .
Definition math.F90:305
Defines a matrix.
Definition matrix.f90:34
Build configurations.
integer, parameter neko_bcknd_device
integer, parameter, public dp
Definition num_types.f90:10
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
type(point_zone_registry_t), target, public neko_point_zone_registry
Global point_zone registry.
Implements probes.
Definition probes.F90:37
subroutine read_point_zone(this, json, dof)
Construct a list of points from a point zone.
Definition probes.F90:464
subroutine read_circle(this, json)
Construct a list of points from a circle.
Definition probes.F90:388
subroutine probes_init_from_components(this, dof, output_file, name, tolerance, padding)
Initialize based on individual parameters.
Definition probes.F90:273
subroutine add_points(this, new_points)
Append a new list of points to the exsiting list.
Definition probes.F90:505
subroutine probes_init_common(this, dof, output_file, name)
Common constructor.
Definition probes.F90:538
subroutine probes_evaluate_and_write(this, time)
Interpolate each probe from its r,s,t coordinates.
Definition probes.F90:787
subroutine read_line(this, json)
Construct a list of points from a line.
Definition probes.F90:344
subroutine read_probe_locations(this, xyz, n_local_probes, n_global_probes, points_file)
Initialize the physical coordinates from a csv input file.
Definition probes.F90:904
subroutine read_point(this, json)
Read a list of points from the json file.
Definition probes.F90:319
subroutine probes_show(this)
Print current probe status, with number of probes and coordinates.
Definition probes.F90:716
subroutine probes_free(this)
Destructor.
Definition probes.F90:657
subroutine probes_debug(this)
Show the status of processor/element owner and error code for each point.
Definition probes.F90:746
subroutine probes_setup_offset(this)
Setup offset for rank 0.
Definition probes.F90:763
subroutine probes_open_output(this)
Open a new numbered file and initialize its probe metadata.
Definition probes.F90:635
subroutine read_file(this, json)
Read a list of points from a csv file.
Definition probes.F90:298
subroutine read_xyz_from_csv(xyz, n_local_probes, n_global_probes, f)
Read and initialize the number of probes from a csv input file.
Definition probes.F90:948
subroutine probes_init_from_json(this, json, case)
Constructor from json.
Definition probes.F90:149
Defines a registry for storing solution fields.
Definition registry.f90:34
type(registry_t), target, public neko_registry
Global field registry.
Definition registry.f90:158
Simulation components are objects that encapsulate functionality that can be fit to a particular comp...
subroutine compute_(this, time)
Dummy compute function.
Tensor operations.
Definition tensor.f90:61
subroutine, public trsp(a, lda, b, ldb)
Transpose of a rectangular tensor .
Definition tensor.f90:124
Module with things related to the simulation time.
Utilities.
Definition utils.f90:35
integer, parameter, public neko_varname_len
Definition utils.f90:43
pure integer function, dimension(4), public nonlinear_index(linear_index, lx, ly, lz)
Compute (i,j,k,l) array given linear index with sizes (1:lx, 1:ly, 1:lz, :).
Definition utils.f90:337
field_list_t, To be able to group fields together
A wrapper around a polymorphic generic_file_t that handles its init. This is essentially a factory fo...
Definition file.f90:56
Implements global interpolation for arbitrary points in the domain.
Interface for HDF5 files.
Reusable distribution of a rank-ordered host array over NEKO_COMM. Counts are vector elements or matr...
Definition hdf5_file.F90:73
Host-only temporary array.
Base abstract type for point zones.
Base abstract class for simulation components.
A struct that contains all info about the time, expand as needed.