22#include "../base/base_uses.f90"
31 CHARACTER(LEN=*),
PARAMETER,
PRIVATE :: moduleN =
'realspace_grid_cube'
32 INTEGER,
PARAMETER,
PRIVATE :: cube_entry_len = 14, &
33 cube_num_entries_line = 6
36 CHARACTER(LEN=*),
PARAMETER,
PRIVATE :: cube_whitespace =
' '//achar(9)//achar(10)//achar(11)//achar(12)//achar(13)
37 CHARACTER(LEN=*),
PARAMETER,
PRIVATE :: missing_zeff_warning = &
38 "Effective nuclear charges were not supplied; "// &
39 "zeros will be written to the Cube atom records."
43 CHARACTER(LEN=:),
ALLOCATABLE :: line
44 INTEGER :: pos = 1, length = 0, complete = 0
59 INTEGER,
INTENT(IN) :: unit_nr
60 REAL(KIND=
dp),
DIMENSION(:),
INTENT(OUT) :: buffer
62 CHARACTER(LEN=:),
ALLOCATABLE :: longer_line
63 INTEGER :: count, first, nchar, nvalues, readstat
65 IF (.NOT.
ALLOCATED(reader%line))
ALLOCATE (
CHARACTER(LEN=512) :: reader%line)
67 DO WHILE (nvalues <
SIZE(buffer))
68 IF (reader%pos > reader%complete)
THEN
70 reader%line(:reader%length - reader%complete) = reader%line(reader%complete + 1:reader%length)
71 reader%length = reader%length - reader%complete
73 IF (reader%length == len(reader%line))
THEN
74 ALLOCATE (
CHARACTER(LEN=2*reader%length) :: longer_line)
75 longer_line(:reader%length) = reader%line
76 CALL move_alloc(longer_line, reader%line)
78 READ (unit_nr,
'(A)', advance=
'NO', size=nchar, iostat=readstat) reader%line(reader%length + 1:)
79 IF (readstat > 0) cpabort(
"Error reading cube data.")
80 reader%length = reader%length + nchar
81 IF (readstat < 0)
THEN
82 IF (is_iostat_end(readstat) .AND. reader%length == 0)
THEN
83 cpabort(
"Unexpected end of cube data.")
85 reader%complete = reader%length
88 reader%complete = scan(reader%line(:reader%length), cube_whitespace, back=.true.)
89 IF (reader%complete > 0)
EXIT
95 count = cube_value_span(reader%line(:reader%complete), reader%pos,
SIZE(buffer) - nvalues)
96 CALL cube_parse_values(reader%line(first:reader%pos - 1), buffer(nvalues + 1:nvalues + count))
97 nvalues = nvalues + count
109 INTEGER FUNCTION cube_value_span(text, pos, limit)
RESULT(count)
110 CHARACTER(LEN=*),
INTENT(INOUT) :: text
111 INTEGER,
INTENT(INOUT) :: pos
112 INTEGER,
INTENT(IN) :: limit
118 DO WHILE (pos <= len(text))
119 SELECT CASE (text(pos:pos))
120 CASE (
' ', achar(9):achar(13))
124 IF (.NOT. in_value)
THEN
125 IF (count == limit)
EXIT
132 END FUNCTION cube_value_span
139 SUBROUTINE cube_parse_values(text, values)
140 CHARACTER(LEN=*),
INTENT(IN) :: text
141 REAL(KIND=
dp),
DIMENSION(:),
INTENT(OUT) :: values
145 IF (
SIZE(values) == 0)
RETURN
147 IF (scan(text,
',/*;!') /= 0) cpabort(
"Bad value while reading cube data.")
148 READ (text, *, iostat=stat) values
149 IF (stat /= 0) cpabort(
"Bad value while reading cube data.")
150 END SUBROUTINE cube_parse_values
166 SUBROUTINE pw_to_cube(pw, unit_nr, title, particles_r, particles_z, particles_zeff, &
167 stride, max_file_size_mb, zero_tails, silent, mpi_io)
169 INTEGER,
INTENT(IN) :: unit_nr
170 CHARACTER(*),
INTENT(IN),
OPTIONAL :: title
171 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN), &
172 OPTIONAL :: particles_r
173 INTEGER,
DIMENSION(:),
INTENT(IN),
OPTIONAL :: particles_z
174 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN),
OPTIONAL :: particles_zeff
175 INTEGER,
DIMENSION(:),
OPTIONAL,
POINTER :: stride
176 REAL(kind=
dp),
INTENT(IN),
OPTIONAL :: max_file_size_mb
177 LOGICAL,
INTENT(IN),
OPTIONAL :: zero_tails, silent, mpi_io
179 CHARACTER(LEN=*),
PARAMETER :: routinen =
'pw_to_cube'
181 INTEGER :: checksum, dest, handle, i, i1, i2, i3, iat, ip, l1, l2, l3, msglen, my_rank, &
182 my_stride(3), np, num_linebreak, num_pe, rank(2), size_of_z, source, tag, u1, u2, u3
183 LOGICAL :: be_silent, my_zero_tails, parallel_write
184 REAL(kind=
dp) :: compression_factor, my_max_file_size_mb
185 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: buf
189 CALL timeset(routinen, handle)
191 my_zero_tails = .false.
193 parallel_write = .false.
194 my_max_file_size_mb = 0.0_dp
195 IF (
PRESENT(zero_tails)) my_zero_tails = zero_tails
196 IF (
PRESENT(silent)) be_silent = silent
197 IF (
PRESENT(mpi_io)) parallel_write = mpi_io
198 IF (
PRESENT(max_file_size_mb)) my_max_file_size_mb = max_file_size_mb
199 cpassert(my_max_file_size_mb >= 0)
200 IF (.NOT. parallel_write .AND.
PRESENT(particles_z) .AND. &
201 .NOT.
PRESENT(particles_zeff))
THEN
202 cpwarn(missing_zeff_warning)
206 IF (
PRESENT(stride))
THEN
207 IF (
SIZE(stride) /= 1 .AND.
SIZE(stride) /= 3)
THEN
208 CALL cp_abort(__location__,
"STRIDE keyword can accept only 1 "// &
209 "(the same for X,Y,Z) or 3 values. Correct your input file.")
211 IF (
SIZE(stride) == 1)
THEN
213 my_stride(i) = stride(1)
216 my_stride = stride(1:3)
220 IF (my_max_file_size_mb > 0)
THEN
222 compression_factor = 1.0e-06_dp*cube_entry_len*product(real(pw%pw_grid%npts,
dp))/my_max_file_size_mb
223 my_stride(:) = int(compression_factor**(1.0_dp/3.0_dp)) + 1
226 cpassert(my_stride(1) > 0)
227 cpassert(my_stride(2) > 0)
228 cpassert(my_stride(3) > 0)
230 IF (.NOT. parallel_write)
THEN
231 IF (unit_nr > 0)
THEN
234 WRITE (unit_nr,
'(a11)')
"-Quickstep-"
235 IF (
PRESENT(title))
THEN
236 WRITE (unit_nr, *) trim(title)
238 WRITE (unit_nr, *)
"No Title"
241 cpassert(
PRESENT(particles_z) .EQV.
PRESENT(particles_r))
243 IF (
PRESENT(particles_z))
THEN
244 cpassert(
SIZE(particles_z) ==
SIZE(particles_r, dim=2))
247 np = min(99999,
SIZE(particles_z))
250 WRITE (unit_nr,
'(I5,3f12.6)') np, 0.0_dp, 0._dp, 0._dp
252 WRITE (unit_nr,
'(I5,3f12.6)') (pw%pw_grid%npts(1) + my_stride(1) - 1)/my_stride(1), &
253 pw%pw_grid%dh(1, 1)*real(my_stride(1),
dp), pw%pw_grid%dh(2, 1)*real(my_stride(1),
dp), &
254 pw%pw_grid%dh(3, 1)*real(my_stride(1),
dp)
255 WRITE (unit_nr,
'(I5,3f12.6)') (pw%pw_grid%npts(2) + my_stride(2) - 1)/my_stride(2), &
256 pw%pw_grid%dh(1, 2)*real(my_stride(2),
dp), pw%pw_grid%dh(2, 2)*real(my_stride(2),
dp), &
257 pw%pw_grid%dh(3, 2)*real(my_stride(2),
dp)
258 WRITE (unit_nr,
'(I5,3f12.6)') (pw%pw_grid%npts(3) + my_stride(3) - 1)/my_stride(3), &
259 pw%pw_grid%dh(1, 3)*real(my_stride(3),
dp), pw%pw_grid%dh(2, 3)*real(my_stride(3),
dp), &
260 pw%pw_grid%dh(3, 3)*real(my_stride(3),
dp)
262 IF (
PRESENT(particles_z))
THEN
263 IF (
PRESENT(particles_zeff))
THEN
265 WRITE (unit_nr,
'(I5,4f12.6)') particles_z(iat), particles_zeff(iat), particles_r(:, iat)
269 WRITE (unit_nr,
'(I5,4f12.6)') particles_z(iat), 0._dp, particles_r(:, iat)
276 l1 = pw%pw_grid%bounds(1, 1)
277 l2 = pw%pw_grid%bounds(1, 2)
278 l3 = pw%pw_grid%bounds(1, 3)
279 u1 = pw%pw_grid%bounds(2, 1)
280 u2 = pw%pw_grid%bounds(2, 2)
281 u3 = pw%pw_grid%bounds(2, 3)
283 ALLOCATE (buf(l3:u3))
285 my_rank = pw%pw_grid%para%group%mepos
286 gid = pw%pw_grid%para%group
287 num_pe = pw%pw_grid%para%group%num_pe
293 IF (unit_nr > 0) checksum = 1
295 CALL gid%sum(checksum)
296 cpassert(checksum == 1)
298 CALL gid%maxloc(rank)
299 cpassert(
rank(1) > 0)
302 DO i1 = l1, u1, my_stride(1)
303 DO i2 = l2, u2, my_stride(2)
307 DO ip = 0, num_pe - 1
308 IF (pw%pw_grid%para%bo(1, 1, ip, 1) <= i1 - l1 + 1 .AND. pw%pw_grid%para%bo(2, 1, ip, 1) >= i1 - l1 + 1 .AND. &
309 pw%pw_grid%para%bo(1, 2, ip, 1) <= i2 - l2 + 1 .AND. pw%pw_grid%para%bo(2, 2, ip, 1) >= i2 - l2 + 1)
THEN
317 IF (source == dest)
THEN
318 IF (my_rank == source)
THEN
319 buf(:) = pw%array(i1, i2, :)
322 IF (my_rank == source)
THEN
323 buf(:) = pw%array(i1, i2, :)
324 CALL gid%send(buf, dest, tag)
326 IF (my_rank == dest)
THEN
327 CALL gid%recv(buf, source, tag)
331 IF (unit_nr > 0)
THEN
332 IF (my_zero_tails)
THEN
334 IF (buf(i3) < 1.e-7_dp) buf(i3) = 0.0_dp
353 size_of_z = ceiling(real(pw%pw_grid%bounds(2, 3) - pw%pw_grid%bounds(1, 3) + 1,
dp)/real(my_stride(3),
dp))
354 num_linebreak = (size_of_z + cube_num_entries_line - 1)/cube_num_entries_line
356 CALL mp_unit%set_handle(unit_nr)
357 CALL pw_to_cube_parallel(pw, mp_unit, title, particles_r, particles_z, particles_zeff, &
358 my_stride, my_zero_tails, msglen)
361 CALL timestop(handle)
377 SUBROUTINE cube_to_pw(grid, filename, scaling, parallel_read, silent)
380 CHARACTER(LEN=*),
INTENT(IN) :: filename
381 REAL(kind=
dp),
INTENT(IN) :: scaling
382 LOGICAL,
INTENT(IN) :: parallel_read
383 LOGICAL,
INTENT(IN),
OPTIONAL :: silent
385 CHARACTER(LEN=*),
PARAMETER :: routinen =
'cube_to_pw'
387 INTEGER :: extunit, handle, i, j, my_rank, nat, &
389 INTEGER(KIND=file_offset) :: data_position
390 INTEGER,
DIMENSION(3) :: lbounds, lbounds_local, npoints, &
391 ubounds, ubounds_local
393 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: buffer
394 REAL(kind=
dp),
DIMENSION(3) :: rdum
398 CALL timeset(routinen, handle)
402 IF (
PRESENT(silent)) be_silent = silent
403 gid = grid%pw_grid%para%group
405 npoints = grid%pw_grid%npts
406 lbounds = grid%pw_grid%bounds(1, :)
407 ubounds = grid%pw_grid%bounds(2, :)
408 lbounds_local = grid%pw_grid%bounds_local(1, :)
409 ubounds_local = grid%pw_grid%bounds_local(2, :)
411 IF (my_rank == 0)
THEN
412 IF (output_unit > 0 .AND. .NOT. be_silent)
THEN
413 WRITE (output_unit, fmt=
"(/,T2,A,/,/,T2,A,/)")
"Reading the cube file: ", trim(filename)
415 CALL open_file(file_name=filename, file_status=
"OLD", file_form=
"FORMATTED", &
416 file_action=
"READ", file_access=
"STREAM", unit_number=extunit)
421 READ (extunit, *) nat, rdum
423 READ (extunit, *) ndum, rdum
424 IF ((ndum /= npoints(i) .OR. abs(rdum(i) - grid%pw_grid%dh(i, i)) > 1e-4_dp) .AND. &
425 output_unit > 0)
THEN
426 WRITE (output_unit, *)
"Restart from density | ERROR! | CUBE FILE NOT COINCIDENT WITH INTERNAL GRID ", i
427 WRITE (output_unit, *)
"Restart from density | ", ndum,
" DIFFERS FROM ", npoints(i)
428 WRITE (output_unit, *)
"Restart from density | ", rdum,
" DIFFERS FROM ", grid%pw_grid%dh(i, i)
436 IF (parallel_read)
THEN
437 IF (my_rank == 0)
THEN
438 INQUIRE (unit=extunit, pos=data_position)
441 CALL gid%bcast(data_position, 0)
443 CALL cube_to_pw_parallel(grid, filename, scaling, data_position - 1)
445 ALLOCATE (buffer(lbounds(3):ubounds(3)))
446 DO i = lbounds(1), ubounds(1)
447 DO j = lbounds(2), ubounds(2)
448 IF (my_rank == 0)
CALL reader%read(extunit, buffer)
449 CALL gid%bcast(buffer, 0)
450 IF (lbounds_local(1) <= i .AND. i <= ubounds_local(1) .AND. &
451 lbounds_local(2) <= j .AND. j <= ubounds_local(2))
THEN
452 grid%array(i, j, lbounds(3):ubounds(3)) = scaling*buffer
456 IF (my_rank == 0)
CALL close_file(unit_number=extunit)
459 CALL timestop(handle)
470 SUBROUTINE cube_to_pw_parallel(grid, filename, scaling, data_offset)
472 CHARACTER(LEN=*),
INTENT(IN) :: filename
473 REAL(kind=
dp),
INTENT(IN) :: scaling
474 INTEGER(KIND=file_offset),
INTENT(IN) :: data_offset
476 CHARACTER(LEN=:),
ALLOCATABLE :: text
477 INTEGER :: count, dest, i, j, n, np, nz, packed, &
478 pos, rank, received, xhi, xlo
479 INTEGER(KIND=file_offset) :: base, block_size, chunk_end, consumed, &
480 file_size, first, hi, lo, plane, &
482 INTEGER(KIND=file_offset),
ALLOCATABLE, &
483 DIMENSION(:) :: counts, offsets
484 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: rcount, rdispl, scount, sdispl
485 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: local, sendbuf, values
489 gid = grid%pw_grid%para%group
492 nz = grid%pw_grid%npts(3)
493 plane_size = int(grid%pw_grid%npts(2),
file_offset)*nz
494 total = grid%pw_grid%ngpts
495 ALLOCATE (offsets(0:np), counts(0:np - 1), local(
SIZE(grid%array)))
496 ALLOCATE (rcount(0:np - 1), rdispl(0:np - 1), scount(0:np - 1), sdispl(0:np - 1))
499 CALL file%get_size(file_size)
501 block_size = min(8_file_offset*1024*1024, (file_size - data_offset + np - 1)/np)
505 DO WHILE (consumed < total)
506 IF (base >= file_size) cpabort(
"Unexpected end of cube data.")
507 chunk_end = min(file_size, base + (rank + 1)*block_size)
509 CALL gid%allgather(cube_chunk_end(file, chunk_end, file_size), offsets(1:))
510 ALLOCATE (
CHARACTER(LEN=offsets(rank + 1) - offsets(rank)) :: text)
511 CALL file%read_at_all(offsets(rank), text)
513 count = cube_value_span(text, pos, len(text))
514 CALL gid%allgather(int(count,
file_offset), counts)
516 first = min(consumed + sum(counts(:rank - 1)), total)
517 n = int(min(int(count,
file_offset), total - first))
520 n = cube_value_span(text, pos, n)
522 ALLOCATE (values(n), sendbuf(n))
523 CALL cube_parse_values(text(:pos - 1), values)
527 xlo = int(first/plane_size) + 1
528 xhi = int((first + n - 1)/plane_size) + 1
531 sdispl(dest) = packed
532 associate(bo => grid%pw_grid%para%bo(:, :, dest, 1))
533 DO i = max(bo(1, 1), xlo), min(bo(2, 1), xhi)
534 plane = (i - 1)*plane_size
535 lo = max(first, plane + int(bo(1, 2) - 1,
file_offset)*nz)
536 hi = min(first + n, plane + int(bo(2, 2),
file_offset)*nz)
538 sendbuf(packed + 1:packed + hi - lo) = values(lo - first + 1:hi - first)
539 packed = packed + int(hi - lo)
542 scount(dest) = packed - sdispl(dest)
544 CALL gid%alltoall(scount, rcount, 1)
547 rdispl(i) = rdispl(i - 1) + rcount(i - 1)
549 CALL gid%alltoall(sendbuf, scount, sdispl, local(received + 1:), rcount, rdispl)
550 received = received + sum(rcount)
551 consumed = consumed + sum(counts)
553 DEALLOCATE (sendbuf, values)
558 associate(bounds => grid%pw_grid%bounds_local)
559 DO i = bounds(1, 1), bounds(2, 1)
560 DO j = bounds(1, 2), bounds(2, 2)
561 grid%array(i, j, :) = scaling*local(pos + 1:pos + nz)
566 END SUBROUTINE cube_to_pw_parallel
575 FUNCTION cube_chunk_end(file, offset, file_size)
RESULT(last)
576 TYPE(mp_file_type),
INTENT(IN) :: file
577 INTEGER(KIND=file_offset),
INTENT(IN) :: offset, file_size
578 INTEGER(KIND=file_offset) :: last
580 CHARACTER(LEN=128) :: probe
584 DO WHILE (last < file_size)
585 n = int(min(int(len(probe), file_offset), file_size - last))
586 CALL file%read_at(last, probe(:n))
587 pos = scan(probe(:n), cube_whitespace)
589 last = last + pos - 1
594 END FUNCTION cube_chunk_end
610 SUBROUTINE pw_to_cube_parallel(grid, unit_nr, title, particles_r, particles_z, particles_zeff, &
611 stride, zero_tails, msglen)
613 TYPE(pw_r3d_rs_type),
INTENT(IN) :: grid
614 TYPE(mp_file_type),
INTENT(IN) :: unit_nr
615 CHARACTER(*),
INTENT(IN),
OPTIONAL :: title
616 REAL(kind=dp),
DIMENSION(:, :),
INTENT(IN), &
617 OPTIONAL :: particles_r
618 INTEGER,
DIMENSION(:),
INTENT(IN),
OPTIONAL :: particles_z
619 REAL(kind=dp),
DIMENSION(:),
INTENT(IN),
OPTIONAL :: particles_zeff
620 INTEGER,
INTENT(IN) :: stride(3)
621 LOGICAL,
INTENT(IN) :: zero_tails
622 INTEGER,
INTENT(IN) :: msglen
624 INTEGER,
PARAMETER :: header_len = 41, header_len_z = 53
626 CHARACTER(LEN=cube_entry_len) :: value
627 CHARACTER(LEN=header_len) ::
header
628 CHARACTER(LEN=header_len_z) :: header_z
629 INTEGER :: counter, i, islice, j, k, my_rank, np, &
631 INTEGER(KIND=file_offset) :: bof
632 INTEGER(KIND=file_offset),
ALLOCATABLE, &
633 DIMENSION(:),
TARGET :: displacements
634 INTEGER,
ALLOCATABLE,
DIMENSION(:),
TARGET :: blocklengths
635 INTEGER,
DIMENSION(3) :: lbounds, lbounds_local, ubounds, &
637 CHARACTER(LEN=msglen),
ALLOCATABLE,
DIMENSION(:) ::
writebuffer
638 CHARACTER(LEN=msglen) :: tmp
639 LOGICAL :: should_write(2)
640 TYPE(mp_comm_type) :: gid
641 TYPE(mp_file_descriptor_type) :: mp_desc
644 gid = grid%pw_grid%para%group
645 my_rank = grid%pw_grid%para%group%mepos
646 IF (
PRESENT(particles_z) .AND. .NOT.
PRESENT(particles_zeff))
THEN
647 cpwarn(missing_zeff_warning)
651 lbounds = grid%pw_grid%bounds(1, :)
652 ubounds = grid%pw_grid%bounds(2, :)
653 lbounds_local = grid%pw_grid%bounds_local(1, :)
654 ubounds_local = grid%pw_grid%bounds_local(2, :)
657 DO i = lbounds(1), ubounds(1), stride(1)
658 should_write(:) = .true.
659 IF (i < lbounds_local(1))
THEN
660 should_write(1) = .false.
661 ELSE IF (i > ubounds_local(1))
THEN
664 DO j = lbounds(2), ubounds(2), stride(2)
665 should_write(2) = .true.
666 IF (j < lbounds_local(2) .OR. j > ubounds_local(2))
THEN
667 should_write(2) = .false.
669 IF (all(should_write .EQV. .true.))
THEN
677 CALL unit_nr%get_position(bof)
679 IF (my_rank == 0)
THEN
682 CALL unit_nr%write_at(bof,
"-Quickstep-"//new_line(
"C"))
683 bof = bof + len(
"-Quickstep-"//new_line(
"C"))*mpi_character_size
684 IF (
PRESENT(title))
THEN
685 CALL unit_nr%write_at(bof, trim(title)//new_line(
"C"))
686 bof = bof + len(trim(title)//new_line(
"C"))*mpi_character_size
688 CALL unit_nr%write_at(bof,
"No Title"//new_line(
"C"))
689 bof = bof + len(
"No Title"//new_line(
"C"))*mpi_character_size
692 cpassert(
PRESENT(particles_z) .EQV.
PRESENT(particles_r))
694 IF (
PRESENT(particles_z))
THEN
695 cpassert(
SIZE(particles_z) ==
SIZE(particles_r, dim=2))
698 np = min(99999,
SIZE(particles_z))
701 WRITE (
header,
'(I5,3f12.6)') np, 0.0_dp, 0._dp, 0._dp
702 CALL unit_nr%write_at(bof,
header//new_line(
"C"))
703 bof = bof + len(
header//new_line(
"C"))*mpi_character_size
705 WRITE (
header,
'(I5,3f12.6)') (grid%pw_grid%npts(1) + stride(1) - 1)/stride(1), &
706 grid%pw_grid%dh(1, 1)*real(stride(1), dp), grid%pw_grid%dh(2, 1)*real(stride(1), dp), &
707 grid%pw_grid%dh(3, 1)*real(stride(1), dp)
708 CALL unit_nr%write_at(bof,
header//new_line(
"C"))
709 bof = bof + len(
header//new_line(
"C"))*mpi_character_size
711 WRITE (
header,
'(I5,3f12.6)') (grid%pw_grid%npts(2) + stride(2) - 1)/stride(2), &
712 grid%pw_grid%dh(1, 2)*real(stride(2), dp), grid%pw_grid%dh(2, 2)*real(stride(2), dp), &
713 grid%pw_grid%dh(3, 2)*real(stride(2), dp)
714 CALL unit_nr%write_at(bof,
header//new_line(
"C"))
715 bof = bof + len(
header//new_line(
"C"))*mpi_character_size
717 WRITE (
header,
'(I5,3f12.6)') (grid%pw_grid%npts(3) + stride(3) - 1)/stride(3), &
718 grid%pw_grid%dh(1, 3)*real(stride(3), dp), grid%pw_grid%dh(2, 3)*real(stride(3), dp), &
719 grid%pw_grid%dh(3, 3)*real(stride(3), dp)
720 CALL unit_nr%write_at(bof,
header//new_line(
"C"))
721 bof = bof + len(
header//new_line(
"C"))*mpi_character_size
723 IF (
PRESENT(particles_z))
THEN
724 IF (
PRESENT(particles_zeff))
THEN
726 WRITE (header_z,
'(I5,4f12.6)') particles_z(i), particles_zeff(i), particles_r(:, i)
727 CALL unit_nr%write_at(bof, header_z//new_line(
"C"))
728 bof = bof + len(header_z//new_line(
"C"))*mpi_character_size
732 WRITE (header_z,
'(I5,4f12.6)') particles_z(i), 0._dp, particles_r(:, i)
733 CALL unit_nr%write_at(bof, header_z//new_line(
"C"))
734 bof = bof + len(header_z//new_line(
"C"))*mpi_character_size
740 CALL gid%bcast(bof, grid%pw_grid%para%group%source)
743 ALLOCATE (displacements(nslices))
747 DO i = lbounds(1), ubounds(1), stride(1)
748 should_write(:) = .true.
749 IF (i < lbounds_local(1))
THEN
750 should_write(1) = .false.
751 ELSE IF (i > ubounds_local(1))
THEN
754 DO j = lbounds(2), ubounds(2), stride(2)
755 should_write(2) = .true.
756 IF (j < lbounds_local(2) .OR. j > ubounds_local(2))
THEN
757 should_write(2) = .false.
759 IF (all(should_write .EQV. .true.))
THEN
760 IF (islice > nslices) cpabort(
"Index out of bounds.")
761 displacements(islice) = bof
764 DO k = lbounds(3), ubounds(3), stride(3)
765 IF (zero_tails .AND. grid%array(i, j, k) < 1.e-7_dp)
THEN
770 tmp = trim(tmp)//trim(
value)
771 counter = counter + 1
772 IF (
modulo(counter, cube_num_entries_line) == 0 .OR. ubounds(3) - k < stride(3))
THEN
773 tmp = trim(tmp)//new_line(
'C')
785 ALLOCATE (blocklengths(nslices))
786 blocklengths(:) = msglen
787 mp_desc = mp_file_type_hindexed_make_chv(nslices, blocklengths, displacements)
793 CALL mp_file_type_set_view_chv(unit_nr, bof, mp_desc)
795 CALL unit_nr%write_all(msglen, nslices,
writebuffer, mp_desc)
797 CALL mp_file_type_free(mp_desc)
799 DEALLOCATE (blocklengths, displacements)
801 END SUBROUTINE pw_to_cube_parallel
814 TYPE(pw_r3d_rs_type),
INTENT(IN) :: pw
815 INTEGER,
INTENT(IN) :: unit_nr
816 INTEGER,
DIMENSION(:),
INTENT(IN),
OPTIONAL :: stride
817 TYPE(pw_r3d_rs_type),
INTENT(IN),
OPTIONAL :: pw2
819 CHARACTER(LEN=*),
PARAMETER :: routinen =
'pw_to_simple_volumetric'
821 INTEGER :: checksum, dest, handle, i, i1, i2, i3, &
822 ip, l1, l2, l3, my_rank, my_stride(3), &
823 ngrids, npoints, num_pe, rank(2), &
824 source, tag, u1, u2, u3
826 REAL(kind=dp) :: x, y, z
827 REAL(kind=dp),
ALLOCATABLE,
DIMENSION(:) :: buf, buf2
828 TYPE(mp_comm_type) :: gid
830 CALL timeset(routinen, handle)
834 IF (
PRESENT(pw2)) double = .true.
837 IF (
PRESENT(stride))
THEN
838 IF (
SIZE(stride) /= 1 .AND.
SIZE(stride) /= 3)
THEN
839 CALL cp_abort(__location__,
"STRIDE keyword can accept only 1 "// &
840 "(the same for X,Y,Z) or 3 values. Correct your input file.")
842 IF (
SIZE(stride) == 1)
THEN
844 my_stride(i) = stride(1)
847 my_stride = stride(1:3)
849 cpassert(my_stride(1) > 0)
850 cpassert(my_stride(2) > 0)
851 cpassert(my_stride(3) > 0)
855 l1 = pw%pw_grid%bounds(1, 1)
856 l2 = pw%pw_grid%bounds(1, 2)
857 l3 = pw%pw_grid%bounds(1, 3)
858 u1 = pw%pw_grid%bounds(2, 1)
859 u2 = pw%pw_grid%bounds(2, 2)
860 u3 = pw%pw_grid%bounds(2, 3)
864 IF (double) ngrids = 2
865 npoints = ((pw%pw_grid%npts(1) + my_stride(1) - 1)/my_stride(1))* &
866 ((pw%pw_grid%npts(2) + my_stride(2) - 1)/my_stride(1))* &
867 ((pw%pw_grid%npts(3) + my_stride(3) - 1)/my_stride(1))
868 IF (unit_nr > 1)
WRITE (unit_nr,
'(I7,I5)') npoints, ngrids
870 ALLOCATE (buf(l3:u3))
871 IF (double)
ALLOCATE (buf2(l3:u3))
873 my_rank = pw%pw_grid%para%group%mepos
874 gid = pw%pw_grid%para%group
875 num_pe = pw%pw_grid%para%group%num_pe
881 IF (unit_nr > 0) checksum = 1
883 CALL gid%sum(checksum)
884 cpassert(checksum == 1)
886 CALL gid%maxloc(rank)
887 cpassert(
rank(1) > 0)
890 DO i1 = l1, u1, my_stride(1)
891 DO i2 = l2, u2, my_stride(2)
894 IF (pw%pw_grid%para%mode /= pw_mode_local)
THEN
895 DO ip = 0, num_pe - 1
896 IF (pw%pw_grid%para%bo(1, 1, ip, 1) <= i1 - l1 + 1 .AND. pw%pw_grid%para%bo(2, 1, ip, 1) >= i1 - l1 + 1 .AND. &
897 pw%pw_grid%para%bo(1, 2, ip, 1) <= i2 - l2 + 1 .AND. pw%pw_grid%para%bo(2, 2, ip, 1) >= i2 - l2 + 1)
THEN
905 IF (source == dest)
THEN
906 IF (my_rank == source)
THEN
907 buf(:) = pw%array(i1, i2, :)
908 IF (double) buf2(:) = pw2%array(i1, i2, :)
911 IF (my_rank == source)
THEN
912 buf(:) = pw%array(i1, i2, :)
913 CALL gid%send(buf, dest, tag)
915 buf2(:) = pw2%array(i1, i2, :)
916 CALL gid%send(buf2, dest, tag)
919 IF (my_rank == dest)
THEN
920 CALL gid%recv(buf, source, tag)
921 IF (double)
CALL gid%recv(buf2, source, tag)
925 IF (.NOT. double)
THEN
926 DO i3 = l3, u3, my_stride(3)
927 x = pw%pw_grid%dh(1, 1)*i1 + &
928 pw%pw_grid%dh(2, 1)*i2 + &
929 pw%pw_grid%dh(3, 1)*i3
931 y = pw%pw_grid%dh(1, 2)*i1 + &
932 pw%pw_grid%dh(2, 2)*i2 + &
933 pw%pw_grid%dh(3, 2)*i3
935 z = pw%pw_grid%dh(1, 3)*i1 + &
936 pw%pw_grid%dh(2, 3)*i2 + &
937 pw%pw_grid%dh(3, 3)*i3
939 IF (unit_nr > 0)
THEN
940 WRITE (unit_nr,
'(6(1X,ES12.4E3), 6(1X,ES12.4E3), 6(1X,ES12.4E3), 6(1X,ES12.4E3))') x, y, z, buf(i3)
946 DO i3 = l3, u3, my_stride(3)
947 x = pw%pw_grid%dh(1, 1)*i1 + &
948 pw%pw_grid%dh(2, 1)*i2 + &
949 pw%pw_grid%dh(3, 1)*i3
951 y = pw%pw_grid%dh(1, 2)*i1 + &
952 pw%pw_grid%dh(2, 2)*i2 + &
953 pw%pw_grid%dh(3, 2)*i3
955 z = pw%pw_grid%dh(1, 3)*i1 + &
956 pw%pw_grid%dh(2, 3)*i2 + &
957 pw%pw_grid%dh(3, 3)*i3
959 IF (unit_nr > 0)
THEN
960 WRITE (unit_nr,
'(6(1X,ES12.4E3), 6(1X,ES12.4E3), 6(1X,ES12.4E3), 6(1X,ES12.4E3))') x, y, z, buf(i3), buf2(i3)
978 IF (double)
DEALLOCATE (buf2)
980 CALL timestop(handle)
static int modulo(int a, int m)
Equivalent of Fortran's MODULO which always returns a positive number. https://gcc....
Utility routines to open and close files.
subroutine, public open_file(file_name, unit_number, file_status, file_form, file_action, file_position, file_pad, file_access, debug)
Opens a file and returns its logical unit number.
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
Defines the basic variable types.
integer, parameter, public dp
Interface to the message passing library MPI.
type(mp_file_descriptor_type) function, public mp_file_type_hindexed_make_chv(count, lengths, displs)
Creates an indexed MPI type for arrays of strings using bytes for spacing (hindexed type).
subroutine, public mp_file_type_free(type_descriptor)
Releases the type used for MPI I/O.
integer, parameter, public mpi_character_size
integer, parameter, public file_offset
integer, parameter, public file_amode_rdonly
subroutine, public mp_file_type_set_view_chv(fh, offset, type_descriptor)
Uses a previously created indexed MPI character type to tell the MPI processes how to partition (set_...
integer, parameter, public pw_mode_local
Generate Gaussian cube files.
subroutine, public cube_to_pw(grid, filename, scaling, parallel_read, silent)
Computes the external density on the grid hacked from external_read_density.
character(len= *), parameter, public cube_values_format
subroutine, public pw_to_simple_volumetric(pw, unit_nr, stride, pw2)
Prints a simple grid file: X Y Z value.
character(len= *), parameter, public cube_value_format
subroutine cube_read_values(reader, unit_nr, buffer)
Read whitespace-separated cube values, retaining any unused part of the current record.
subroutine, public pw_to_cube(pw, unit_nr, title, particles_r, particles_z, particles_zeff, stride, max_file_size_mb, zero_tails, silent, mpi_io)
...
void writebuffer(int *psockfd, char *data, int *plen)
Writes to a socket.