Octopus
mix.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2014 M. Marques, A. Castro, A. Rubio, G. Bertsch, M. Oliveira
2!! Copyright (C) 2024 A. Buccheri
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
22module mix_oct_m
23 use debug_oct_m
25 use global_oct_m
26 use, intrinsic :: iso_fortran_env
29 use math_oct_m
30 use mesh_oct_m
35 use parser_oct_m
40 use space_oct_m
42 use types_oct_m
44
45 implicit none
46
47 private
48 public :: &
49 mix_t, &
50 mix_init, &
51 mix_clear, &
52 mix_end, &
53 mix_dump, &
54 mix_load, &
55 mixing, &
56 dmixing, &
57 zmixing, &
59 mix_scheme, &
60 mix_d3, &
62 mixfield_t, &
70
71 ! A better design would be to have mixfield_t and mixfield_ptr_t separate from mix_t:
72 ! mix_t would contain constants, and should be immutable with only getters.
73 ! mixfield_type encapsulates the data used in mixing, and has bound procedures for
74 ! operating on this data.
75
77 type mixfield_t
78 private
79 ! Real versions of the data
80 real(real64), allocatable :: ddf(:, :, :)
81 real(real64), allocatable :: ddv(:, :, :)
82 real(real64), allocatable :: df_old(:, :)
83 real(real64), allocatable :: dvin_old(:, :)
84 real(real64), allocatable :: dvin(:, :)
85 real(real64), allocatable :: dvout(:, :)
86 real(real64), allocatable :: dvnew(:, :)
87 real(real64), allocatable :: dresidual(:, :)
88
89 ! Complex versions of the data
90 complex(real64), allocatable :: zdf(:, :, :)
91 complex(real64), allocatable :: zdv(:, :, :)
92 complex(real64), allocatable :: zf_old(:, :)
93 complex(real64), allocatable :: zvin_old(:, :)
94 complex(real64), allocatable :: zvin(:, :)
95 complex(real64), allocatable :: zvout(:, :)
96 complex(real64), allocatable :: zvnew(:, :)
97 complex(real64), allocatable :: zresidual(:, :)
98
99 type(type_t) :: func_type
100 integer :: d1, d2, d3
101 ! d1 is mesh%np, d2 vary depending on where this called
102 ! d3 is for SCF history of a quantity.
103 logical :: mix_spin_density_matrix
104 real(real64) :: last_residue
105 end type mixfield_t
106
107 type mixfield_ptr_t
108 private
109 type(mixfield_t), pointer :: p
110 end type mixfield_ptr_t
111
112 integer, parameter :: MAX_AUXMIXFIELD = 5
113
121 type mix_t
122 private
123 type(derivatives_t), pointer :: der
124
125 integer :: scheme
126 real(real64), allocatable :: coeff(:)
127 real(real64) :: adaptive_damping
128 integer :: n_backtrack = 0
129 real(real64) :: residual_coeff
130 integer :: iter
131 ! !< include the iterations done in previous calculations.
132 integer, public :: ns
133 integer, public :: ns_restart
134 integer :: interval
135
136 integer :: last_ipos
137 ! This should have been an attribute of type(mixfield_t)
138
139 ! Fields
140 type(mixfield_t) :: mixfield
141 integer :: nauxmixfield
142 type(mixfield_ptr_t) :: auxmixfield(MAX_AUXMIXFIELD)
143 integer :: ipos
144 ! This should have been an attribute of type(mixfield_ptr_t)
145
146 ! Preconditioning
147 logical :: kerker
148 real(real64) :: kerker_factor
149 logical :: precondition
150 type(nl_operator_t) :: preconditioner
151 ! [GPAW](https://wiki.fysik.dtu.dk/gpaw/documentation/densitymix/densitymix.html)
152
153 logical :: mix_spin_density_matrix
154 contains
155 procedure :: compute_residuals_aux_field
156
157 end type mix_t
158
159 interface mixfield_set_vin
160 module procedure dmixfield_set_vin, zmixfield_set_vin
161 end interface mixfield_set_vin
162
163 interface mixfield_set_vout
164 module procedure dmixfield_set_vout, zmixfield_set_vout
165 end interface mixfield_set_vout
166
167 interface mixfield_get_vnew
168 module procedure dmixfield_get_vnew, zmixfield_get_vnew
169 end interface mixfield_get_vnew
170
171contains
174 subroutine mix_init(smix, namespace, space, der, d1, d2, def_, func_type_, prefix_)
175 type(mix_t), intent(out) :: smix
176 type(namespace_t), intent(in) :: namespace
177 class(space_t), intent(in) :: space
178 type(derivatives_t), target, intent(in) :: der
179 integer, intent(in) :: d1
180 integer, intent(in) :: d2
181 integer, optional, intent(in) :: def_
182 type(type_t), optional, intent(in) :: func_type_
183 character(len=*), optional, intent(in) :: prefix_
184
185 integer :: def, ii
186 character(len=32) :: prefix
187 type(type_t) :: func_type
188 real(real64) :: coeff
190 push_sub(mix_init)
192 smix%der => der
193
194 def = option__mixingscheme__broyden
195 if (present(def_)) def = def_
196 if (present(func_type_)) then
197 func_type = func_type_
198 else
199 func_type = type_float
200 end if
201 prefix = ''
202 if (present(prefix_)) prefix = prefix_
203
204 call messages_obsolete_variable(namespace, 'TypeOfMixing', 'MixingScheme')
205
206 !%Variable MixingScheme
207 !%Type integer
208 !%Default broyden
209 !%Section SCF::Mixing
210 !%Description
211 !% The scheme used to produce, at each iteration in the self-consistent cycle
212 !% that attempts to solve the Kohn-Sham equations, the input density from the value
213 !% of the input and output densities of previous iterations.
214 !%Option linear 0
215 !% Simple linear mixing.
216 !%Option broyden 2
217 !% Broyden scheme [C. G Broyden, <i>Math. Comp.</i> <b>19</b>, 577 (1965);
218 !% D. D. Johnson, <i>Phys. Rev. B</i> <b>38</b>, 12807 (1988)].
219 !% The scheme is slightly adapted, see the comments in the code.
220 !% For complex functions (e.g. Sternheimer with <tt>EMEta</tt> > 0), we use the generalization
221 !% with a complex dot product.
222 !%Option diis 9
223 !% Direct inversion in the iterative subspace (diis)
224 !% scheme [P. Pulay, <i>Chem. Phys. Lett.</i>, <b>73</b>, 393
225 !% (1980)] as described in [G. Kresse, and J. Furthmueller,
226 !% <i>Phys. Rev. B</i> <b>54</b>, 11169 (1996)].
227 !%Option broyden_adaptive 11
228 !% Broyden mixing with adaptive damping. The Broyden update follows
229 !% [C. G. Broyden, <i>Math. Comp.</i> <b>19</b>, 577 (1965);
230 !% D. D. Johnson, <i>Phys. Rev. B</i> <b>38</b>, 12807 (1988)].
231 !% The adaptive damping logic is inspired by
232 !% [M. F. Herbst and A. Levitt, <i>J. Comput. Phys.</i> <b>459</b>, 111127 (2022)]
233 !% and by the adaptive-coefficient analysis of
234 !% [M. Novak et al., <i>Comput. Phys. Commun.</i> <b>292</b>, 108865 (2023)].
235 !%End
236 call parse_variable(namespace, trim(prefix)//'MixingScheme', def, smix%scheme)
237 if (.not. varinfo_valid_option('MixingScheme', smix%scheme)) then
238 call messages_input_error(namespace, 'MixingScheme', 'invalid option')
239 end if
240 call messages_print_var_option("MixingScheme", smix%scheme, namespace=namespace)
241
242 !%Variable MixingPreconditioner
243 !%Type logical
244 !%Default false
245 !%Section SCF::Mixing
246 !%Description
247 !% (Experimental) If set to yes, Octopus will use a preconditioner
248 !% for the mixing operator.
249 !% This preconditioner is disabled for systems with dimension other than 3.
250 !%End
251 call parse_variable(namespace, trim(prefix)+'MixingPreconditioner', .false., smix%precondition)
252 if (der%dim /= 3) smix%precondition = .false.
253 if (smix%precondition) call messages_experimental('MixingPreconditioner', namespace=namespace)
255 !%Variable Mixing
256 !%Type float
257 !%Default 0.3
258 !%Section SCF::Mixing
259 !%Description
260 !% The linear, Broyden, adaptive Broyden, and DIIS scheme depend on a "mixing parameter", set by this variable.
261 !% Must be 0 < <tt>Mixing</tt> <= 1.
262 !% For adaptive Broyden this is the maximum damping, which may be reduced automatically.
263 !%End
264 call parse_variable(namespace, trim(prefix)+'Mixing', 0.3_real64, coeff)
265 if (coeff <= m_zero .or. coeff > m_one) then
266 call messages_input_error(namespace, 'Mixing', 'Value should be positive and smaller than one')
267 end if
268 safe_allocate(smix%coeff(1:d2))
269 smix%coeff = coeff
270 smix%adaptive_damping = m_one
271 smix%n_backtrack = 0
272
273 if (d2 > 1) then
274 !%Variable MixingSpinDensityMatrix
275 !%Type logical
276 !%Default false
277 !%Section SCF::Mixing
278 !%Description
279 !% If set to yes, Octopus mixes the components up, down, and real and imaginary parts of
280 !% the up down (for spinor calculations) of the density or potential.
281 !% By default, Octopus mixes the magnetization and the charge density with a different weight,
282 !% where the magnetization mixing coefficient is given by <tt>MixingMagnetization</tt>
283 !%End
284 call parse_variable(namespace, trim(prefix)+'MixingSpinDensityMatrix', .false., smix%mix_spin_density_matrix)
285
286 !%Variable MixingMagnetization
287 !%Type float
288 !%Default 1.5
289 !%Section SCF::Mixing
290 !%Description
291 !% Same as <tt>Mixing</tt>, but for the "magnetization".
292 !% The default value is taken from D. D. Johnson, <i>Phys. Rev. B</i> <b>38</b>, 12807 (1988).
293 !%End
294 call parse_variable(namespace, trim(prefix)+'MixingMagnetization', 1.5_real64, coeff)
295
296 if (.not. smix%mix_spin_density_matrix) smix%coeff(2:d2) = coeff
297 else
298 smix%mix_spin_density_matrix = .true.
299 end if
300
301 !%Variable MixingResidual
302 !%Type float
303 !%Default 0.05
304 !%Section SCF::Mixing
305 !%Description
306 !% In the DIIS mixing it is benefitial to include a bit of
307 !% residual into the mixing. This parameter controls this amount.
308 !%End
309 call parse_variable(namespace, trim(prefix)+'MixingResidual', 0.05_real64, smix%residual_coeff)
310 if (smix%residual_coeff <= m_zero .or. smix%residual_coeff > m_one) then
311 call messages_input_error(namespace, 'MixingResidual', 'Value should be positive and smaller than one.')
312 end if
313
314 !%Variable MixNumberSteps
315 !%Type integer
316 !%Default 4
317 !%Section SCF::Mixing
318 !%Description
319 !% In the Broyden scheme, the new input density or potential is constructed
320 !% from the values of the densities/potentials of a given number of previous iterations.
321 !% This number is set by this variable. Must be greater than 1.
322 !%End
323 if (smix%scheme /= option__mixingscheme__linear) then
324 call parse_variable(namespace, trim(prefix)//'MixNumberSteps', 4, smix%ns)
325 if (smix%ns <= 1) call messages_input_error(namespace, 'MixNumberSteps')
326 else
327 smix%ns = 0
328 end if
329
330 !%Variable MixingRestart
331 !%Type integer
332 !%Default 20
333 !%Section SCF::Mixing
334 !%Description
335 !% In the Broyden scheme, the mixing is restarted after
336 !% the number of iterations given by this variable.
337 !% Set this to zero to disable restarting the mixing.
338 !%End
339 if (smix%scheme /= option__mixingscheme__linear) then
340 call parse_variable(namespace, trim(prefix)//'MixingRestart', 20, smix%ns_restart)
341 if (smix%ns_restart < 0) call messages_input_error(namespace, 'MixingRestart')
342 else
343 smix%ns_restart = 0
344 end if
345
346 write(message(1), '(A,I4,A,I4,A)') "Info: Mixing uses ", smix%ns, " steps and restarts after ", &
347 smix%ns_restart, " steps."
348 call messages_info(1, namespace=namespace)
349
350 !%Variable MixInterval
351 !%Type integer
352 !%Default 1
353 !%Section SCF::Mixing
354 !%Description
355 !% When this variable is set to a value different than 1 (the
356 !% default) a combined mixing scheme will be used, with MixInterval
357 !% - 1 steps of linear mixing followed by 1 step of the selected
358 !% mixing. For the moment this variable only works with DIIS mixing.
359 !%End
360 call parse_variable(namespace, trim(prefix)//'MixInterval', 1, smix%interval)
361 if (smix%interval < 1) call messages_input_error(namespace, 'MixInterval', 'MixInterval must be larger or equal than 1')
362
363 smix%iter = 0
364
365 !%Variable MixingKerker
366 !%Type logical
367 !%Default false
368 !%Section SCF::Mixing
369 !%Description
370 !% (Experimental) If set to yes, Octopus will use the Kerker preconditioner
371 !% for the mixing operator applied in the linear and Broyden mixing schemes.
372 !% When using preconditioning with the Broyden scheme, the user should increase the <tt>Mixing</tt> from its default
373 !% to between 0.5 and 0.8, for optimal results.
374 !% The implementation follows Yoshinori Shiihara et al., Modelling Simul. Mater. Sci. Eng. 16 (2008), 035004
375 !%End
376 call parse_variable(namespace, trim(prefix)+'MixingKerker', .false., smix%kerker)
377 if(smix%kerker) call messages_experimental('MixingKerker')
378
379 !%Variable MixingKerkerFactor
380 !%Type float
381 !%Default 1.0
382 !%Section SCF::Mixing
383 !%Description
384 !% The screening factor, $q_0$ in the Kerker preconditioner in units of inverse length.
385 !% For small wave vectors, q, screening behaves like $\frac{Aq}{q_0}$, where A is the mixing coefficient.
386 !% An optimal value is determined empirically for a given system and mixing coeffient, however the default
387 !% is a reasonable choice in many cases.
388 !%End
389 call parse_variable(namespace, trim(prefix)+'MixingKerkerFactor', 1._real64, smix%kerker_factor)
390
391 smix%nauxmixfield = 0
392 do ii = 1,max_auxmixfield
393 nullify(smix%auxmixfield(ii)%p)
394 end do
395
396 call mixfield_init(smix, smix%mixfield, d1, d2, smix%ns, func_type)
397 call mix_clear(smix)
398 if (smix%precondition) call init_preconditioner()
399
400 pop_sub(mix_init)
401
402 contains
403
404 subroutine init_preconditioner()
405
406 integer :: ns, maxp, ip, is
407 real(real64), parameter :: weight = 50.0_real64
408
409 ! This the mixing preconditioner from GPAW:
410 !
411 ! https://wiki.fysik.dtu.dk/gpaw/documentation/densitymix/densitymix.html
412 !
413
414 assert(.not. der%mesh%use_curvilinear)
415 assert(der%dim == 3)
416
417 call nl_operator_init(smix%preconditioner, "Mixing preconditioner")
418 call stencil_cube_get_lapl(smix%preconditioner%stencil, der%dim, 1)
419 call nl_operator_build(space, der%mesh, smix%preconditioner, der%mesh%np, const_w = .not. der%mesh%use_curvilinear)
420
421 ns = smix%preconditioner%stencil%size
422
423 if (smix%preconditioner%const_w) then
424 maxp = 1
425 else
426 maxp = der%mesh%np
427 end if
428
429 do ip = 1, maxp
430
431 do is = 1, ns
432 select case (sum(abs(smix%preconditioner%stencil%points(1:der%dim, is))))
433 case (0)
434 smix%preconditioner%w(is, ip) = m_one + weight/8.0_real64
435 case (1)
436 smix%preconditioner%w(is, ip) = weight/16.0_real64
437 case (2)
438 smix%preconditioner%w(is, ip) = weight/32.0_real64
439 case (3)
440 smix%preconditioner%w(is, ip) = weight/64.0_real64
441 case default
442 assert(.false.)
443 end select
444
445 end do
446 end do
447
448 call nl_operator_allocate_gpu_buffers(smix%preconditioner)
449 call nl_operator_update_gpu_buffers(smix%preconditioner)
450 call nl_operator_output_weights(smix%preconditioner)
451
452 end subroutine init_preconditioner
453
454 end subroutine mix_init
455
456
457 ! ---------------------------------------------------------
458 subroutine mix_clear(smix)
459 type(mix_t), intent(inout) :: smix
460
461 push_sub(mix_clear)
462
463 call mixfield_clear(smix%scheme, smix%mixfield)
464
465 smix%iter = 0
466 smix%last_ipos = 0
467 smix%adaptive_damping = m_one
468 smix%n_backtrack = 0
469
470 pop_sub(mix_clear)
471 end subroutine mix_clear
472
473
474 ! ---------------------------------------------------------
475 subroutine mix_end(smix)
476 type(mix_t), intent(inout) :: smix
477
478 integer :: ii
479
480 push_sub(mix_end)
481
482 if (smix%precondition) call nl_operator_end(smix%preconditioner)
483
484 call mixfield_end(smix, smix%mixfield)
485
486 smix%nauxmixfield = 0
487 do ii = 1,max_auxmixfield
488 nullify(smix%auxmixfield(ii)%p)
489 end do
490
491 safe_deallocate_a(smix%coeff)
492
493 pop_sub(mix_end)
494 end subroutine mix_end
495
496
497 ! ---------------------------------------------------------
498 subroutine mix_dump(namespace, restart, smix, mesh, ierr)
499 type(namespace_t), intent(in) :: namespace
500 class(restart_t), intent(in) :: restart
501 type(mix_t), intent(in) :: smix
502 class(mesh_t), intent(in) :: mesh
503 integer, intent(out) :: ierr
504
505 integer :: iunit, id2, id3, err, err2(4)
506 character(len=40) :: lines(7)
507 character(len=80) :: filename
508
509 push_sub(mix_dump)
510
511 ierr = 0
512
513 if (restart%skip()) then
514 pop_sub(mix_dump)
515 return
516 end if
517
518 message(1) = "Debug: Writing mixing restart."
519 call messages_info(1, namespace=namespace, debug_only=.true.)
520
521 ! functions to be written need to be compatible with the mesh
522 assert(mesh%np == smix%mixfield%d1)
523
524 ! First we write some information about the mixing
525 iunit = restart%open('mixing')
526 if(restart%do_i_write()) then
527 write(lines(1), '(a11,i1)') 'scheme= ', smix%scheme
528 ! Number of global mesh points have to be written, not only smix%d1
529 write(lines(2), '(a11,i10)') 'd1= ', mesh%np_global
530 write(lines(3), '(a11,i10)') 'd2= ', smix%mixfield%d2
531 write(lines(4), '(a11,i10)') 'd3= ', smix%mixfield%d3
532 write(lines(5), '(a11,i10)') 'iter= ', smix%iter
533 write(lines(6), '(a11,i10)') 'ns= ', smix%ns
534 write(lines(7), '(a11,i10)') 'last_ipos= ', smix%last_ipos
535 call restart%write(iunit, lines, 7, err)
536 ierr = ierr + err
537 end if
538 call restart%close(iunit)
539
540 ! Now we write the different functions.
541 ! These are not needed when using linear mixing, so we will make sure we skip this step in that case.
542 err2 = 0
543 if (smix%scheme /= option__mixingscheme__linear) then
544 do id2 = 1, smix%mixfield%d2
545 do id3 = 1, smix%mixfield%d3
546
547 write(filename,'(a3,i2.2,i2.2)') 'df_', id2, id3
548 if (smix%mixfield%func_type == type_float) then
549 call restart%write_mesh_function(filename, mesh, smix%mixfield%ddf(1:mesh%np, id2, id3), err)
550 else
551 call restart%write_mesh_function(filename, mesh, smix%mixfield%zdf(1:mesh%np, id2, id3), err)
552 end if
553 err2(1) = err2(1) + err
554
555 write(filename,'(a3,i2.2,i2.2)') 'dv_', id2, id3
556 if (smix%mixfield%func_type == type_float) then
557 call restart%write_mesh_function(filename, mesh, smix%mixfield%ddv(1:mesh%np, id2, id3), err)
558 else
559 call restart%write_mesh_function(filename, mesh, smix%mixfield%zdv(1:mesh%np, id2, id3), err)
560 end if
561 err2(2) = err2(2) + err
562
563 end do
564
565 write(filename,'(a6,i2.2)') 'f_old_', id2
566 if (smix%mixfield%func_type == type_float) then
567 call restart%write_mesh_function(filename, mesh, smix%mixfield%df_old(1:mesh%np, id2), err)
568 else
569 call restart%write_mesh_function(filename, mesh, smix%mixfield%zf_old(1:mesh%np, id2), err)
570 end if
571 err2(3) = err2(3) + err
572
573 write(filename,'(a8,i2.2)') 'vin_old_', id2
574 if (smix%mixfield%func_type == type_float) then
575 call restart%write_mesh_function(filename, mesh, smix%mixfield%dvin_old(1:mesh%np, id2), err)
576 else
577 call restart%write_mesh_function(filename, mesh, smix%mixfield%zvin_old(1:mesh%np, id2), err)
578 end if
579 err2(4) = err2(4) + err
580
581 end do
582
583 if (err2(1) /= 0) ierr = ierr + 2
584 if (err2(2) /= 0) ierr = ierr + 4
585 if (err2(3) /= 0) ierr = ierr + 8
586 if (err2(4) /= 0) ierr = ierr + 16
587 end if
588
589 message(1) = "Debug: Writing mixing restart done."
590 call messages_info(1, namespace=namespace, debug_only=.true.)
591
592 pop_sub(mix_dump)
593 end subroutine mix_dump
594
595
596 !---------------------------------------------------------
597 subroutine mix_load(namespace, restart, smix, mesh, ierr)
598 type(namespace_t), intent(in) :: namespace
599 type(restart_t), intent(in) :: restart
600 type(mix_t), intent(inout) :: smix
601 class(mesh_t), intent(in) :: mesh
602 integer, intent(out) :: ierr
603
604 integer :: iunit, err, err2(4)
605 integer :: scheme, d1, d2, d3, ns
606 integer :: id2, id3
607 character(len=11) :: str
608 character(len=80) :: filename
609 character(len=256) :: lines(7)
610
611 push_sub(mix_load)
612
613 ierr = 0
614
615 if (restart%skip()) then
616 ierr = -1
617 pop_sub(mix_load)
618 return
619 end if
620
621 message(1) = "Debug: Reading mixing restart."
622 call messages_info(1, namespace=namespace, debug_only=.true.)
623
624 ! First we read some information about the mixing
625 iunit = restart%open('mixing')
626 call restart%read(iunit, lines, 7, err)
627 if (err /= 0) then
628 ierr = ierr + 1
629 else
630 read(lines(1), *) str, scheme
631 read(lines(2), *) str, d1
632 read(lines(3), *) str, d2
633 read(lines(4), *) str, d3
634 read(lines(5), *) str, smix%iter
635 read(lines(6), *) str, ns
636 read(lines(7), *) str, smix%last_ipos
637 end if
638 call restart%close(iunit)
639
640
641 if (ierr == 0) then
642 ! We can only use the restart information if the mixing scheme and the number of steps used remained the same
643 if (scheme /= smix%scheme .or. ns /= smix%ns) then
644 message(1) = "The mixing scheme from the restart data is not the same as the one used in the current calculation."
645 call messages_warning(1, namespace=namespace)
646 ierr = ierr + 2
647 end if
648
649 ! Check the dimensions of the arrays to be read
650 if (mesh%np_global /= d1 .or. mesh%np /= smix%mixfield%d1 .or. d2 /= smix%mixfield%d2) then
651 message(1) = "The dimensions of the arrays from the mixing restart data"
652 message(2) = "are not the same as the ones used in this calculation."
653 call messages_warning(2, namespace=namespace)
654 ierr = ierr + 4
655 end if
656 end if
657
658
659 ! Now we read the different functions.
660 ! Note that we may have more or less functions than the ones needed (d3 /= smix%d3)
661 if (ierr == 0) then
662 if (smix%scheme /= option__mixingscheme__linear) then
663 err2 = 0
664 do id2 = 1, smix%mixfield%d2
665 do id3 = 1, smix%mixfield%d3
666
667 write(filename,'(a3,i2.2,i2.2)') 'df_', id2, id3
668 if (smix%mixfield%func_type == type_float) then
669 call restart%read_mesh_function(filename, mesh, smix%mixfield%ddf(1:mesh%np, id2, id3), err)
670 else
671 call restart%read_mesh_function(filename, mesh, smix%mixfield%zdf(1:mesh%np, id2, id3), err)
672 end if
673 if (err /= 0) err2(1) = err2(1) + 1
674
675 write(filename,'(a3,i2.2,i2.2)') 'dv_', id2, id3
676 if (smix%mixfield%func_type == type_float) then
677 call restart%read_mesh_function(filename, mesh, smix%mixfield%ddv(1:mesh%np, id2, id3), err)
678 else
679 call restart%read_mesh_function(filename, mesh, smix%mixfield%zdv(1:mesh%np, id2, id3), err)
680 end if
681 if (err /= 0) err2(2) = err2(2) + 1
682
683 end do
684
685 write(filename,'(a6,i2.2)') 'f_old_', id2
686 if (smix%mixfield%func_type == type_float) then
687 call restart%read_mesh_function(filename, mesh, smix%mixfield%df_old(1:mesh%np, id2), err)
688 else
689 call restart%read_mesh_function(filename, mesh, smix%mixfield%zf_old(1:mesh%np, id2), err)
690 end if
691 if (err /= 0) err2(3) = err2(3) + 1
693 write(filename,'(a8,i2.2)') 'vin_old_', id2
694 if (smix%mixfield%func_type == type_float) then
695 call restart%read_mesh_function(filename, mesh, smix%mixfield%dvin_old(1:mesh%np, id2), err)
696 else
697 call restart%read_mesh_function(filename, mesh, smix%mixfield%zvin_old(1:mesh%np, id2), err)
698 end if
699 if (err /= 0) err2(4) = err2(4) + 1
700
701 end do
702
703 if (err2(1) /= 0) ierr = ierr + 8
704 if (err2(2) /= 0) ierr = ierr + 16
705 if (err2(3) /= 0) ierr = ierr + 32
706 if (err2(4) /= 0) ierr = ierr + 64
707 end if
708 end if
709
710 if (ierr /= 0) then
711 ! Something went wront, so make sure we start from scratch
712 call mix_clear(smix)
713 end if
714
715 if (ierr == 0 .and. smix%scheme == option__mixingscheme__broyden_adaptive) then
716 smix%adaptive_damping = m_one
717 smix%n_backtrack = 0
718 smix%mixfield%last_residue = m_zero
719 end if
720
721
722 message(1) = "Debug: Reading mixing restart done."
723 call messages_info(1, namespace=namespace, debug_only=.true.)
724
725 pop_sub(mix_load)
726 end subroutine mix_load
727
728 real(real64) pure function mix_coefficient(this) result(coefficient)
729 type(mix_t), intent(in) :: this
730
731 coefficient = this%coeff(1)
732 end function mix_coefficient
733
734 integer pure function mix_scheme(this) result(scheme)
735 type(mix_t), intent(in) :: this
736
737 scheme = this%scheme
738 end function mix_scheme
739
740 integer pure function mix_d3(this)
741 type(mix_t), intent(in) :: this
742
743 mix_d3 = this%mixfield%d3
744 end function mix_d3
745
746 subroutine mix_get_field(this, mixfield)
747 type(mix_t), target, intent(in) :: this
748 type(mixfield_t), pointer, intent(out) :: mixfield
749
750 mixfield => this%mixfield
751 end subroutine mix_get_field
752
754 subroutine mixing(namespace, smix)
755 type(namespace_t), intent(in) :: namespace
756 type(mix_t), intent(inout) :: smix
757
758 push_sub(mixing)
759
760 if (smix%mixfield%func_type == type_float) then
761 call dmixing(namespace, smix, smix%mixfield%dvin, smix%mixfield%dvout, smix%mixfield%dvnew)
762 else
763 call zmixing(namespace, smix, smix%mixfield%zvin, smix%mixfield%zvout, smix%mixfield%zvnew)
764 end if
765
766 pop_sub(mixing)
767 end subroutine mixing
768
769 subroutine mix_add_auxmixfield(namespace, smix, mixfield)
770 type(namespace_t), intent(in) :: namespace
771 type(mix_t), intent(inout) :: smix
772 type(mixfield_t), target, intent(in) :: mixfield
773
774 push_sub(mix_add_auxmixfield)
775
776 smix%nauxmixfield = smix%nauxmixfield + 1
777 smix%auxmixfield(smix%nauxmixfield)%p => mixfield
778
779 if (smix%scheme == option__mixingscheme__diis) then
780 message(1) = 'Mixing scheme DIIS is not implemented for auxiliary mixing fields'
781 call messages_fatal(1, namespace=namespace)
782 end if
783
784 pop_sub(mix_add_auxmixfield)
785 end subroutine mix_add_auxmixfield
786
788 subroutine mixfield_init(smix, mixfield, d1, d2, d3, func_type)
789 type(mix_t), intent(in) :: smix
790 type(mixfield_t), intent(inout) :: mixfield
791 integer, intent(in) :: d1, d2, d3
792 type(type_t), intent(in) :: func_type
793
794 push_sub(mixfield_init)
795
796 mixfield%d1 = d1
797 mixfield%d2 = d2
798 mixfield%d3 = d3
799
800 mixfield%func_type = func_type
801
802 mixfield%mix_spin_density_matrix = smix%mix_spin_density_matrix
803 mixfield%last_residue = m_zero
804
805 if (smix%scheme /= option__mixingscheme__linear) then
806 if (mixfield%func_type == type_float) then
807 safe_allocate( mixfield%ddf(1:d1, 1:d2, 1:d3))
808 safe_allocate( mixfield%ddv(1:d1, 1:d2, 1:d3))
809 safe_allocate(mixfield%dvin_old(1:d1, 1:d2))
810 safe_allocate( mixfield%df_old(1:d1, 1:d2))
811 else
812 safe_allocate( mixfield%zdf(1:d1, 1:d2, 1:d3))
813 safe_allocate( mixfield%zdv(1:d1, 1:d2, 1:d3))
814 safe_allocate(mixfield%zvin_old(1:d1, 1:d2))
815 safe_allocate( mixfield%zf_old(1:d1, 1:d2))
816 end if
817 end if
818
819 if (mixfield%func_type == type_float) then
820 safe_allocate(mixfield%dvin(1:d1, 1:d2))
821 safe_allocate(mixfield%dvout(1:d1, 1:d2))
822 safe_allocate(mixfield%dvnew(1:d1, 1:d2))
823 safe_allocate(mixfield%dresidual(1:d1, 1:d2))
824 else
825 safe_allocate(mixfield%zvin(1:d1, 1:d2))
826 safe_allocate(mixfield%zvout(1:d1, 1:d2))
827 safe_allocate(mixfield%zvnew(1:d1, 1:d2))
828 safe_allocate(mixfield%zresidual(1:d1, 1:d2))
829 end if
830
831 pop_sub(mixfield_init)
832 end subroutine mixfield_init
833
835 subroutine mixfield_end(smix, mixfield)
836 type(mix_t), intent(inout) :: smix
837 type(mixfield_t), intent(inout) :: mixfield
838
839 push_sub(mixfield_end)
840
841 ! Arrays got allocated for all mixing schemes, except linear mixing
842 if (smix%scheme /= option__mixingscheme__linear) then
843 if (mixfield%func_type == type_float) then
844 safe_deallocate_a(mixfield%ddf)
845 safe_deallocate_a(mixfield%ddv)
846 safe_deallocate_a(mixfield%dvin_old)
847 safe_deallocate_a(mixfield%df_old)
848 else
849 safe_deallocate_a(mixfield%zdf)
850 safe_deallocate_a(mixfield%zdv)
851 safe_deallocate_a(mixfield%zvin_old)
852 safe_deallocate_a(mixfield%zf_old)
853 end if
854 end if
855
856 if (mixfield%func_type == type_float) then
857 safe_deallocate_a(mixfield%dvin)
858 safe_deallocate_a(mixfield%dvout)
859 safe_deallocate_a(mixfield%dvnew)
860 safe_deallocate_a(mixfield%dresidual)
861 else
862 safe_deallocate_a(mixfield%zvin)
863 safe_deallocate_a(mixfield%zvout)
864 safe_deallocate_a(mixfield%zvnew)
865 safe_deallocate_a(mixfield%zresidual)
866 end if
867
868 pop_sub(mixfield_end)
869 end subroutine mixfield_end
870
872 subroutine mixfield_clear(scheme, mixfield)
873 integer, intent(in) :: scheme
874 type(mixfield_t), intent(inout) :: mixfield
875 integer :: d1, d2, d3
876
877 push_sub(mixfield_clear)
878
879 d1 = mixfield%d1
880 d2 = mixfield%d2
881 d3 = mixfield%d3
882
883 if (scheme /= option__mixingscheme__linear) then
884 if (mixfield%func_type == type_float) then
885 assert(allocated(mixfield%ddf))
886 mixfield%ddf(1:d1, 1:d2, 1:d3) = m_zero
887 mixfield%ddv(1:d1, 1:d2, 1:d3) = m_zero
888 mixfield%dvin_old(1:d1, 1:d2) = m_zero
889 mixfield%df_old(1:d1, 1:d2) = m_zero
890 else
891 assert(allocated(mixfield%zdf))
892 mixfield%zdf(1:d1, 1:d2, 1:d3) = m_z0
893 mixfield%zdv(1:d1, 1:d2, 1:d3) = m_z0
894 mixfield%zvin_old(1:d1, 1:d2) = m_z0
895 mixfield%zf_old(1:d1, 1:d2) = m_z0
896 end if
897 end if
898
899 if (mixfield%func_type == type_float) then
900 mixfield%dvin(1:d1, 1:d2) = m_zero
901 mixfield%dvout(1:d1, 1:d2) = m_zero
902 mixfield%dvnew(1:d1, 1:d2) = m_zero
903 mixfield%dresidual(1:d1, 1:d2) = m_zero
904 else
905 mixfield%zvin(1:d1, 1:d2) = m_z0
906 mixfield%zvout(1:d1, 1:d2) = m_z0
907 mixfield%zvnew(1:d1, 1:d2) = m_z0
908 mixfield%zresidual(1:d1, 1:d2) = m_z0
909 end if
910
911 pop_sub(mixfield_clear)
912 end subroutine mixfield_clear
913
922 subroutine compute_residuals_aux_field(this)
923 class(mix_t), intent(inout) :: this
924
925 type(mixfield_t), pointer :: aux_field
926 integer :: dims(2)
927 integer :: i
928
931 do i = 1, this%nauxmixfield
932 aux_field => this%auxmixfield(i)%p
933 dims = [aux_field%d1, aux_field%d2]
934
935 ! Auxiliary fields are not necessarily mesh functions, so the Kerker preconditioner cannot be applied
936 if (aux_field%func_type == type_float) then
937 call dcompute_residual(this, dims, aux_field%dvin, aux_field%dvout, aux_field%dresidual, precondition=.false.)
938 else
939 call zcompute_residual(this, dims, aux_field%zvin, aux_field%zvout, aux_field%zresidual, precondition=.false.)
940 end if
941
942 end do
943 nullify(aux_field)
944
946
947 end subroutine compute_residuals_aux_field
948
960 subroutine linear_mixing_aux_field(smix)
961 type(mix_t), intent(inout) :: smix
962
963 type(mixfield_t), pointer :: field
964 integer :: i
965
968 call smix%compute_residuals_aux_field()
969
970 do i = 1, smix%nauxmixfield
971 field => smix%auxmixfield(i)%p
972
973 if (field%func_type == type_float) then
974 call dmixing_linear(field%d1, field%d2, smix%coeff, field%dvin, field%dresidual, field%dvnew)
975 else
976 call zmixing_linear(field%d1, field%d2, smix%coeff, field%zvin, field%zresidual, field%zvnew)
977 end if
978
979 end do
980
981 nullify(field)
982
984 end subroutine linear_mixing_aux_field
985
986#include "undef.F90"
987#include "real.F90"
988
989#include "mix_inc.F90"
990
991#include "undef.F90"
992#include "complex.F90"
993
994#include "mix_inc.F90"
995
996end module mix_oct_m
997
998!! Local Variables:
999!! mode: f90
1000!! coding: utf-8
1001!! End:
subroutine init_preconditioner()
Definition: mix.F90:500
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
real(real64), parameter, public m_zero
Definition: global.F90:200
real(real64), parameter, public m_one
Definition: global.F90:201
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:117
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:120
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:525
subroutine, public messages_obsolete_variable(namespace, name, rep)
Definition: messages.F90:1000
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:691
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1040
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:594
subroutine, public mixfield_end(smix, mixfield)
Deallocate all arrays of a mixfield instance.
Definition: mix.F90:931
integer pure function, public mix_scheme(this)
Definition: mix.F90:830
subroutine dmixfield_set_vin(mixfield, vin)
Definition: mix.F90:1966
real(real64) pure function, public mix_coefficient(this)
Definition: mix.F90:824
subroutine, public mixing(namespace, smix)
Main entry-point to SCF mixer.
Definition: mix.F90:850
subroutine, public mixfield_init(smix, mixfield, d1, d2, d3, func_type)
Initialise all attributes of a mixfield instance.
Definition: mix.F90:884
subroutine zmixfield_get_vnew(mixfield, vnew)
Definition: mix.F90:2993
subroutine, public mix_get_field(this, mixfield)
Definition: mix.F90:842
subroutine compute_residuals_aux_field(this)
Compute the residuals of the auxilliary fields.
Definition: mix.F90:1018
subroutine zmixfield_set_vout(mixfield, vout)
Definition: mix.F90:2974
subroutine zcompute_residual(smix, dims, vin, vout, residual, precondition)
Compute the residual of the potential (or density) for SCF mixing.
Definition: mix.F90:2233
subroutine, public mix_add_auxmixfield(namespace, smix, mixfield)
Definition: mix.F90:865
subroutine dmixfield_get_vnew(mixfield, vnew)
Definition: mix.F90:2032
subroutine dmixing_linear(d1, d2, coeff, vin, residual, vnew)
Linear mixing of the input and output potentials.
Definition: mix.F90:1212
subroutine, public mix_dump(namespace, restart, smix, mesh, ierr)
Definition: mix.F90:594
subroutine, public mixfield_clear(scheme, mixfield)
Zero all potential and field attributes of a mixfield instance.
Definition: mix.F90:968
subroutine dcompute_residual(smix, dims, vin, vout, residual, precondition)
Compute the residual of the potential (or density) for SCF mixing.
Definition: mix.F90:1244
subroutine, public mix_init(smix, namespace, space, der, d1, d2, def_, func_type_, prefix_)
Initialise mix_t instance.
Definition: mix.F90:270
subroutine zmixfield_set_vin(mixfield, vin)
Definition: mix.F90:2955
subroutine, public zmixing(namespace, smix, vin, vout, vnew)
Mix the input and output potentials (or densities) according to some scheme.
Definition: mix.F90:2144
integer pure function, public mix_d3(this)
Definition: mix.F90:836
subroutine, public mix_load(namespace, restart, smix, mesh, ierr)
Definition: mix.F90:693
subroutine dmixfield_set_vout(mixfield, vout)
Definition: mix.F90:1999
subroutine, public dmixing(namespace, smix, vin, vout, vnew)
Mix the input and output potentials (or densities) according to some scheme.
Definition: mix.F90:1155
subroutine linear_mixing_aux_field(smix)
Linear mixing of the auxilliary fields.
Definition: mix.F90:1056
subroutine zmixing_linear(d1, d2, coeff, vin, residual, vnew)
Linear mixing of the input and output potentials.
Definition: mix.F90:2201
subroutine, public mix_end(smix)
Definition: mix.F90:571
subroutine, public mix_clear(smix)
Definition: mix.F90:554
This module defines non-local operators.
subroutine, public nl_operator_init(op, label, symm)
initialize an instance of a non-local operator by setting the label
subroutine, public nl_operator_update_gpu_buffers(op)
subroutine, public nl_operator_output_weights(this)
subroutine, public nl_operator_end(op)
subroutine, public nl_operator_build(space, mesh, op, np, const_w, regenerate)
Creates the nonlocal operators for the stencils used for finite differences.
subroutine, public nl_operator_allocate_gpu_buffers(op)
This module is intended to contain "only mathematical" functions and procedures.
Definition: solvers.F90:117
This module defines routines, generating operators for a cubic stencil.
subroutine, public stencil_cube_get_lapl(this, dim, order)
type(type_t), parameter, public type_float
Definition: types.F90:136
class representing derivatives
Describes mesh distribution to nodes.
Definition: mesh.F90:187
God class for mixing.
Definition: mix.F90:216
Quantities used in mixing: Input, output and new potentials, and the residuals.
Definition: mix.F90:172
int true(void)