Octopus
local_multipoles.F90
Go to the documentation of this file.
1!! Copyright (C) 2014 M. Oliveira, J. Jornet-Somoza
2!! Copyright (C) 2020-2021 M. Oliveira, K. Lively, A. Obzhirov, I. Albar
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation; either version 2, or (at your option)
7!! any later version.
8!!
9!! This program is distributed in the hope that it will be useful,
10!! but WITHOUT ANY WARRANTY; without even the implied warranty of
11!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12!! GNU General Public License for more details.
13!!
14!! You should have received a copy of the GNU General Public License
15!! along with this program; if not, write to the Free Software
16!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17!! 02110-1301, USA.
18!!
19
20#include "global.h"
21
23 use atom_oct_m
24 use basins_oct_m
27 use box_oct_m
32 use debug_oct_m
33 use global_oct_m
36 use io_oct_m
38 use ions_oct_m
39 use kick_oct_m
40 use, intrinsic :: iso_fortran_env
41 use loct_oct_m
43 use mesh_oct_m
45 use mpi_oct_m
48 use parser_oct_m
52 use space_oct_m
56 use string_oct_m
57 use unit_oct_m
59 use utils_oct_m
61 use xc_oct_m
62
63 implicit none
64
65 private
66 public :: local_multipoles_main
67
68contains
69
70 subroutine local_multipoles_main() bind(c)
71 type local_domain_t
72 class(box_t), pointer :: box
73 character(len=500) :: clist
74 character(len=15) :: lab
75 integer :: dshape
76 logical, allocatable :: mesh_mask(:)
77 logical, allocatable :: ions_mask(:)
78 type(local_write_t) :: writ
79 end type local_domain_t
80
81 type(electrons_t), pointer :: sys
82 integer, parameter :: BADER = 9 ! should be = OPTION__OUTPUT__BADER
83
84 ! Initialize stuff
85 call global_init()
86
87 call parser_init()
88
89 call messages_init()
90
91 call io_init()
93
95 call messages_print_with_emphasis(msg="Local Domains mode", namespace=global_namespace)
97
98 call messages_experimental("oct-local_multipoles utility")
99
101
102 call calc_mode_par%set_parallelization(p_strategy_states, default = .false.)
104
105 call local_domains()
106
107 safe_deallocate_p(sys)
109 call io_end()
110 call print_date("Calculation ended on ")
111 call messages_end()
112 call parser_end()
113 call global_end()
114
115 contains
116 ! -------------
120 subroutine local_domains()
121 integer :: nd
122 type(local_domain_t), allocatable :: loc_domains(:)
123 integer :: err, iter, l_start, l_end, l_step, id
124 integer :: ia, n_spec_def, read_data, iunit, ispec
125 integer :: length, folder_index
126 real(real64) :: default_dt, dt
127 character(len=MAX_PATH_LEN) :: filename, folder, folder_default, radiifile
128 character(len=MAX_PATH_LEN) :: in_folder, restart_folder, basename, frmt, ldrestart_folder
129 character(len=15) :: lab
130 logical :: iterate, ldupdate, ldoverwrite, ldrestart, ld_need_hm
131 type(kick_t) :: kick
132 type(restart_t) :: restart, restart_ld
133
134 push_sub(local_domains)
135
136 message(1) = 'Info: Creating local domains'
137 message(2) = ''
138 call messages_info(2)
139
140 write(folder_default,'(a)') 'restart/gs/'
141
142 !%Variable LDFolder
143 !%Type string
144 !%Section Utilities::oct-local_multipoles
145 !%Description
146 !% The folder name where the density used as input file is.
147 !%End
148 call parse_variable(global_namespace, 'LDFolder', folder_default, folder)
149
150 ! Check if the folder is finished by an /
151 if (index(folder, '/', back = .true.) /= len_trim(folder)) then
152 folder = trim(folder)//'/'
153 end if
154
155 default_dt = m_zero
156 call parse_variable(global_namespace, 'TDTimeStep', default_dt, dt, unit = units_inp%time)
157 if (dt < m_zero) then
158 write(message(1),'(a)') 'Input: TDTimeStep must be positive.'
159 write(message(2),'(a)') 'Input: TDTimeStep reset to 0. Check input file.'
160 call messages_info(2)
161 end if
162
163 !%Variable LDFilename
164 !%Type string
165 !%Default 'density'
166 !%Section Utilities::oct-local_multipoles
167 !%Description
168 !% Input filename. The original filename for the density which is going to be
169 !% fragmented into domains.
170 !%End
171 call parse_variable(global_namespace, 'LDFilename', 'density', basename)
172 if (basename == " ") basename = ""
173 ! Delete the extension if present
174 length = len_trim(basename)
175 if (length > 4) then
176 if (basename(length-3:length) == '.obf') then
177 basename = trim(basename(1:length-4))
178 end if
179 end if
180
181 !%Variable LDUpdate
182 !%Type logical
183 !%Default false
184 !%Section Utilities::oct-local_multipoles
185 !%Description
186 !% Controls if the calculation of the local domains is desired at each iteration.
187 !%End
188 call parse_variable(global_namespace, 'LDUpdate', .false., ldupdate)
189
190 !%Variable LDOverWrite
191 !%Type logical
192 !%Default true
193 !%Section Utilities::oct-local_multipoles
194 !%Description
195 !% Controls whether to over-write existing files.
196 !%End
197 call parse_variable(global_namespace, 'LDOverWrite', .true., ldoverwrite)
198
199 !%Variable LDRadiiFile
200 !%Type string
201 !%Default 'default'
202 !%Section Utilities::oct-local_multipoles
203 !%Description
204 !% Full path for the radii file. If set, def_rsize will be reset to the new values.
205 !% This file should have the same format as share/PP/default.
206 !%End
207 call parse_variable(global_namespace, 'LDRadiiFile', 'default', radiifile)
208
209 if (trim(radiifile) /= "default") then
210 n_spec_def = max(0, loct_number_of_lines(string_f_to_c(radiifile)))
211 if (n_spec_def > 0) n_spec_def = n_spec_def - 1 ! First line is a comment
212
213 ! get parameters from file
214 do ia = 1, sys%ions%nspecies
215 read_data = 0
216 iunit = io_open(radiifile, global_namespace, action='read', status='old', die=.false.)
217 if (iunit /= -1) then
218 if (ia == 1) then
219 write(message(1),'(a,a)') 'Redefining def_rsize from file:', trim(radiifile)
220 call messages_info(1)
221 end if
222 read(iunit,*)
223 default_file: do ispec = 1, n_spec_def
224 read(iunit,*) lab
225 if (trim(lab) == trim(sys%ions%species(ia)%s%get_label())) then
226 select type(spec=>sys%ions%species(ia)%s)
227 class is(pseudopotential_t)
228 call read_from_default_file(iunit, read_data, spec)
229 end select
230 exit default_file
231 end if
232 end do default_file
233 call io_close(iunit)
234 else
235 write(message(1),'(a,a)') 'Octopus could not open then file:', trim(radiifile)
236 call messages_warning(1)
237 end if
238 end do
239 end if
240
241 !TODO: use standart FromSratch and %RestartOptions
242 !%Variable LDRestart
243 !%Type logical
244 !%Default false
245 !%Section Utilities::oct-local_multipoles
246 !%Description
247 !% Restart information will be read from <tt>LDRestartFolder</tt>.
248 !%End
249 call parse_variable(global_namespace, 'LDRestart', .false., ldrestart)
250
251 if (ldrestart) then
252 write(folder_default,'(a)')'ld.general'
253
254 !%Variable LDRestartFolder
255 !%Type string
256 !%Default "ld.general"
257 !%Section Utilities::oct-local_multipoles
258 !%Description
259 !% The folder name where the density used as input file is.
260 !%End
261 call parse_variable(global_namespace, 'LDRestartFolder', folder_default, ldrestart_folder)
262
263 ! Check if the folder is finished by an /
264 if (index(ldrestart_folder, '/', .true.) /= len_trim(ldrestart_folder)) then
265 write(ldrestart_folder,'(a,a1)') trim(ldrestart_folder), '/'
266 end if
267 end if
268
269
270 !%Variable LDIterateFolder
271 !%Type logical
272 !%Default false
273 !%Section Utilities::oct-local_multipoles
274 !%Description
275 !% This variable decides if a folder is going to be iterated.
276 !%End
277 call parse_variable(global_namespace, 'LDIterateFolder', .false., iterate)
278
279 !%Variable LDStart
280 !%Type integer
281 !%Default 0
282 !%Section Utilities::oct-local_multipoles
283 !%Description
284 !% The starting number of the filename or folder.
285 !%End
286 call parse_variable(global_namespace, 'LDStart', 0, l_start)
287
288 !%Variable LDEnd
289 !%Type integer
290 !%Default 0
291 !%Section Utilities::oct-local_multipoles
292 !%Description
293 !% The last number of the filename or folder.
294 !%End
295 call parse_variable(global_namespace, 'LDEnd', 0, l_end)
296
297 !%Variable LDStep
298 !%Type integer
299 !%Default 1
300 !%Section Utilities::oct-local_multipoles
301 !%Description
302 !% The padding between the filenames or folder.
303 !%End
304 call parse_variable(global_namespace, 'LDStep', 1, l_step)
305
306 message(1) = 'Info: Computing local multipoles'
307 message(2) = ''
308 call messages_info(2)
309
310 call local_init(sys%space, sys%gr, sys%ions, nd, loc_domains)
311
312 ! Starting loop over selected densities.
313 if (any(loc_domains(:)%dshape == bader)) then
314 call messages_experimental('Bader volumes in oct-local_multipoles')
315 end if
316
317 call kick_init(kick, global_namespace, sys%space, sys%kpoints, sys%st%d%ispin)
318 ld_need_hm = .false.
319 do id = 1, nd
320 call local_write_init(loc_domains(id)%writ, global_namespace, loc_domains(id)%lab, 0, dt)
321 ld_need_hm = ld_need_hm .or. local_write_check_hm(loc_domains(id)%writ)
322 end do
323
324 if (ld_need_hm) then
325 message(1) = 'Info: Local domains output includes local potential and/or local energy.'
326 call messages_info(1)
327
328 call hamiltonian_elec_epot_generate(sys%hm, global_namespace, sys%space, sys%gr, sys%ions, &
329 sys%ext_partners, sys%st, time=m_zero)
330 call sys%hm%update(sys%gr, global_namespace, sys%space, sys%ext_partners, time=m_zero)
331 end if
332
333 if (ldrestart) then
334 !TODO: check for domains & mesh compatibility
335 call restart_ld%init(global_namespace, restart_undefined, restart_type_load, sys%mc, err, &
336 dir=trim(ldrestart_folder), mesh = sys%gr)
337 call local_restart_read(sys%space, sys%gr, sys%ions, nd, loc_domains, restart_ld)
338 call restart_ld%end()
339 end if
340
341 ! Initialize the restart directory from <tt>LDFolder</tt> value.
342 ! This directory has to have the files 'grid', 'mesh' and 'lxyz.obf'
343 ! and the files that are going to be used, must be inside this folder
344 if (iterate) then
345 ! Delete the last / and find the previous /, if any
346 in_folder = folder(1:len_trim(folder)-1)
347 folder_index = index(in_folder, '/', .true.)
348 restart_folder = in_folder(1:folder_index)
349 else
350 restart_folder = folder
351 end if
352 call restart%init(global_namespace, restart_undefined, restart_type_load, sys%mc, err, &
353 dir=trim(restart_folder), mesh = sys%gr)
354
355!!$ call loct_progress_bar(-1, l_end-l_start)
356 do iter = l_start, l_end, l_step
357 if (iterate) then
358 ! Delete the last / and add the corresponding folder number
359 write(in_folder,'(a,i0.7,a)') folder(folder_index+1:len_trim(folder)-1),iter, "/"
360 write(filename, '(a,a,a)') trim(in_folder), trim(basename)
361 else
362 in_folder = "."
363 if (l_start /= l_end) then
364 ! Here, we are only considering 10 character long filenames.
365 ! Subtract the initial part given at 'ConvertFilename' from the format and pad
366 ! with zeros.
367 write(frmt,'(a,i0,a)')"(a,i0.",10-len_trim(basename),")"
368 write(filename, fmt=trim(frmt)) trim(basename), iter
369 else
370 ! Assuming filename is given complete in the 'LDFilename'
371 write(filename, '(a,a,a,a)') trim(in_folder),"/", trim(basename)
372 filename = basename
373 end if
374 end if
375 ! FIXME: there is a special function for reading the density. Why not use that?
376 ! TODO: Find the function that reads the density. Which one?
377 ! FIXME: why only real functions? Please generalize.
378 ! TODO: up to know the local_multipoles utlity acts over density functions, which are real.
379 if (err == 0) then
380 call restart%read_mesh_function(trim(filename), sys%gr, sys%st%rho(:,1), err)
381 end if
382 if (err /= 0) then
383 write(message(1),*) 'While reading density: "', trim(filename), '", error code:', err
384 call messages_fatal(1)
385 end if
386
387 ! Look for the mesh points inside local domains
388 if ((iter == l_start .and. .not. ldrestart) .or. ldupdate) then
389 call local_inside_domain(sys%space, sys%gr, sys%ions, nd, loc_domains, global_namespace, sys%st%rho(:,1))
390 call local_restart_write(global_namespace, sys%space, sys%gr, sys%mc, nd, loc_domains)
391 end if
392
393 do id = 1, nd
394 call local_write_iter(loc_domains(id)%writ, global_namespace, sys%space, loc_domains(id)%lab, loc_domains(id)%ions_mask, &
395 loc_domains(id)%mesh_mask, sys%gr, sys%st, sys%hm, sys%ks, sys%ions, &
396 sys%ext_partners, kick, iter, l_start, ldoverwrite)
397 end do
398 call loct_progress_bar(iter-l_start, l_end-l_start)
399 end do
400
401 call restart%end()
402 call kick_end(kick)
403
404 do id = 1, nd
405 call local_end(loc_domains(id))
406 end do
407 safe_deallocate_a(loc_domains)
408
409 message(1) = 'Info: Exiting local domains'
410 message(2) = ''
411 call messages_info(2)
412
413 pop_sub(local_domains)
414 end subroutine local_domains
415
416 ! ---------------------------------------------------------
419 ! ---------------------------------------------------------
420 subroutine local_init(space, mesh, ions, nd, loc_domains)
421 class(space_t), intent(in) :: space
422 class(mesh_t), intent(in) :: mesh
423 type(ions_t), intent(in) :: ions
424 integer, intent(out) :: nd
425 type(local_domain_t), allocatable, intent(out) :: loc_domains(:)
426
427 integer :: id
428 type(block_t) :: blk
429
430 push_sub(local_init)
431
432 !TODO: use same strategy used in %Species block to define variables
433 !%Variable LocalDomains
434 !%Type block
435 !%Section Utilities::oct-local_multipoles
436 !%Description
437 !% The LocalDomains are by definition part of the global grid. The domains are defined by
438 !% selecting a type shape. The domain box will be constructed using the given parameters.
439 !% A local domain could be construct by addition of several box centered on the ions.
440 !% The grid points inside this box will belong to the local domain.
441 !%
442 !% The format of this block is the following:<br>
443 !% <tt> 'Label' | Shape | %< | Shape dependencies >% </tt>
444 !% <br>The first field is the label of the domain.
445 !% Label = string with the name of the new local domain.
446 !% The second is the shape type of the box used to define the domain.
447 !% Shape = SPHERE, CYLINDER, PARALLELEPIPED, MINIMUM, BADER.
448 !% Some types may need some parameters given in the remaining fields of the row.
449 !% (the valid options are detailed below).
450 !%
451 !% <tt>%LocalDomains
452 !% <br>case (SPHERE): | rsize | %<dim origin coordinates>
453 !% <br>case (CYLINDER): | rsize | xsize | %<origin coordinates>
454 !% <br>case (PARALLELEPIPED): | %<lsize> | %<origin coordinates>
455 !% <br>case (MINIMUM): | rsize | 'center_list'
456 !% <br>case (BADER): | 'center_list'
457 !% <br>%</tt>
458 !% <br>rsize: Radius in input length units
459 !% <br>xsize: the length of the cylinder in the x-direction
460 !% <br>origin coordinates: in input length units separated by |, where the box is centered.
461 !% <br>lsize: half of the length of the parallelepiped in each direction.
462 !% <br>center_list: string containing the list of atoms in xyz file for each domain in the form "2,16-23"
463 !%End
464
465 ! First, find out if there is a LocalDomains block.
466 nd = 0
467 if (parse_block(global_namespace, 'LocalDomains', blk) == 0) then
468 nd = parse_block_n(blk)
469 end if
470
471 safe_allocate(loc_domains(1:nd))
472
473 block: do id = 1, nd
474 safe_allocate(loc_domains(id)%mesh_mask(1:mesh%np))
475 safe_allocate(loc_domains(id)%ions_mask(1:ions%natoms))
476 call parse_block_string(blk, id-1, 0, loc_domains(id)%lab)
477 call local_read_from_block(loc_domains(id), space, ions, blk, id-1, global_namespace)
478 end do block
479 call parse_block_end(blk)
480 message(1) = ''
481 call messages_info(1)
482
483 pop_sub(local_init)
484 end subroutine local_init
485
486 ! ---------------------------------------------------------
489 ! ---------------------------------------------------------
490 subroutine local_end(domain)
491 type(local_domain_t), intent(inout) :: domain
493 class(box_t), pointer :: box
494
495 push_sub(local_end)
496
497 if (domain%dshape /= bader) then
498 box => domain%box
499 safe_deallocate_p(box)
500 end if
501 call local_write_end(domain%writ)
502 safe_deallocate_a(domain%mesh_mask)
503 safe_deallocate_a(domain%ions_mask)
504
505 pop_sub(local_end)
506 end subroutine local_end
507
508 ! ---------------------------------------------------------
509 subroutine local_read_from_block(domain, space, ions, blk, row, namespace)
510 type(local_domain_t), intent(inout) :: domain
511 class(space_t), intent(in) :: space
512 type(ions_t), intent(in) :: ions
513 type(block_t), intent(in) :: blk
514 integer, intent(in) :: row
515 type(namespace_t), intent(in) :: namespace
516
517 integer :: ic, ia
518 real(real64) :: radius, center(space%dim), half_length(space%dim)
519 class(box_union_t), pointer :: minimum_box
520
521 push_sub(local_read_from_block)
522
523 call parse_block_integer(blk, row, 1, domain%dshape)
524
525 select case (domain%dshape)
526 case (minimum)
527 call parse_block_float(blk, row, 2, radius, unit = units_inp%length)
528 if (radius < m_zero) call messages_input_error(namespace, 'radius', row=row, column=2)
529 call parse_block_string(blk, row, 3, domain%clist)
530
531 minimum_box => box_union_t(space%dim)
532 do ia = 1, ions%natoms
533 if (loct_isinstringlist(ia, domain%clist)) then
534 call minimum_box%add_box(box_sphere_t(space%dim, ions%pos(:, ia), radius, namespace))
535 end if
536 end do
537 domain%box => minimum_box
538
539 ic = 0
540 do ia = 1, ions%natoms
541 if (domain%box%contains_point(ions%pos(:, ia)) .and. .not. loct_isinstringlist(ia, domain%clist)) then
542 ic = ic + 1
543 if (ic <= 20) write(message(ic),'(a,a,I0,a,a)')'Atom: ',trim(ions%atom(ia)%species%get_label()), ia, &
544 ' is inside the union box BUT not in list: ', trim(domain%clist)
545 end if
546 end do
547 if (ic > 0) then
548 if (ic > 20) ic = 20
549 call messages_warning(ic)
550 message(1) = ' THIS COULD GIVE INCORRECT RESULTS '
551 call messages_warning(1)
552 if (ic == 20) then
553 message(1) = ' AT LEAST 19 ATOMS ARE NOT PRESENT IN THE LIST'
554 call messages_warning(1)
555 end if
556 end if
557
558 case (sphere)
559 call parse_block_float(blk, row, 2, radius, unit = units_inp%length)
560 if (radius < m_zero) call messages_input_error(namespace, 'radius', row=row, column=2)
561 do ic = 1, space%dim
562 call parse_block_float(blk, row, 2 + ic, center(ic), unit = units_inp%length)
563 end do
564
565 domain%box => box_sphere_t(space%dim, center, radius, namespace)
566
567 case (cylinder)
568 call parse_block_float(blk, row, 2, radius, unit = units_inp%length)
569 if (radius < m_zero) then
570 call messages_input_error(namespace, 'radius', row=row, column=2)
571 end if
572 call parse_block_float(blk, row, 3, half_length(1), unit = units_inp%length)
573 do ic = 1, space%dim
574 call parse_block_float(blk, row, 3 + ic, center(ic), unit = units_inp%length)
575 end do
576
577 domain%box => box_cylinder_t(space%dim, center, ions%latt%rlattice, radius, half_length(1)*m_two, namespace)
578
579 case (parallelepiped)
580 do ic = 1, space%dim
581 call parse_block_float(blk, row, 1 + ic, half_length(ic), unit = units_inp%length)
582 end do
583 do ic = 1, space%dim
584 call parse_block_float(blk, row, 1 + space%dim + ic, center(ic), unit = units_inp%length)
585 end do
586
587 domain%box => box_parallelepiped_t(space%dim, center, ions%latt%rlattice, half_length*m_two, namespace)
588
589 case (bader)
590 call parse_block_string(blk, row, 2, domain%clist)
591 pop_sub(local_read_from_block)
592 return
593 end select
594
595 pop_sub(local_read_from_block)
596 end subroutine local_read_from_block
597
598 ! ---------------------------------------------------------
600 subroutine local_restart_write(namespace, space, mesh, mc, nd, loc_domains)
601 type(namespace_t), intent(in) :: namespace
602 class(space_t), intent(in) :: space
603 class(mesh_t), intent(in) :: mesh
604 type(multicomm_t), intent(in) :: mc
605 integer, intent(in) :: nd
606 type(local_domain_t), contiguous, intent(in) :: loc_domains(:)
607
608 integer :: id, ip, iunit, ierr
609 character(len=MAX_PATH_LEN) :: dirname
610 character(len=140), allocatable :: lines(:)
611 real(real64), allocatable :: ff(:,:)
612 type(restart_t) :: restart
613
614 push_sub(local_restart_write)
615
616 dirname = "./restart/ld"
617 write(message(1),'(a,a)')'Info: Writing restart info to ', trim(dirname)
618 call messages_info(1)
619
620 call restart%init(namespace, restart_undefined, restart_type_dump, mc, ierr, mesh=mesh, dir=trim(dirname))
621
622 safe_allocate(lines(1:nd+2))
623 write(lines(1),'(a,1x,i5)') 'Number of local domains =', nd
624 write(lines(2),'(a3,1x,a15,1x,a5,1x)') '#id', 'label', 'shape'
625 do id = 1, nd
626 write(lines(id+2), '(i3,1x,a15,1x,i5)') id, trim(loc_domains(id)%lab), loc_domains(id)%dshape
627 end do
628 iunit = restart%open("ldomains.info")
629 call restart%write(iunit, lines, nd+2, ierr)
630 call io_close(iunit)
631 safe_deallocate_a(lines)
632
633 safe_allocate(ff(1:mesh%np, 1))
634 ff = m_zero
635 do id = 1, nd
636 do ip = 1, mesh%np
637 if (loc_domains(id)%mesh_mask(ip)) ff(ip, 1) = ff(ip, 1) + 2**real(id, real64)
638 end do
639 end do
640 call restart%write_mesh_function("ldomains", mesh, ff(1:mesh%np, 1), ierr)
641 safe_deallocate_a(ff)
642
643 call restart%end()
644
645 pop_sub(local_restart_write)
646 end subroutine local_restart_write
647
648 ! ---------------------------------------------------------
649 subroutine local_restart_read(space, mesh, ions, nd, loc_domains, restart)
650 class(space_t), intent(in) :: space
651 class(mesh_t), intent(in) :: mesh
652 type(ions_t), intent(in) :: ions
653 integer, intent(in) :: nd
654 type(local_domain_t), contiguous, intent(inout) :: loc_domains(:)
655 type(restart_t), intent(in) :: restart
656
657 integer :: id, ip, iunit, ierr
658 character(len=31) :: line(1), tmp
659 real(real64), allocatable :: mask(:)
660
661 push_sub(local_restart_read)
662
663 message(1) = 'Info: Reading mesh points inside each local domain'
664 call messages_info(1)
665
666 safe_allocate(mask(1:mesh%np))
667
668 !Read local domain information from ldomains.info
669 iunit = restart%open("ldomains.info", status='old')
670 call restart%read(iunit, line, 1, ierr)
671 read(line(1),'(a25,1x,i5)') tmp, ierr
672 call restart%close(iunit)
673
674 call restart%read_mesh_function("ldomains", mesh, mask, ierr)
675
676 do id = 1, nd
677 loc_domains(id)%mesh_mask = .false.
678 do ip = 1 , mesh%np
679 if (bitand(int(mask(ip)), 2**id) /= 0) loc_domains(id)%mesh_mask(ip) = .true.
680 end do
681
682 !Check for atom list inside each domain
683 call local_ions_mask(loc_domains(id)%mesh_mask, ions, mesh, loc_domains(id)%ions_mask)
684 end do
685
686 safe_deallocate_a(mask)
687
688 pop_sub(local_restart_read)
689 end subroutine local_restart_read
690
691 ! ---------------------------------------------------------
692 subroutine local_inside_domain(space, mesh, ions, nd, loc_domains, namespace, ff)
693 class(space_t), intent(in) :: space
694 class(mesh_t), intent(in) :: mesh
695 type(ions_t), intent(in) :: ions
696 integer, intent(in) :: nd
697 type(local_domain_t), contiguous, intent(inout) :: loc_domains(:)
698 type(namespace_t), intent(in) :: namespace
699 real(real64), contiguous, intent(in) :: ff(:)
700
701 integer(int64) :: how
702 integer :: id, ip, ierr
703 type(basins_t) :: basins
704 character(len=MAX_PATH_LEN) :: filename
705 real(real64), allocatable :: dble_domain_map(:), domain_mesh(:)
706
707 push_sub(local_inside_domain)
708
709 !TODO: find a parallel algorithm to perform the charge-density fragmentation.
710 message(1) = 'Info: Assigning mesh points inside each local domain'
711 call messages_info(1)
712
713 ! If we have Bader domains, then we need to get the basins
714 if (any(loc_domains(:)%dshape == bader)) then
715 if (mesh%parallel_in_domains) then
716 write(message(1),'(a)') 'Bader volumes can only be computed in serial'
717 call messages_fatal(1)
718 end if
719 call create_basins(namespace, space, mesh, ions, ff, basins)
720 end if
722 do id = 1, nd
723 ! Create the mask that tells which mesh points are inside the domain
724 select case (loc_domains(id)%dshape)
725 case (bader)
726 call bader_domain_create_mask(loc_domains(id), basins, mesh, ions)
727 case default
728 call box_domain_create_mask(loc_domains(id), mesh)
729 end select
730
731 !Check for atom list inside each domain
732 call local_ions_mask(loc_domains(id)%mesh_mask, ions, mesh, loc_domains(id)%ions_mask)
733 end do
734
735 if (any(loc_domains(:)%dshape == bader)) then
736 call basins_end(basins)
737 end if
738
739 if (debug%info) then
740 call parse_variable(namespace, 'LDOutputFormat', 0, how)
741 if (.not. varinfo_valid_option('OutputFormat', how, is_flag=.true.)) then
742 call messages_input_error(namespace, 'LDOutputFormat')
743 end if
744
745 ! Write extra information about domains
746 safe_allocate(dble_domain_map(1:mesh%np))
747 safe_allocate(domain_mesh(1:mesh%np))
748
749 domain_mesh = m_zero
750 do id = 1, nd
751 dble_domain_map = m_zero
752 do ip = 1, mesh%np
753 if (loc_domains(id)%mesh_mask(ip)) then
754 dble_domain_map(ip) = real(id, real64)
755 domain_mesh(ip) = domain_mesh(ip) + dble_domain_map(ip)
756 end if
757 end do
758
759 write(filename,'(a,a)') 'domain.', trim(loc_domains(id)%lab)
760 call dio_function_output(how, 'local.general', trim(filename), namespace, space, mesh, dble_domain_map, unit_one, ierr, &
761 pos=ions%pos, atoms=ions%atom)
762 end do
763
764 call dio_function_output(how, 'local.general', 'domain.mesh', namespace, space, mesh, domain_mesh, unit_one, ierr, &
765 pos=ions%pos, atoms=ions%atom)
766
767 safe_deallocate_a(dble_domain_map)
768 safe_deallocate_a(domain_mesh)
769 end if
770
771 pop_sub(local_inside_domain)
772 end subroutine local_inside_domain
773
774 ! ---------------------------------------------------------
775 subroutine create_basins(namespace, space, mesh, ions, ff, basins)
776 type(namespace_t), intent(in) :: namespace
777 class(space_t), intent(in) :: space
778 class(mesh_t), intent(in) :: mesh
779 type(ions_t), intent(in) :: ions
780 real(real64), contiguous, intent(in) :: ff(:)
781 type(basins_t), intent(inout) :: basins
782
783 logical :: use_atomic_radii
784 integer :: ip, ierr, ia, is, rankmin
785 integer(int64) :: how
786 real(real64) :: bader_threshold, dmin, dd
787 integer, allocatable :: ion_map(:)
788 real(real64), allocatable :: ff2(:,:), ffs(:)
789
790 push_sub(create_basins)
791
792 !%Variable LDBaderThreshold
793 !%Type float
794 !%Default 0.01
795 !%Section Utilities::oct-local_multipoles
796 !%Description
797 !% This variable sets the threshold for the basins calculations. Recommended values:
798 !% 0.01 -> intramolecular volumes; 0.2 -> intermolecular volumes.
799 !%End
800 call parse_variable(namespace, 'LDBaderThreshold', 0.01_real64, bader_threshold)
801
802 ! Copy function used to construct the basins, as we need to modify it
803 safe_allocate(ff2(1:mesh%np,1))
804 ff2(1:mesh%np,1) = ff(1:mesh%np)
805
806 ! Add long range density from atoms
807 safe_allocate(ffs(1:mesh%np))
808 do ia = 1, ions%natoms
809 call species_get_long_range_density(ions%atom(ia)%species, ions%namespace, ions%space, ions%latt, &
810 ions%pos(:, ia), mesh, ffs)
811 do is = 1, ubound(ff2, dim=2)
812 call lalg_axpy(mesh%np, -m_one, ffs, ff2(:, is))
813 end do
814 end do
815 safe_deallocate_a(ffs)
816
817 call basins_init(basins, namespace, mesh)
818 call basins_analyze(basins, namespace, mesh, ff2(:,1), ff2, bader_threshold)
819
820 if (debug%info) then
821 call parse_variable(namespace, 'LDOutputFormat', 0, how)
822 if (.not. varinfo_valid_option('OutputFormat', how, is_flag=.true.)) then
823 call messages_input_error(namespace, 'LDOutputFormat')
824 end if
825
826 call dio_function_output(how, 'local.general', 'basinsmap', namespace, space, mesh, real(basins%map(1:mesh%np), real64) , &
827 unit_one, ierr, pos=ions%pos, atoms=ions%atom)
828 call dio_function_output(how, 'local.general', 'dens_ff2', namespace, space, mesh, ff2(:,1), &
829 unit_one, ierr, pos=ions%pos, atoms=ions%atom)
830 end if
831 safe_deallocate_a(ff2)
832
833 !%Variable LDUseAtomicRadii
834 !%Type logical
835 !%Default false
836 !%Section Utilities::oct-local_multipoles
837 !%Description
838 !% If set, atomic radii will be used to assign lone pairs to ion.
839 !%End
840 call parse_variable(global_namespace, 'LDUseAtomicRadii', .false., use_atomic_radii)
841
842 if (use_atomic_radii) then
843 safe_allocate(ion_map(1:ions%natoms))
844 ion_map = 0
845 do ia = 1, ions%natoms
846 ion_map(ia) = basins%map(mesh_nearest_point(mesh, ions%pos(:, ia), dmin, rankmin))
847 end do
848
849 do ip = 1, mesh%np
850 ! Check if lonely pair has no ions assigned to it
851 if (all(ion_map(:) /= basins%map(ip))) then
852 ! Assign lonely pair to ion in a atomic radii distance
853 do ia = 1, ions%natoms
854 dd = norm2(mesh%x(:, ip) - ions%pos(:, ia))
855 if (dd <= ions%atom(ia)%species%get_vdw_radius()) basins%map(ip) = ion_map(ia)
856 end do
857 end if
858 end do
859
860 safe_deallocate_a(ion_map)
861 end if
862
863 pop_sub(create_basins)
864 end subroutine create_basins
865
866 ! ---------------------------------------------------------
867 subroutine box_domain_create_mask(domain, mesh)
868 class(local_domain_t), intent(inout) :: domain
869 class(mesh_t), intent(in) :: mesh
870
871 push_sub(box_domain_create_mask)
872
873 domain%mesh_mask = domain%box%contains_points(mesh%np, mesh%x_t)
874
876 end subroutine box_domain_create_mask
877
878 ! ---------------------------------------------------------
879 subroutine bader_domain_create_mask(domain, basins, mesh, ions)
880 class(local_domain_t), intent(inout) :: domain
881 type(basins_t), intent(in) :: basins
882 class(mesh_t), intent(in) :: mesh
883 type(ions_t), intent(in) :: ions
884
885 integer :: ia, ib, ip, ix, n_basins, rankmin
886 real(real64) :: dmin
887 integer, allocatable :: domain_map(:)
888
890
891 ! Count then number of basins to consider. That is the number of ions specified in the input by the user
892 n_basins = 0
893 do ia = 1, ions%natoms
894 if (loct_isinstringlist(ia, domain%clist)) n_basins = n_basins + 1
895 end do
896
897 safe_allocate(domain_map(1:n_basins))
898
899 ! Assign basins to ions
900 domain_map = 0
901 ib = 0
902 do ia = 1, ions%natoms
903 if (.not. loct_isinstringlist(ia, domain%clist)) cycle
904 ib = ib + 1
905 ix = mesh_nearest_point(mesh, ions%pos(:, ia), dmin, rankmin)
906 domain_map(ib) = basins%map(ix)
907 end do
908
909 ! Now construct the mask: a point belongs to this Bader domain if it is part
910 ! of at least one basin that is part of this domain
911 do ip = 1, mesh%np
912 domain%mesh_mask(ip) = any(domain_map(1:n_basins) == basins%map(ip))
913 end do
914
915 safe_deallocate_a(domain_map)
916
918 end subroutine bader_domain_create_mask
919
920 ! ---------------------------------------------------------
921 !Check for the ions inside each local domain.
922 subroutine local_ions_mask(mesh_mask, ions, mesh, ions_mask)
923 logical, contiguous, intent(in) :: mesh_mask(:)
924 type(ions_t), intent(in) :: ions
925 class(mesh_t), intent(in) :: mesh
926 logical, contiguous, intent(out) :: ions_mask(:)
927
928 integer :: ia, ix
929 integer, allocatable :: mask_tmp(:)
930 integer :: rankmin
931 real(real64) :: dmin
932
933 push_sub(local_ions_mask)
934
935 safe_allocate(mask_tmp(1:ions%natoms))
936 mask_tmp = 0
937
938 do ia = 1, ions%natoms
939 ix = mesh_nearest_point(mesh, ions%pos(:, ia), dmin, rankmin)
940 if (rankmin /= mesh%mpi_grp%rank) cycle
941 if (mesh_mask(ix)) mask_tmp(ia) = 1
942 end do
943
944 call mesh%allreduce(mask_tmp, ions%natoms)
945 ions_mask = mask_tmp == 1
946
947 safe_deallocate_a(mask_tmp)
948
949 pop_sub(local_ions_mask)
950 end subroutine local_ions_mask
953
954!! Local Variables:
955!! mode: f90
956!! coding: utf-8
957!! End:
constant times a vector plus a vector
Definition: lalg_basic.F90:171
subroutine bader_domain_create_mask(domain, basins, mesh, ions)
subroutine local_restart_write(namespace, space, mesh, mc, nd, loc_domains)
Write restart files for local domains.
subroutine local_init(space, mesh, ions, nd, loc_domains)
Initialize local_domain_t variable, allocating variable and reading parameters from input file.
subroutine create_basins(namespace, space, mesh, ions, ff, basins)
subroutine box_domain_create_mask(domain, mesh)
subroutine local_inside_domain(space, mesh, ions, nd, loc_domains, namespace, ff)
subroutine local_ions_mask(mesh_mask, ions, mesh, ions_mask)
subroutine local_restart_read(space, mesh, ions, nd, loc_domains, restart)
subroutine local_read_from_block(domain, space, ions, blk, row, namespace)
subroutine local_domains()
Reads a global grid and density and make local domains This is a high-level interface that reads the ...
subroutine local_end(domain)
Ending local_domain_t variable, allocating variable and reading parameters from input file.
subroutine, public basins_init(this, namespace, mesh)
Definition: basins.F90:130
subroutine, public basins_analyze(this, namespace, mesh, f, rho, threshold)
Definition: basins.F90:167
subroutine, public basins_end(this)
Definition: basins.F90:149
Module, implementing a factory for boxes.
Definition: box_factory.F90:99
integer, parameter, public sphere
integer, parameter, public minimum
integer, parameter, public parallelepiped
integer, parameter, public cylinder
This module handles the calculation mode.
type(calc_mode_par_t), public calc_mode_par
Singleton instance of parallel calculation mode.
integer, parameter, public p_strategy_states
parallelization in states
type(debug_t), save, public debug
Definition: debug.F90:133
real(real64), parameter, public m_two
Definition: global.F90:175
subroutine, public global_end()
Finalise parser varinfo file, and MPI.
Definition: global.F90:500
real(real64), parameter, public m_zero
Definition: global.F90:173
subroutine, public global_init(communicator)
Initialise Octopus.
Definition: global.F90:381
real(real64), parameter, public m_one
Definition: global.F90:174
subroutine, public hamiltonian_elec_epot_generate(this, namespace, space, gr, ions, ext_partners, st, time)
subroutine, public dio_function_output(how, dir, fname, namespace, space, mesh, ff, unit, ierr, pos, atoms, grp, root)
Top-level IO routine for functions defined on the mesh.
Definition: io.F90:93
subroutine, public io_init(defaults)
If the argument defaults is present and set to true, then the routine will not try to read anything f...
Definition: io.F90:142
subroutine, public io_close(iunit, grp)
Definition: io.F90:444
subroutine, public io_end()
Definition: io.F90:248
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:379
subroutine, public kick_end(kick)
Definition: kick.F90:776
subroutine, public kick_init(kick, namespace, space, kpoints, nspin)
Definition: kick.F90:202
subroutine, public local_multipoles_main()
subroutine, public local_write_init(writ, namespace, lab, iter, dt)
subroutine, public local_write_end(writ)
logical function, public local_write_check_hm(writ)
subroutine, public local_write_iter(writ, namespace, space, lab, ions_mask, mesh_mask, mesh, st, hm, ks, ions, ext_partners, kick, iter, l_start, ldoverwrite)
System information (time, memory, sysname)
Definition: loct.F90:94
logical function, public loct_isinstringlist(a, s)
Definition: loct.F90:265
subroutine, public loct_progress_bar(a, maxcount)
A wrapper around the progress bar, such that it can be silenced without needing to dress the call wit...
Definition: loct.F90:253
character(kind=c_char, len=1) function, dimension(len_trim(f_string)+1), private string_f_to_c(f_string)
convert a Fortran string to a C string
Definition: loct.F90:217
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
integer function, public mesh_nearest_point(mesh, pos, dmin, rankmin)
Returns the index of the point which is nearest to a given vector position pos.
Definition: mesh.F90:368
subroutine, public messages_end()
Definition: messages.F90:249
subroutine, public messages_print_with_emphasis(msg, iunit, namespace)
Definition: messages.F90:878
character(len=512), private msg
Definition: messages.F90:143
subroutine, public messages_init(output_dir)
Definition: messages.F90:196
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:501
subroutine, public print_date(str)
Definition: messages.F90:963
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:386
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:671
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1020
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:570
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:317
This module handles the communicators for the various parallelization strategies.
Definition: multicomm.F90:124
type(namespace_t), public global_namespace
Definition: namespace.F90:112
subroutine, public parse_block_string(blk, l, c, res, convert_to_c)
Definition: parser.F90:795
subroutine, public parser_init()
Initialise the Octopus parser.
Definition: parser.F90:387
subroutine, public parser_end()
End the Octopus parser.
Definition: parser.F90:419
integer function, public parse_block(namespace, name, blk, check_varinfo_)
Definition: parser.F90:600
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
subroutine, public profiling_end(namespace)
Definition: profiling.F90:455
subroutine, public profiling_init(namespace)
Create profiling subdirectory.
Definition: profiling.F90:264
subroutine, public read_from_default_file(iunit, read_data, spec)
integer, parameter, public restart_undefined
Definition: restart.F90:133
integer, parameter, public restart_type_dump
Definition: restart.F90:161
integer, parameter, public restart_type_load
Definition: restart.F90:161
subroutine, public species_get_long_range_density(species, namespace, space, latt, pos, mesh, rho, sphere_inout, nlr_x)
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:111
This module defines the unit system, used for input and output.
subroutine, public unit_system_init(namespace)
type(unit_system_t), public units_inp
the units systems for reading and writing
type(unit_t), public unit_one
some special units required for particular quantities
This module is intended to contain simple general-purpose utility functions and procedures.
Definition: utils.F90:99
subroutine, public print_header()
This subroutine prints the logo followed by information about the compilation and the system....
Definition: utils.F90:290
Definition: xc.F90:97
Class implementing a cylinder box. The cylinder axis is always along the first direction defined by t...
class to tell whether a point is inside or outside
Definition: box.F90:120
Class implementing a parallelepiped box. Currently this is restricted to a rectangular cuboid (all th...
Class implementing a spherical box.
Definition: box_sphere.F90:111
Class implementing a box that is an union other boxes.
Definition: box_union.F90:111
Class describing the electron system.
Definition: electrons.F90:202
Describes mesh distribution to nodes.
Definition: mesh.F90:165
Stores all communicators and groups.
Definition: multicomm.F90:185
int true(void)