30 use,
intrinsic :: iso_fortran_env
84 real(real64),
allocatable :: center(:)
85 real(real64) :: radius =
m_zero
86 class(box_t),
pointer :: box => null()
88 integer,
allocatable :: map(:)
89 integer :: num_regions
90 integer,
allocatable :: regions(:)
91 type(accel_mem_t) :: buff_map
92 real(real64),
allocatable :: rel_x(:,:)
93 real(real64),
allocatable :: r(:)
94 type(mesh_t),
pointer :: mesh => null()
97 integer :: np_global = -1
98 real(real64),
allocatable :: rel_x_global(:,:)
99 integer,
allocatable :: part_v(:)
100 integer,
allocatable :: global2local(:)
102 type(mesh_cube_map_t) :: cube_map
119 recursive real(real64) function f_n(dims)
result(fn)
124 else if (dims == 1)
then
134 type(submesh_t),
intent(inout) :: this
135 class(space_t),
intent(in) :: space
136 class(mesh_t),
target,
intent(in) :: mesh
137 class(box_t),
target,
intent(in) :: box
138 real(real64),
intent(in) :: center(:)
140 integer :: original_mesh_index, submesh_index
141 logical :: submesh_points(1:mesh%np)
142 real(real64),
allocatable :: xtmp(:, :), rtmp(:)
146 assert(.not. space%is_periodic())
153 submesh_points = box%contains_points(mesh%np, mesh%x)
154 this%np = count(submesh_points)
156 assert(this%np <= mesh%np)
158 safe_allocate(this%map(1:this%np))
159 safe_allocate(xtmp(1:space%dim, 1:this%np))
160 safe_allocate(rtmp(1:this%np))
166 do original_mesh_index = 1, mesh%np
168 if (submesh_points(original_mesh_index))
then
169 submesh_index = submesh_index + 1
170 this%map(submesh_index) = original_mesh_index
172 xtmp(:, submesh_index) = mesh%x(original_mesh_index, :) - center(:)
173 rtmp(submesh_index) = norm2(xtmp(:, submesh_index))
179 safe_deallocate_a(xtmp)
180 safe_deallocate_a(rtmp)
189 class(
mesh_t),
target,
intent(in) :: mesh
191 real(real64),
intent(in) :: center(1:space%dim)
192 real(real64),
intent(in) :: rc
194 real(real64) :: r2, rc2, xx(space%dim), rc_norm_n
195 real(real64),
allocatable :: center_copies(:,:), xtmp(:, :), rtmp(:), x(:,:)
196 integer :: icell, is, ip, ix, iy, iz
197 integer(int64) :: max_elements_count
198 type(lattice_iterator_t) :: latt_iter
199 integer,
allocatable :: map_inv(:), map_temp(:)
200 integer :: nmax(3), nmin(3)
201 real(real64),
parameter :: tol = 1e-13_real64
207 assert(space%dim <= 3)
211 safe_allocate(this%center(1:space%dim))
220 if (.not. space%is_periodic())
then
223 safe_allocate(map_inv(0:this%mesh%np))
224 map_inv(0:this%mesh%np) = 0
230 nmin(1:space%dim) = int((center(1:space%dim) - abs(rc))/mesh%spacing(1:space%dim)) - 1
231 nmax(1:space%dim) = int((center(1:space%dim) + abs(rc))/mesh%spacing(1:space%dim)) + 1
235 nmin(1:space%dim) = max(mesh%idx%nr(1, 1:space%dim), nmin(1:space%dim))
236 nmax(1:space%dim) = min(mesh%idx%nr(2, 1:space%dim), nmax(1:space%dim))
240 do iz = nmin(3), nmax(3)
241 do iy = nmin(2), nmax(2)
242 do ix = nmin(1), nmax(1)
244 if (ip == 0 .or. ip > mesh%np) cycle
245 r2 = sum((mesh%x(ip, :) - center)**2)
257 safe_allocate(this%map(1:this%np))
258 safe_allocate(xtmp(1:space%dim, 1:this%np))
259 safe_allocate(rtmp(1:this%np))
262 do iz = nmin(3), nmax(3)
263 do iy = nmin(2), nmax(2)
264 do ix = nmin(1), nmax(1)
266 if (ip == 0 .or. ip > mesh%np) cycle
270 xtmp(:, is) = mesh%x(ip, :) - center
271 rtmp(is) = norm2(xtmp(:,is))
276 safe_deallocate_a(map_inv)
289 safe_allocate(center_copies(1:space%dim, 1:latt_iter%n_cells))
290 do icell = 1, latt_iter%n_cells
291 center_copies(:, icell) = center + latt_iter%get(icell)
296 rc_norm_n = product(ceiling(rc / mesh%spacing(1:space%dim), int64) +
m_one)
297 if (mesh%use_curvilinear) rc_norm_n = rc_norm_n / mesh%coord_system%min_mesh_scaling_product
298 max_elements_count = 3**space%dim * int(
m_pi**
floor(0.5 * space%dim) * rc_norm_n *
f_n(space%dim), int64)
300 safe_allocate(x(1:space%dim,1:mesh%np))
302 x(:,ip) = mesh%x(ip,:)
305 safe_allocate(map_temp(1:max_elements_count))
306 safe_allocate(xtmp(1:space%dim, 1:max_elements_count))
307 safe_allocate(rtmp(1:max_elements_count))
311 do icell = 1, latt_iter%n_cells
312 xx = x(:,ip) - center_copies(:, icell)
313 if(any(abs(xx)>rc+tol)) cycle
323 assert(is < huge(is))
326 safe_allocate(this%map(1:this%np))
327 this%map(1:this%np) = map_temp(1:this%np)
330 safe_deallocate_a(map_temp)
331 safe_deallocate_a(center_copies)
339 safe_deallocate_a(xtmp)
340 safe_deallocate_a(rtmp)
347 class(
space_t),
intent(in) :: space
348 real(real64),
intent(in) :: xtmp(:, :), rtmp(:)
350 integer :: ip, i_region, offset
351 integer,
allocatable :: order(:), order_new(:)
352 integer,
allocatable :: map_new(:)
353 integer,
allocatable :: np_region(:), tmp_array(:)
360 safe_allocate(order(1:this%np))
361 safe_allocate(this%rel_x(1:space%dim, 1:this%np))
362 safe_allocate(this%r(1:this%np))
369 call sort(this%map, order)
372 this%overlap = .false.
373 do ip = 1, this%np - 1
374 if (this%map(ip) == this%map(ip + 1))
then
376 this%overlap = .
true.
384 if(this%overlap)
then
388 safe_allocate(tmp_array(1:this%np))
389 safe_allocate(order_new(1:this%np))
390 safe_allocate(np_region(1:this%np))
391 safe_allocate(map_new( 1:this%np))
398 if (this%map(ip) == this%map(ip - 1))
then
399 i_region = i_region + 1
400 if (i_region > this%num_regions)
then
401 this%num_regions = i_region
402 np_region(i_region) = 0
407 tmp_array(ip) = i_region
408 np_region(i_region) = np_region(i_region) + 1
411 assert( .not.
allocated(this%regions))
414 safe_allocate(this%regions(1:this%num_regions+1))
418 if(this%num_regions > 1)
then
419 do i_region = 1, this%num_regions
420 this%regions(i_region + 1) = this%regions(i_region) + np_region(i_region)
423 this%regions(2) = this%np + 1
426 np_region(1:this%np) = 0
427 order_new(1:this%np) = -1
428 map_new(1:this%np) = -1
433 i_region = tmp_array(ip)
434 np_region(i_region) = np_region(i_region) + 1
435 offset = this%regions(i_region) - 1
436 map_new( offset + np_region(i_region) ) = this%map(ip)
437 order_new( offset + np_region(i_region) ) = order(ip)
440 order(1:this%np) = order_new(1:this%np)
441 this%map(1:this%np) = map_new(1:this%np)
443 safe_deallocate_a(tmp_array)
444 safe_deallocate_a(order_new)
445 safe_deallocate_a(np_region)
446 safe_deallocate_a(map_new)
451 safe_allocate(this%regions(1:2))
453 this%regions(2) = this%np + 1
458 this%rel_x(:, ip) = xtmp(:, order(ip))
459 this%r(ip) = rtmp(order(ip))
462 safe_deallocate_a(order)
473 class(
space_t),
intent(in) :: space
474 class(
mesh_t),
target,
intent(in) :: mesh
477 real(real64),
optional,
intent(in) :: shift(:)
481 real(real64) :: xx(space%dim), diff_centers(space%dim)
488 safe_allocate(this%center(1:space%dim))
489 this%center = sm1%center
490 this%radius = sm1%radius
494 this%overlap = sm1%overlap .or. sm2%overlap
496 diff_centers = sm1%center - sm2%center
497 if (
present(shift)) diff_centers = diff_centers - shift
504 xx = sm2%rel_x(:, ip) - diff_centers
506 if (sum(xx**2) > sm1%radius**2) is = is + 1
511 safe_allocate(this%map(1:this%np))
512 safe_allocate(this%rel_x(1:space%dim, 1:this%np))
513 safe_allocate(this%r(1:this%np))
514 this%map(1:sm1%np) = sm1%map(1:sm1%np)
515 this%rel_x(:, 1:sm1%np) = sm1%rel_x(:, 1:sm1%np)
516 this%r(1:sm1%np) = sm1%r(1:sm1%np)
521 xx = sm2%rel_x(:, ip) - diff_centers
523 if (r2 > sm1%radius**2)
then
525 this%map(is) = sm2%map(ip)
526 this%r(is) =
sqrt(r2)
527 this%rel_x(:, is) = xx
539 class(
space_t),
intent(in) :: space
540 real(real64),
intent(in) :: newcenter(:)
542 real(real64) :: xx(space%dim), diff_centers(space%dim), oldcenter(space%dim)
548 oldcenter = this%center
549 this%center = newcenter
551 diff_centers = newcenter - oldcenter
554 xx = this%rel_x(:, ip) - diff_centers
555 this%r(ip) = norm2(xx)
556 this%rel_x(:, ip) = xx
566 class(
space_t),
intent(in) :: space
567 type(
mesh_t),
target,
intent(in) :: mesh
568 real(real64),
intent(in) :: center(1:space%dim)
569 real(real64),
intent(in) :: radius
570 integer,
intent(in) :: root
573 integer :: nparray(1:3)
578 if (root /= mpi_grp%rank)
then
584 if (mpi_grp%size > 1)
then
586 if (root == mpi_grp%rank)
then
588 nparray(2) = this%num_regions
589 if (this%overlap)
then
596 call mpi_grp%bcast(nparray, 2, mpi_integer, root)
598 this%num_regions = nparray(2)
599 this%overlap = (nparray(3) == 1)
601 if (root /= mpi_grp%rank)
then
602 safe_allocate(this%map(1:this%np))
603 safe_allocate(this%rel_x(1:space%dim, 1:this%np))
604 safe_allocate(this%r(1:this%np))
605 safe_allocate(this%regions(1:this%num_regions+1))
608 call mpi_grp%bcast(this%regions(1), this%num_regions+1, mpi_integer, root)
610 if (this%np > 0)
then
611 call mpi_grp%bcast(this%map(1), this%np, mpi_integer, root)
612 call mpi_grp%bcast(this%rel_x(1, 1), this%np*space%dim, mpi_double_precision, root)
613 call mpi_grp%bcast(this%r(1), this%np, mpi_double_precision, root)
625 real(real64),
intent(in) :: radius
626 real(real64),
intent(in) :: center(:)
627 real(real64),
intent(in) :: dx
630 if (
allocated(this%center))
then
633 if (radius <= this%radius+dx*1e-6_real64 .and. all(abs(this%center - center) < 0.25*dx))
then
646 if (this%np /= -1)
then
649 safe_deallocate_a(this%center)
650 safe_deallocate_a(this%map)
651 safe_deallocate_a(this%rel_x)
652 safe_deallocate_a(this%r)
653 safe_deallocate_a(this%regions)
667 integer,
intent(out) :: map_inv(:)
673 map_inv(1:this%mesh%np_part) = 0
675 map_inv(this%map(is)) = is
685 class(
space_t),
intent(in) :: space
687 integer :: ii, jj, dd
692 if (.not. space%is_periodic())
then
694 distance = sum((sm1%center - sm2%center)**2)
695 overlap =
distance <= (1.5_real64*(sm1%radius + sm2%radius))**2
698 if (.not. overlap)
return
706 do while(ii <= sm1%np .and. jj <= sm2%np)
707 dd = sm1%map(ii) - sm2%map(jj)
710 else if (dd > 0)
then
718 if (sm1%mesh%parallel_in_domains)
then
719 call sm1%mesh%mpi_grp%allreduce_inplace(overlap, 1, mpi_logical, mpi_lor)
727 class(
space_t),
intent(in) :: space
729 integer,
allocatable :: part_np(:)
730 integer :: ipart, ind, ip
734 if (.not. this%mesh%parallel_in_domains)
then
735 this%np_global = this%np
740 safe_allocate(part_np(this%mesh%pv%npart))
742 part_np(this%mesh%pv%partno) = this%np
744 call this%mesh%allreduce(part_np)
745 this%np_global = sum(part_np)
747 safe_allocate(this%rel_x_global(1:space%dim, 1:this%np_global))
748 safe_allocate(this%part_v(1:this%np_global))
749 safe_allocate(this%global2local(1:this%np_global))
750 this%rel_x_global(1:space%dim, 1:this%np_global) =
m_zero
751 this%part_v(1:this%np_global) = 0
752 this%global2local(1:this%np_global) = 0
755 do ipart = 1, this%mesh%pv%npart
756 if (ipart == this%mesh%pv%partno)
then
758 this%rel_x_global(:, ind + ip) = this%rel_x(:, ip)
759 this%part_v(ind + ip) = this%mesh%pv%partno
760 this%global2local(ind + ip) = ip
763 ind = ind + part_np(ipart)
766 call this%mesh%allreduce(this%rel_x_global)
767 call this%mesh%allreduce(this%part_v)
768 call this%mesh%allreduce(this%global2local)
770 safe_deallocate_a(part_np)
781 safe_deallocate_a(this%rel_x_global)
783 safe_deallocate_a(this%part_v)
784 safe_deallocate_a(this%global2local)
793 complex(real64),
intent(in) :: sphi(:)
794 complex(real64),
intent(inout) :: phi(:)
795 complex(real64),
optional,
intent(in) :: factor
799 push_sub(zzdsubmesh_add_to_mesh)
801 if (
present(factor))
then
805 phi(this%map(ip)) = phi(this%map(ip)) + factor*sphi(ip)
807 if (this%np.ge.4)
then
808 do ip = m+1, this%np, 4
809 phi(this%map(ip)) = phi(this%map(ip)) + factor*sphi(ip)
810 phi(this%map(ip+1)) = phi(this%map(ip+1)) + factor*sphi(ip+1)
811 phi(this%map(ip+2)) = phi(this%map(ip+2)) + factor*sphi(ip+2)
812 phi(this%map(ip+3)) = phi(this%map(ip+3)) + factor*sphi(ip+3)
818 phi(this%map(ip)) = phi(this%map(ip)) + sphi(ip)
820 if (this%np.ge.4)
then
821 do ip = m+1, this%np, 4
822 phi(this%map(ip)) = phi(this%map(ip)) + sphi(ip)
823 phi(this%map(ip+1)) = phi(this%map(ip+1)) + sphi(ip+1)
824 phi(this%map(ip+2)) = phi(this%map(ip+2)) + sphi(ip+2)
825 phi(this%map(ip+3)) = phi(this%map(ip+3)) + sphi(ip+3)
830 pop_sub(zzdsubmesh_add_to_mesh)
836 complex(real64),
intent(in) :: sphi(:)
837 complex(real64),
intent(in) :: phi(:)
838 logical,
optional,
intent(in) :: reduce
846 if (this%mesh%use_curvilinear)
then
848 dotp = dotp + this%mesh%vol_pp(this%map(is))*phi(this%map(is))*conjg(sphi(is))
853 dotp = dotp + phi(this%map(ip))*conjg(sphi(ip))
855 if (this%np.ge.4)
then
856 do ip = m+1, this%np, 4
857 dotp = dotp + phi(this%map(ip))*conjg(sphi(ip)) &
858 + phi(this%map(ip+1))*conjg(sphi(ip+1)) &
859 + phi(this%map(ip+2))*conjg(sphi(ip+2)) &
860 + phi(this%map(ip+3))*conjg(sphi(ip+3))
863 dotp = dotp*this%mesh%vol_pp(1)
868 call this%mesh%allreduce(dotp)
878 type(
submesh_t),
target,
intent(in) :: sm
879 class(
space_t),
intent(in) :: space
880 integer,
intent(out) :: db(1:space%dim)
883 real(real64) :: chi(space%dim)
884 integer :: db_red(1:space%dim)
891 chi = sm%mesh%coord_system%from_cartesian(sm%rel_x(:, ip))
892 do idir = 1, space%dim
893 db(idir) = max(db(idir), nint(abs(chi(idir))/sm%mesh%spacing(idir) +
m_half))
897 if(sm%mesh%parallel_in_domains)
then
898 call sm%mesh%mpi_grp%allreduce(db(1), db_red(1), space%dim, mpi_integer, mpi_max)
899 db(1:space%dim) = db_red(1:space%dim)
909 type(
submesh_t),
target,
intent(inout) :: sm
910 class(
space_t),
intent(in) :: space
914 real(real64) :: chi(space%dim), shift(space%dim)
918 sm%cube_map%nmap = sm%np
920 safe_allocate(sm%cube_map%map(1:space%dim, 1:sm%cube_map%nmap))
924 chi = sm%mesh%coord_system%from_cartesian(sm%center)
925 do idir = 1, space%dim
926 shift(idir) = nint(chi(idir)/sm%mesh%spacing(idir))*sm%mesh%spacing(idir)
928 shift = sm%mesh%coord_system%to_cartesian(shift)
929 shift = shift - sm%center
931 do ip = 1, sm%cube_map%nmap
932 chi = sm%mesh%coord_system%from_cartesian(sm%rel_x(:,ip) - shift)
933 do idir = 1, space%dim
934 sm%cube_map%map(idir, ip) = nint(chi(idir)/sm%mesh%spacing(idir))
940 call accel_write_buffer(sm%cube_map%map_buffer, sm%cube_map%nmap*space%dim, sm%cube_map%map)
965 class(
batch_t),
intent(in) :: ss
966 class(
batch_t),
intent(inout) :: mm
968 integer :: ist, idim, jdim, is
972 assert(.not. mm%is_packed())
975 assert(ss%nst_linear == mm%nst_linear)
976 assert(ss%status() == mm%status())
977 assert(ss%dim == mm%dim)
979 assert(mm%nst == ss%nst)
984 jdim = min(idim, ss%dim)
987 mm%zff(this%map(is), idim, ist) = &
988 mm%zff(this%map(is), idim, ist) + ss%dff(is, jdim, ist)
1001#include "submesh_inc.F90"
1004#include "complex.F90"
1005#include "submesh_inc.F90"
This is the common interface to a sorting routine. It performs the shell algorithm,...
double sqrt(double __x) __attribute__((__nothrow__
double floor(double __x) __attribute__((__nothrow__
subroutine, public accel_release_buffer(this)
pure logical function, public accel_is_enabled()
integer, parameter, public accel_mem_read_only
This module implements batches of mesh functions.
Module implementing boundary conditions in Octopus.
real(real64), parameter, public m_two
real(real64), parameter, public m_zero
real(real64), parameter, public m_pi
some mathematical constants
complex(real64), parameter, public m_z0
real(real64), parameter, public m_half
real(real64), parameter, public m_one
This module implements the index, used for the mesh points.
subroutine, public mesh_cube_map_end(this)
This module defines the meshes, which are used in Octopus.
integer function, public mesh_local_index_from_coords(mesh, ix)
This function returns the local index of the point for a given vector of integer coordinates.
Some general things and nomenclature:
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
This module is intended to contain "only mathematical" functions and procedures.
real(real64) function, public dsm_integrate_frommesh(mesh, sm, ff, reduce)
subroutine zdsubmesh_add_to_mesh(this, sphi, phi, factor)
subroutine, public submesh_init_cube_map(sm, space)
logical function, public submesh_overlap(sm1, sm2, space)
subroutine, public zsubmesh_batch_dotp_matrix(this, mm, ss, dot, reduce)
real(real64) function, public dsm_nrm2(sm, ff, reduce)
this function returns the the norm of a vector
subroutine zzsubmesh_add_to_mesh(this, sphi, phi, factor)
subroutine, public submesh_end_global(this)
subroutine, public zsubmesh_copy_from_mesh(this, phi, sphi, conjugate)
subroutine submesh_reorder_points(this, space, xtmp, rtmp)
complex(real64) function, public zsm_integrate(mesh, sm, ff, reduce)
subroutine, public submesh_shift_center(this, space, newcenter)
subroutine, public submesh_init_box(this, space, mesh, box, center)
This subroutine creates a submesh which has the same box of the one that the user passes.
complex(real64) function zzsubmesh_to_mesh_dotp(this, sphi, phi, reduce)
real(real64) function, public dsm_integrate(mesh, sm, ff, reduce)
subroutine, public dsubmesh_batch_add_matrix(this, factor, ss, mm)
The following functions takes a batch of functions defined in submesh (ss) and adds all of them to ea...
recursive real(real64) function f_n(dims)
subroutine, public dsubmesh_copy_from_mesh(this, phi, sphi, conjugate)
real(real64) function ddsubmesh_to_mesh_dotp(this, sphi, phi, reduce)
subroutine, public submesh_merge(this, space, mesh, sm1, sm2, shift)
subroutine, public dzsubmesh_batch_add(this, ss, mm)
The following function takes a batch of functions defined in submesh (ss) and adds one of them to eac...
subroutine, public zsubmesh_batch_add(this, ss, mm)
The following function takes a batch of functions defined in submesh (ss) and adds one of them to eac...
real(real64) function, public zsm_nrm2(sm, ff, reduce)
this function returns the the norm of a vector
subroutine, public zsubmesh_batch_add_matrix(this, factor, ss, mm)
The following functions takes a batch of functions defined in submesh (ss) and adds all of them to ea...
subroutine, public submesh_end_cube_map(sm)
subroutine, public submesh_end(this)
subroutine ddsubmesh_add_to_mesh(this, sphi, phi, factor)
subroutine, public submesh_get_cube_dim(sm, space, db)
finds the dimension of a box containing the submesh
subroutine, public dsubmesh_batch_dotp_matrix(this, mm, ss, dot, reduce)
complex(real64) function, public zsm_integrate_frommesh(mesh, sm, ff, reduce)
logical function, public submesh_compatible(this, radius, center, dx)
subroutine, public submesh_build_global(this, space)
subroutine, public submesh_broadcast(this, space, mesh, center, radius, root, mpi_grp)
subroutine, public submesh_get_inv(this, map_inv)
subroutine, public dsubmesh_batch_add(this, ss, mm)
The following function takes a batch of functions defined in submesh (ss) and adds one of them to eac...
subroutine, public submesh_init(this, space, mesh, latt, center, rc)
complex(real64) function zdsubmesh_to_mesh_dotp(this, sphi, phi, reduce)
type(type_t), public type_float
type(type_t), public type_cmplx
type(type_t), public type_integer
Class defining batches of mesh functions.
The following class implements a lattice iterator. It allows one to loop over all cells that are with...
Describes mesh distribution to nodes.
This is defined even when running serial.
A submesh is a type of mesh, used for the projectors in the pseudopotentials It contains points on a ...
void distance(const int iatom, const int jatom, const double coordinates[], double *rr, double *rr2, double *rr6, double *rr7)