Octopus
geom_opt.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2007 M. Marques, A. Castro, A. Rubio, G. Bertsch, M. Oliveira
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
21module geom_opt_oct_m
22 use accel_oct_m
24 use debug_oct_m
28 use forces_oct_m
29 use global_oct_m
31 use io_oct_m
34 use ions_oct_m
35 use, intrinsic :: iso_fortran_env
37 use lcao_oct_m
38 use loct_oct_m
39 use math_oct_m
40 use mesh_oct_m
43 use mpi_oct_m
46 use parser_oct_m
47 use pcm_oct_m
51 use scf_oct_m
57 use unit_oct_m
59 use v_ks_oct_m
61
62 implicit none
63
64 private
65 public :: geom_opt_run
66
67 type geom_opt_t
68 private
69 integer(int64) :: type
70 integer :: method
71 real(real64) :: step
72 real(real64) :: line_tol
73 real(real64) :: fire_mass
74 integer :: fire_integrator
75 real(real64) :: tolgrad
76 real(real64) :: toldr
77 integer :: max_iter
78 integer :: what2minimize
79
81 type(scf_t) :: scfv
82 type(ions_t), pointer :: ions
83 type(hamiltonian_elec_t), pointer :: hm
84 type(electrons_t), pointer :: syst
85 class(mesh_t), pointer :: mesh
86 type(states_elec_t), pointer :: st
87 integer :: dim
88 integer :: periodic_dim
89 integer :: size
90 integer :: fixed_atom = 0
91
92 real(real64), allocatable :: cell_force(:, :)
93 logical :: symmetrize = .false.
94 real(real64), allocatable :: initial_length(:)
95 real(real64), allocatable :: initial_rlattice(:, :)
96 real(real64), allocatable :: inv_initial_rlattice(:, :)
97 real(real64) :: pressure
98
99 logical :: poscar_output = .false.
100
101 end type geom_opt_t
102
103 type(geom_opt_t), save :: g_opt
104
105 integer, parameter :: &
106 MINWHAT_ENERGY = 1, &
108
109 integer, parameter :: &
110 GO_IONS = 1, &
111 go_cell = 2, &
112 go_volume = 4
113
114contains
115
116 ! ---------------------------------------------------------
117 subroutine geom_opt_run(system, from_scratch)
118 class(*), intent(inout) :: system
119 logical, intent(inout) :: from_scratch
120
121 push_sub(geom_opt_run)
122
123 select type (system)
124 class is (multisystem_basic_t)
125 message(1) = "CalculationMode = go not implemented for multi-system calculations"
126 call messages_fatal(1, namespace=system%namespace)
127 type is (electrons_t)
128 call geom_opt_run_legacy(system, from_scratch)
129 end select
130
131 pop_sub(geom_opt_run)
132 end subroutine geom_opt_run
133
134 ! ---------------------------------------------------------
135 subroutine geom_opt_run_legacy(sys, fromscratch)
136 type(electrons_t), target, intent(inout) :: sys
137 logical, intent(inout) :: fromscratch
138
139 integer :: ierr
140 real(real64), allocatable :: coords(:)
141 real(real64) :: energy
142
143 real(real64), allocatable :: mass(:)
144 integer :: iatom, imass
145 type(restart_t) :: restart_load
146 logical :: known_lower_bound
147
148 push_sub(geom_opt_run_legacy)
149
150 if (sys%space%periodic_dim == 1 .or. sys%space%periodic_dim == 2) then
151 message(1) = "Geometry optimization not allowed for systems periodic in 1D and 2D, "
152 message(2) = "as in those cases the total energy is not correct."
153 call messages_fatal(2, namespace=sys%namespace)
154 end if
155
156
157 if (sys%hm%pcm%run_pcm) then
158 call messages_not_implemented("PCM for CalculationMode /= gs or td", namespace=sys%namespace)
159 end if
160
161 if (sys%kpoints%use_symmetries) then
162 call messages_experimental("KPoints symmetries with CalculationMode = go", namespace=sys%namespace)
163 end if
165 call init_(fromscratch)
167 ! load wavefunctions
168 if (.not. fromscratch) then
169 call restart_load%init(sys%namespace, restart_gs, restart_type_load, sys%mc, ierr, mesh=sys%gr)
170 if (ierr == 0) then
171 call states_elec_load(restart_load, sys%namespace, sys%space, sys%st, sys%gr, sys%kpoints, ierr)
172 end if
173 call restart_load%end()
174 if (ierr /= 0) then
175 message(1) = "Unable to read wavefunctions: Starting from scratch."
176 call messages_warning(1, namespace=sys%namespace)
177 fromscratch = .true.
178 end if
179 end if
181 call scf_init(g_opt%scfv, sys%namespace, sys%gr, sys%ions, sys%st, sys%mc, sys%hm, sys%space)
183 if (bitand(g_opt%type, go_cell) /= 0 .or. bitand(g_opt%type, go_volume) /= 0) then
184 if (.not. g_opt%scfv%calc_stress) then
185 message(1) = "In order to optimize the cell, one needs to set SCFCalculateStress = yes."
186 call messages_fatal(1, namespace=sys%namespace)
187 end if
188 end if
190 if (fromscratch) then
191 call lcao_run(sys%namespace, sys%space, sys%gr, sys%ions, sys%ext_partners, sys%st, sys%ks, sys%hm, &
192 lmm_r = g_opt%scfv%lmm_r, known_lower_bound=known_lower_bound)
193 call scf_set_lower_bound_is_known(g_opt%scfv, known_lower_bound)
194 else
195 ! setup Hamiltonian
196 message(1) = 'Info: Setting up Hamiltonian.'
197 call messages_info(1, namespace=sys%namespace)
198 call v_ks_h_setup(sys%namespace, sys%space, sys%gr, sys%ions, sys%ext_partners, sys%st, sys%ks, sys%hm)
199 call scf_set_lower_bound_is_known(g_opt%scfv, .true.)
200 end if
201
202 g_opt%symmetrize = sys%kpoints%use_symmetries .or. sys%st%symmetrize_density
203
204 !Initial point
205 safe_allocate(coords(1:g_opt%size))
206 call to_coords(g_opt, coords)
207
208 if (sys%st%pack_states .and. sys%hm%apply_packed()) call sys%st%pack()
209
210 !Minimize
211 select case (g_opt%method)
213 call minimize_multidim_nograd(g_opt%method, g_opt%size, coords, g_opt%step,&
214 g_opt%toldr, g_opt%max_iter, &
215 calc_point_ng, write_iter_info_ng, energy, ierr)
216 case (minmethod_fire)
217
218 safe_allocate(mass(1:g_opt%size))
219 mass = g_opt%fire_mass
220 imass = 1
221 do iatom = 1, sys%ions%natoms
222 if (g_opt%fixed_atom == iatom) cycle
223 if (g_opt%ions%fixed(iatom)) cycle
224 if (g_opt%fire_mass <= m_zero) mass(imass:imass + 2) = sys%ions%mass(iatom)
225 imass = imass + 3
226 end do
227
228 !TODO: add variable to use Euler integrator
229 call minimize_fire(g_opt%size, g_opt%ions%space%dim, coords, g_opt%step, g_opt%tolgrad, &
230 g_opt%max_iter, calc_point, write_iter_info, energy, ierr, mass, integrator=g_opt%fire_integrator)
231 safe_deallocate_a(mass)
232
233 case default
234 call minimize_multidim(g_opt%method, g_opt%size, coords, g_opt%step ,&
235 g_opt%line_tol , g_opt%tolgrad, g_opt%toldr, g_opt%max_iter, &
236 calc_point, write_iter_info, energy, ierr)
237 end select
238
239 if (ierr == 1025) then
240 ! not a GSL error, set by our minimize routines, so we must handle it separately
241 message(1) = "Reached maximum number of iterations allowed by GOMaxIter."
242 call messages_info(1, namespace=sys%namespace)
243 else if (ierr /= 0 .and. g_opt%method /= minmethod_fire) then
244 message(1) = "Error occurred during the GSL minimization procedure:"
245 call loct_strerror(ierr, message(2))
246 call messages_fatal(2, namespace=sys%namespace)
247 end if
248
249 if (sys%st%pack_states .and. sys%hm%apply_packed()) call sys%st%unpack()
250
251
252 ! print out geometry
253 message(1) = "Writing final coordinates to min.xyz"
254 call messages_info(1, namespace=sys%namespace)
255 call from_coords(g_opt, coords)
256 call g_opt%ions%write_xyz('./min')
257
258 safe_deallocate_a(coords)
259 call scf_end(g_opt%scfv)
260 ! Because g_opt has the "save" attribute, we need to explicitly empty the criteria list here, or there will be a memory leak.
261 call g_opt%scfv%criterion_list%empty()
262 call end_()
263
264 pop_sub(geom_opt_run_legacy)
265 contains
266
267 ! ---------------------------------------------------------
268 subroutine init_(fromscratch)
269 logical, intent(inout) :: fromscratch
270
271 logical :: center, does_exist
272 integer :: iter, iatom, idir
273 character(len=100) :: filename
274 real(real64) :: default_toldr
275 real(real64) :: default_step
276 type(read_coords_info) :: xyz
277
278 push_sub(geom_opt_run_legacy.init_)
279
280 if (sys%space%is_periodic()) then
281 call messages_experimental('Geometry optimization for periodic systems', namespace=sys%namespace)
282 end if
283
284 !%Variable GOType
285 !%Type flag
286 !%Default ions
287 !%Section Calculation Modes::Geometry Optimization
288 !%Description
289 !% This variable defines which parameters are allowed to change during the optimization.
290 !% Multiple options can be chosen e.g. “ion_positions + cell_shape”.
291 !% Only one type of lattice vectors relaxation is possible.
292 !%Option ion_positions 1
293 !% Relax position of ions based on the forces acting on the ions.
294 !%Option cell_shape 2
295 !% Relax cell shape. This changes lattice vector lengths and directions
296 !% based on the stress acting on the lattice vectors.
297 !% See for instance Wentzcovitch, PRB 44, 2358 (1991).
298 !%Option cell_volume 4
299 !% Relax cell volume. Only allow for rescaling the lengths of lattice vectors.
300 !% This is a simplication of the option cell_shape, where only a diagonal strain is allowed.
301 !%End
302
303 call parse_variable(sys%namespace, 'GOType', go_ions, g_opt%type)
304 if (.not. varinfo_valid_option('GOType', g_opt%type, is_flag=.true.)) then
305 call messages_input_error(sys%namespace, 'GOType')
306 end if
307
308 write(message(1),'(a)') 'Input: [GOType = '
309 if (bitand(g_opt%type, go_ions) /= 0) then
310 write(message(1),'(a,1x,a)') trim(message(1)), 'ion_positions'
311 end if
312 if (bitand(g_opt%type, go_cell) /= 0) then
313 if (len_trim(message(1)) > 16) then
314 write(message(1),'(a,1x,a)') trim(message(1)), '+'
315 end if
316 write(message(1),'(a,1x,a)') trim(message(1)), 'cell_shape'
317 end if
318 if (bitand(g_opt%type, go_volume) /= 0) then
319 if (len_trim(message(1)) > 16) then
320 write(message(1),'(a,1x,a)') trim(message(1)), '+'
321 end if
322 write(message(1),'(a,1x,a)') trim(message(1)), 'cell_volume'
323 end if
324 write(message(1),'(2a)') trim(message(1)), ']'
325 call messages_info(1, namespace=sys%namespace)
326
327 if (bitand(g_opt%type, go_volume) /= 0 .and. bitand(g_opt%type, go_cell) /= 0) then
328 message(1) = "Cell and volume optimization cannot be used simultaneously."
329 call messages_fatal(1, namespace=sys%namespace)
330 end if
331
332
333 if (bitand(g_opt%type, go_cell) /= 0 .or. bitand(g_opt%type, go_volume) /= 0) then
334 if (parse_is_defined(sys%namespace, 'TDMomentumTransfer') .or. &
335 parse_is_defined(sys%namespace, 'TDReducedMomentumTransfer')) then
336 call messages_not_implemented("Cell dynamics with TDMomentumTransfer and TDReducedMomentumTransfer")
337 end if
338 end if
339
340 if (bitand(g_opt%type, go_cell) /= 0 .or. bitand(g_opt%type, go_volume) /= 0) then
341 if (accel_is_enabled()) then
342 message(1) = "Cell dynamics not supported on GPUs."
343 call messages_fatal(1, namespace=sys%namespace)
344 end if
345 end if
346
347
348 do iatom = 1, sys%ions%natoms
349 select type(spec=>sys%ions%atom(iatom)%species)
350 class is(allelectron_t)
351 write(message(1),'(a)') "Geometry optimization for all-electron potential is not implemented."
352 call messages_fatal(1)
353 end select
354 end do
355
356
357 call states_elec_allocate_wfns(sys%st, sys%gr, packed=.true.)
358
359 ! shortcuts
360 g_opt%mesh => sys%gr
361 g_opt%ions => sys%ions
362 g_opt%st => sys%st
363 g_opt%hm => sys%hm
364 g_opt%syst => sys
365 g_opt%dim = sys%space%dim
366 g_opt%periodic_dim = sys%space%periodic_dim
367
368 g_opt%size = 0
369 ! Ion dyamics
370 if (bitand(g_opt%type, go_ions) /= 0) then
371 g_opt%size = g_opt%dim * g_opt%ions%natoms
372 end if
373
374 ! Cell dynamics
375 if (bitand(g_opt%type, go_cell) /= 0) then
376 g_opt%size = g_opt%size + (g_opt%periodic_dim +1) * g_opt%periodic_dim / 2
377 safe_allocate(g_opt%cell_force(1:g_opt%periodic_dim, 1:g_opt%periodic_dim))
378 end if
379
380 ! Volume dynamics
381 if (bitand(g_opt%type, go_volume) /= 0) then
382 g_opt%size = g_opt%size + g_opt%periodic_dim
383 safe_allocate(g_opt%cell_force(1:g_opt%periodic_dim, 1:1))
384 ! Store the length of the original lattic vectors, to work with reduced lengthes
385 safe_allocate(g_opt%initial_length(1:g_opt%periodic_dim))
386 do idir = 1, g_opt%periodic_dim
387 g_opt%initial_length(idir) = norm2(g_opt%ions%latt%rlattice(1:g_opt%periodic_dim, idir))
388 end do
389 end if
390
391 ! Store the initial lattice vectors and the inverse matrix
392 if (bitand(g_opt%type, go_cell) /= 0 .or. bitand(g_opt%type, go_volume) /= 0) then
393 safe_allocate(g_opt%initial_rlattice(1:g_opt%periodic_dim, 1:g_opt%periodic_dim))
394 g_opt%initial_rlattice(1:g_opt%periodic_dim, 1:g_opt%periodic_dim) &
395 = g_opt%ions%latt%rlattice(1:g_opt%periodic_dim, 1:g_opt%periodic_dim)
396 safe_allocate(g_opt%inv_initial_rlattice(1:g_opt%periodic_dim, 1:g_opt%periodic_dim))
397 g_opt%inv_initial_rlattice(:, :) = g_opt%initial_rlattice(:, :)
398 call lalg_inverse(g_opt%periodic_dim, g_opt%inv_initial_rlattice, 'dir')
399 end if
400
401 if(g_opt%ions%space%is_periodic()) then
402 call parse_variable(sys%namespace, 'HydrostaticPressure', m_zero, g_opt%pressure)
403 end if
404
405 assert(g_opt%size > 0)
406
407 !%Variable GOCenter
408 !%Type logical
409 !%Default no
410 !%Section Calculation Modes::Geometry Optimization
411 !%Description
412 !% (Experimental) If set to yes, Octopus centers the geometry at
413 !% every optimization step. It also reduces the degrees of
414 !% freedom of the optimization by using the translational
415 !% invariance.
416 !%End
417 call parse_variable(sys%namespace, 'GOCenter', .false., center)
418
419 if (center) then
420 g_opt%fixed_atom = 1
421 g_opt%size = g_opt%size - g_opt%dim
422 call messages_experimental('GOCenter', namespace=sys%namespace)
423 end if
424
425 !Check if atoms are allowed to move and redifine g_opt%size
426 do iatom = 1, g_opt%ions%natoms
427 if (g_opt%ions%fixed(iatom)) then
428 g_opt%size = g_opt%size - g_opt%dim
429 end if
430 end do
431
432 !%Variable GOMethod
433 !%Type integer
434 !%Default fire
435 !%Section Calculation Modes::Geometry Optimization
436 !%Description
437 !% Method by which the minimization is performed. For more information see the
438 !% <a href=http://www.gnu.org/software/gsl/manual/html_node/Multidimensional-Minimization.html>
439 !% GSL documentation</a>.
440 !%Option steep 1
441 !% Simple steepest descent.
442 !%Option steep_native -1
443 !% (Experimental) Non-gsl implementation of steepest descent.
444 !%Option cg_fr 2
445 !% Fletcher-Reeves conjugate-gradient algorithm. The
446 !% conjugate-gradient algorithm proceeds as a succession of line
447 !% minimizations. The sequence of search directions is used to build
448 !% up an approximation to the curvature of the function in the
449 !% neighborhood of the minimum.
450 !%Option cg_pr 3
451 !% Polak-Ribiere conjugate-gradient algorithm.
452 !%Option cg_bfgs 4
453 !% Vector Broyden-Fletcher-Goldfarb-Shanno (BFGS) conjugate-gradient algorithm.
454 !% It is a quasi-Newton method which builds up an approximation to the second
455 !% derivatives of the function <i>f</i> using the difference between successive gradient
456 !% vectors. By combining the first and second derivatives, the algorithm is able
457 !% to take Newton-type steps towards the function minimum, assuming quadratic
458 !% behavior in that region.
459 !%Option cg_bfgs2 5
460 !% The bfgs2 version of this minimizer is the most efficient version available,
461 !% and is a faithful implementation of the line minimization scheme described in
462 !% Fletcher, <i>Practical Methods of Optimization</i>, Algorithms 2.6.2 and 2.6.4.
463 !%Option simplex 6
464 !% This is experimental, and in fact, <b>not</b> recommended unless you just want to
465 !% fool around. It is the Nead-Melder simplex algorithm, as implemented in the
466 !% GNU Scientific Library (GSL). It does not make use of the gradients (<i>i.e.</i>, the
467 !% forces) which makes it less efficient than other schemes. It is included here
468 !% for completeness, since it is free.
469 !%Option fire 8
470 !% The FIRE algorithm. See also <tt>GOFireMass</tt> and <tt>GOFireIntegrator</tt>.
471 !% Ref: E. Bitzek, P. Koskinen, F. Gahler, M. Moseler, and P. Gumbsch, <i>Phys. Rev. Lett.</i> <b>97</b>, 170201 (2006).
472 !%End
473 call parse_variable(sys%namespace, 'GOMethod', minmethod_fire, g_opt%method)
474 if (.not. varinfo_valid_option('GOMethod', g_opt%method)) call messages_input_error(sys%namespace, 'GOMethod')
475
476
477 call messages_print_var_option("GOMethod", g_opt%method, namespace=sys%namespace)
478
479 !%Variable GOTolerance
480 !%Type float
481 !%Default 0.001 H/b (0.051 eV/A)
482 !%Section Calculation Modes::Geometry Optimization
483 !%Description
484 !% Convergence criterion, for stopping the minimization. In
485 !% units of force; minimization is stopped when all forces on
486 !% ions are smaller than this criterion, or the
487 !% <tt>GOMinimumMove</tt> is satisfied. If <tt>GOTolerance < 0</tt>,
488 !% this criterion is ignored.
489 !%End
490 call parse_variable(sys%namespace, 'GOTolerance', 0.001_real64, g_opt%tolgrad, units_inp%force)
491
492 !%Variable GOMinimumMove
493 !%Type float
494 !%Section Calculation Modes::Geometry Optimization
495 !%Description
496 !% Convergence criterion, for stopping the minimization. In
497 !% units of length; minimization is stopped when the coordinates
498 !% of all species change less than <tt>GOMinimumMove</tt>, or the
499 !% <tt>GOTolerance</tt> criterion is satisfied.
500 !% If <tt>GOMinimumMove < 0</tt>, this criterion is ignored.
501 !% Default is -1, except 0.001 b with <tt>GOMethod = simplex</tt>.
502 !% Note that if you use <tt>GOMethod = simplex</tt>,
503 !% then you must supply a non-zero <tt>GOMinimumMove</tt>.
504 !%End
505 if (g_opt%method == minmethod_nmsimplex) then
506 default_toldr = 0.001_real64
507 else
508 default_toldr = -m_one
509 end if
510 call parse_variable(sys%namespace, 'GOMinimumMove', default_toldr, g_opt%toldr, units_inp%length)
511
512 if (g_opt%method == minmethod_nmsimplex .and. g_opt%toldr <= m_zero) call messages_input_error(sys%namespace, 'GOMinimumMove')
513
514 !%Variable GOStep
515 !%Type float
516 !%Section Calculation Modes::Geometry Optimization
517 !%Description
518 !% Initial step for the geometry optimizer. The default is 0.5.
519 !% WARNING: in some weird units.
520 !% For the FIRE minimizer, default value is 0.1 fs,
521 !% and corresponds to the initial time-step for the MD.
522 !%End
523 if (g_opt%method /= minmethod_fire) then
524 default_step = m_half
525 call parse_variable(sys%namespace, 'GOStep', default_step, g_opt%step)
526 else
527 default_step = 0.1_real64*unit_femtosecond%factor
528 call parse_variable(sys%namespace, 'GOStep', default_step, g_opt%step, unit = units_inp%time)
529 end if
530
531 !%Variable GOLineTol
532 !%Type float
533 !%Default 0.1
534 !%Section Calculation Modes::Geometry Optimization
535 !%Description
536 !% Tolerance for line-minimization. Applies only to GSL methods
537 !% that use the forces.
538 !% WARNING: in some weird units.
539 !%End
540 call parse_variable(sys%namespace, 'GOLineTol', 0.1_real64, g_opt%line_tol)
541
542 !%Variable GOMaxIter
543 !%Type integer
544 !%Default 200
545 !%Section Calculation Modes::Geometry Optimization
546 !%Description
547 !% Even if the convergence criterion is not satisfied, the minimization will stop
548 !% after this number of iterations.
549 !%End
550 call parse_variable(sys%namespace, 'GOMaxIter', 200, g_opt%max_iter)
551 if (g_opt%max_iter <= 0) then
552 message(1) = "GOMaxIter has to be larger than 0"
553 call messages_fatal(1, namespace=sys%namespace)
554 end if
555
556 !%Variable GOFireMass
557 !%Type float
558 !%Default 1.0 amu
559 !%Section Calculation Modes::Geometry Optimization
560 !%Description
561 !% The Fire algorithm (<tt>GOMethod = fire</tt>) assumes that all degrees of freedom
562 !% are comparable. All the velocities should be on the same
563 !% scale, which for heteronuclear systems can be roughly
564 !% achieved by setting all the atom masses equal, to the value
565 !% specified by this variable.
566 !% By default the mass of a proton is selected (1 amu).
567 !% However, a selection of <tt>GOFireMass = 0.01</tt> can, in manys systems,
568 !% speed up the geometry optimization procedure.
569 !% If <tt>GOFireMass</tt> <= 0, the masses of each
570 !% species will be used.
571 !%End
572 call parse_variable(sys%namespace, 'GOFireMass', m_one*unit_amu%factor, g_opt%fire_mass, unit = unit_amu)
573
574 !%Variable GOFireIntegrator
575 !%Type integer
576 !%Default verlet
577 !%Section Calculation Modes::Geometry Optimization
578 !%Description
579 !% The Fire algorithm (<tt>GOMethod = fire</tt>) uses a molecular dynamics
580 !% integrator to compute new geometries and velocities.
581 !% Currently, two integrator schemes can be selected
582 !%Option euler 0
583 !% The Explicit Euler method.
584 !%Option verlet 1
585 !% The Velocity Verlet algorithm.
586 !%Option semi_implicit_euler 2
587 !% Semi-implicit Euler integration, see J. Guénolé, et al. Computational Materials Science 175 (2020) 109584.
588 !%End
589 call parse_variable(sys%namespace, 'GOFireIntegrator', option__gofireintegrator__verlet, g_opt%fire_integrator)
590
591 call messages_obsolete_variable(sys%namespace, 'GOWhat2Minimize', 'GOObjective')
592
593 !%Variable GOObjective
594 !%Type integer
595 !%Default minimize_energy
596 !%Section Calculation Modes::Geometry Optimization
597 !%Description
598 !% This rather esoteric option allows one to choose which
599 !% objective function to minimize during a geometry
600 !% minimization. The use of this variable may lead to
601 !% inconsistencies, so please make sure you know what you are
602 !% doing.
603 !%Option minimize_energy 1
604 !% Use the total energy as objective function.
605 !%Option minimize_forces 2
606 !% Use <math>\sqrt{\sum_i \left| f_i \right|^2}</math> as objective function.
607 !% Note that in this case one still uses the forces as the gradient of the objective function.
608 !% This is, of course, inconsistent, and may lead to very strange behavior.
609 !%End
610 call parse_variable(sys%namespace, 'GOObjective', minwhat_energy, g_opt%what2minimize)
611 if (.not. varinfo_valid_option('GOObjective', g_opt%what2minimize)) call messages_input_error(sys%namespace, 'GOObjective')
612 call messages_print_var_option("GOObjective", g_opt%what2minimize, namespace=sys%namespace)
613
614
615 !%Variable XYZGOConstrains
616 !%Type string
617 !%Section Calculation Modes::Geometry Optimization
618 !%Description
619 !% <tt>Octopus</tt> will try to read the coordinate-dependent constrains from the XYZ file
620 !% specified by the variable <tt>XYZGOConstrains</tt>.
621 !% Note: It is important for the contrains to maintain the ordering
622 !% in which the atoms were defined in the coordinates specifications.
623 !% Moreover, constrains impose fixed absolute coordinates, therefore
624 !% constrains are not compatible with GOCenter = yes
625 !%End
626
627 !%Variable XSFGOConstrains
628 !%Type string
629 !%Section Calculation Modes::Geometry Optimization
630 !%Description
631 !% Like <tt>XYZGOConstrains</tt> but in XCrySDen format, as in <tt>XSFCoordinates</tt>.
632 !%End
633
634 !%Variable PDBGOConstrains
635 !%Type string
636 !%Section Calculation Modes::Geometry Optimization
637 !%Description
638 !% Like <tt>XYZGOConstrains</tt> but in PDB format, as in <tt>PDBCoordinates</tt>.
639 !%End
640
641 !%Variable GOConstrains
642 !%Type block
643 !%Section Calculation Modes::Geometry Optimization
644 !%Description
645 !% If <tt>XYZGOConstrains</tt>, <tt>PDBConstrains</tt>, and <tt>XSFGOConstrains</tt>
646 !% are not present, <tt>Octopus</tt> will try to fetch the geometry optimization
647 !% contrains from this block. If this block is not present, <tt>Octopus</tt>
648 !% will not set any constrains. The format of this block can be
649 !% illustrated by this example:
650 !%
651 !% <tt>%GOConstrains
652 !% <br>&nbsp;&nbsp;'C' | 1 | 0 | 0
653 !% <br>&nbsp;&nbsp;'O' | &nbsp;1 | 0 | 0
654 !% <br>%</tt>
655 !%
656 !% Coordinates with a constrain value of 0 will be optimized, while
657 !% coordinates with a constrain different from zero will be kept fixed. So,
658 !% in this example the x coordinates of both atoms will remain fixed and the
659 !% distance between the two atoms along the x axis will be constant.
660 !%
661 !% Note: It is important for the constrains to maintain the ordering
662 !% in which the atoms were defined in the coordinates specifications.
663 !% Moreover, constrains impose fixed absolute coordinates, therefore
664 !% constrains are not compatible with GOCenter = yes
665 !%End
666
667 call read_coords_init(xyz)
668 call read_coords_read('GOConstrains', xyz, g_opt%ions%space, sys%namespace)
669 if (xyz%source /= read_coords_err) then
670 !Sanity check
671 if (g_opt%ions%natoms /= xyz%n) then
672 write(message(1), '(a,i4,a,i4)') 'I need exactly ', g_opt%ions%natoms, ' constrains, but I found ', xyz%n
673 call messages_fatal(1, namespace=sys%namespace)
674 end if
675 ! copy information and adjust units
676 do iatom = 1, g_opt%ions%natoms
677 where(abs(xyz%atom(iatom)%x) <= m_epsilon)
678 g_opt%ions%atom(iatom)%c = m_zero
679 elsewhere
680 g_opt%ions%atom(iatom)%c = m_one
681 end where
682 end do
683
684 call read_coords_end(xyz)
685
686
687 if (g_opt%fixed_atom > 0) then
688 call messages_not_implemented("GOCenter with constrains", namespace=sys%namespace)
689 end if
690 else
691 do iatom = 1, g_opt%ions%natoms
692 g_opt%ions%atom(iatom)%c = m_zero
693 end do
694 end if
695
696
697 call io_rm("geom/optimization.log", sys%namespace)
698
699 call io_rm("work-geom.xyz", sys%namespace)
700
701 if (.not. fromscratch) then
702 inquire(file = './last.xyz', exist = does_exist)
703 if (.not. does_exist) fromscratch = .true.
704 end if
705
706 if (.not. fromscratch) call g_opt%ions%read_xyz('./last')
707
708 ! clean out old geom/go.XXXX.xyz files. must be consistent with write_iter_info
709 iter = 1
710 do
711 write(filename, '(a,i4.4,a)') "geom/go.", iter, ".xyz"
712 inquire(file = trim(filename), exist = does_exist)
713 if (does_exist) then
714 call io_rm(trim(filename), sys%namespace)
715 iter = iter + 1
716 else
717 exit
718 end if
719 ! TODO: clean forces directory
720 end do
721
722 call g_opt%scfv%restart_dump%init(sys%namespace, restart_gs, restart_type_dump, sys%mc, ierr, mesh=sys%gr)
723
725 end subroutine init_
726
727
728 ! ---------------------------------------------------------
729 subroutine end_()
730 push_sub(geom_opt_run_legacy.end_)
731
732 call states_elec_deallocate_wfns(sys%st)
733
734 call g_opt%scfv%restart_dump%end()
735
736 nullify(g_opt%mesh)
737 nullify(g_opt%ions)
738 nullify(g_opt%st)
739 nullify(g_opt%hm)
740 nullify(g_opt%syst)
741
742 safe_deallocate_a(g_opt%cell_force)
743
745 end subroutine end_
746
747 end subroutine geom_opt_run_legacy
748
749
750 ! ---------------------------------------------------------
753 subroutine calc_point(size, coords, objective, getgrad, df)
754 integer, intent(in) :: size
755 real(real64), intent(in) :: coords(size)
756 real(real64), intent(inout) :: objective
757 integer, intent(in) :: getgrad
758 real(real64), intent(inout) :: df(size)
759
760 integer :: iatom, idir, jdir
761 real(real64), dimension(g_opt%periodic_dim, g_opt%periodic_dim) :: stress, strain, right_stretch, rotation, sym_stress
762
763
764 push_sub(calc_point)
765
766 assert(size == g_opt%size)
767
768 call from_coords(g_opt, coords)
769
770 if (bitand(g_opt%type, go_cell) /= 0 .or. bitand(g_opt%type, go_volume) /= 0 ) then
771 call ion_dynamics_box_update(g_opt%syst%namespace, g_opt%syst%gr, g_opt%syst%space, g_opt%ions%latt)
772 end if
773
774 if (g_opt%fixed_atom /= 0) then
775 call g_opt%ions%translate(g_opt%ions%center())
776 end if
777
778 ! When the system is periodic in some directions, the atoms might have moved to a an adjacent cell,
779 ! so we need to move them back to the original cell
780 call g_opt%ions%fold_atoms_into_cell()
781
782 ! Some atoms might have moved outside the simulation box. We stop if this happens.
783 do iatom = 1, g_opt%ions%natoms
784 if (.not. g_opt%syst%gr%box%contains_point(g_opt%syst%ions%pos(:, iatom))) then
785 if (g_opt%syst%space%periodic_dim /= g_opt%syst%space%dim) then
786 ! FIXME: This could fail for partial periodicity systems
787 ! because contains_point is too strict with atoms close to
788 ! the upper boundary to the cell.
789 write(message(1), '(a,i5,a)') "Atom ", iatom, " has moved outside the box during the geometry optimization."
790 call messages_fatal(1, namespace=g_opt%syst%namespace)
791 end if
792 end if
793 end do
794
795 call g_opt%ions%write_xyz('./work-geom', append = .true.)
796
797 call scf_mix_clear(g_opt%scfv)
798
799 ! Update lattice vectors and regenerate grid
800 if (bitand(g_opt%type, go_cell) /= 0 .or. bitand(g_opt%type, go_volume) /= 0 ) then
801 call electrons_lattice_vectors_update(g_opt%syst%namespace, g_opt%syst%gr, &
802 g_opt%syst%space, g_opt%syst%hm%psolver, g_opt%syst%hm%kpoints, &
803 g_opt%syst%mc, g_opt%syst%st%qtot, g_opt%ions%latt)
804 end if
805
806 call hamiltonian_elec_epot_generate(g_opt%hm, g_opt%syst%namespace, g_opt%syst%space, g_opt%syst%gr, &
807 g_opt%ions, g_opt%syst%ext_partners, g_opt%st)
808 call density_calc(g_opt%st, g_opt%syst%gr, g_opt%st%rho)
809 call v_ks_calc(g_opt%syst%ks, g_opt%syst%namespace, g_opt%syst%space, g_opt%hm, g_opt%st, &
810 g_opt%ions,g_opt%syst%ext_partners, calc_eigenval = .true.)
811 call energy_calc_total(g_opt%syst%namespace, g_opt%syst%space, g_opt%hm, g_opt%syst%gr, g_opt%st, g_opt%syst%ext_partners)
812
813 ! do SCF calculation
814 call scf_run(g_opt%scfv, g_opt%syst%namespace, g_opt%syst%space, g_opt%syst%mc, g_opt%syst%gr, &
815 g_opt%ions, g_opt%syst%ext_partners, &
816 g_opt%st, g_opt%syst%ks, g_opt%hm, outp = g_opt%syst%outp, verbosity = verb_compact, restart_dump=g_opt%scfv%restart_dump)
817
818 ! This is already set to zero for periodic systems
819 if (g_opt%ions%force_total_enforce) then
820 call forces_set_total_to_zero(g_opt%ions, g_opt%ions%tot_force)
821 end if
822
823 call scf_print_mem_use(g_opt%syst%namespace)
825 ! Convert stress into cell force
826 ! This is the Parrinello-Rahman equation of motion of the cell,
827 ! see Parrinello and Rahman, J. Appl. Pys. 52, 7182 (1981), Eq. 2.10.
828 ! Here we use a slightly different definition, in which we evolve the right stretch tensor
829 ! instead of h, because this is a symmetric matrix.
830 ! The deformation tensor is defined as F = (1+\epsilon) = h h_0^{-1},
831 ! where \epsilon is the infinitesimal strain tensor
832 ! Using the polar decomposition F = R U, we can request to have \dot{U} that remains symmetric
833 ! in order to kill all the rotation
834 if (bitand(g_opt%type, go_cell) /= 0) then
835 stress = -g_opt%syst%st%stress_tensors%total(1:g_opt%periodic_dim, 1:g_opt%periodic_dim)
836
837 ! Replace \sigma = sym(R^T \sigma R)
838 ! We first compute R from the right polar decomposition R = F U^-1
839 strain = matmul(g_opt%ions%latt%rlattice(1:g_opt%periodic_dim,1:g_opt%periodic_dim), g_opt%inv_initial_rlattice)
840 right_stretch = lalg_remove_rotation(g_opt%periodic_dim, strain)
841 call lalg_inverse(g_opt%periodic_dim, right_stretch, 'dir')
842 rotation = matmul(strain, right_stretch)
843
844 ! Compute the symmetrized stress
845 sym_stress = matmul(transpose(rotation), matmul(stress, rotation))
846 sym_stress = m_half*(sym_stress + transpose(sym_stress))
847
848 do idir = 1, g_opt%periodic_dim
849 sym_stress(idir, idir) = sym_stress(idir, idir) - g_opt%pressure/g_opt%periodic_dim
850 end do
851 g_opt%cell_force = sym_stress * g_opt%ions%latt%rcell_volume
852
853 g_opt%cell_force = matmul(g_opt%cell_force, right_stretch)
854 end if
855
856 ! Convert stress into cell force
857 if (bitand(g_opt%type, go_volume) /= 0) then
858 stress = g_opt%syst%st%stress_tensors%total(1:g_opt%periodic_dim, 1:g_opt%periodic_dim)
859 do idir = 1, g_opt%periodic_dim
860 g_opt%cell_force(idir, 1) = -(g_opt%pressure/g_opt%periodic_dim + stress(idir, idir)) * g_opt%ions%latt%rcell_volume
861 end do
862 end if
863
864 if (bitand(g_opt%type, go_cell) /= 0 .or. bitand(g_opt%type, go_volume) /= 0) then
865 write(message(1),'(a,3a,a)') ' Stress tensor [', trim(units_abbrev(units_out%length)), ']'
866 do idir = 1, g_opt%periodic_dim
867 write(message(1+idir),'(9e18.10)') (units_from_atomic(units_out%length, g_opt%syst%st%stress_tensors%total(jdir, idir)), &
868 jdir = 1, g_opt%periodic_dim)
869 end do
870 call messages_info(1+g_opt%periodic_dim, namespace=g_opt%ions%namespace, debug_only=.true.)
871 write(message(1),'(a,3a,a)') ' Cell force tensor [', trim(units_abbrev(units_out%length)), ']'
872 do idir = 1, ubound(g_opt%cell_force, 2)
873 write(message(1+idir),'(9e18.10)') (units_from_atomic(units_out%length, g_opt%cell_force(jdir, idir)), &
874 jdir = 1, g_opt%periodic_dim)
875 end do
876 call messages_info(1+g_opt%periodic_dim, namespace=g_opt%ions%namespace, debug_only=.true.)
877 end if
878
879
880 ! store results
881 if (getgrad == 1) call to_grad(g_opt, df)
882
883 if (g_opt%what2minimize == minwhat_forces) then
884 objective = m_zero
885 do iatom = 1, g_opt%ions%natoms
886 if (g_opt%ions%fixed(iatom)) cycle
887 objective = objective + sum(g_opt%ions%tot_force(:, iatom)**2)
888 end do
889 if (bitand(g_opt%type, go_cell) /= 0) then
890 do idir = 1, g_opt%periodic_dim
891 objective = objective + sum(g_opt%cell_force(:, idir)**2)
892 end do
893 end if
894 if (bitand(g_opt%type, go_volume) /= 0) then
895 objective = objective + sum(g_opt%cell_force(:,1)**2)
896 end if
897 objective = sqrt(objective)
898 else
899 objective = g_opt%hm%energy%total
900 end if
901
902 pop_sub(calc_point)
903 end subroutine calc_point
904
905
906 ! ---------------------------------------------------------
911 subroutine calc_point_ng(size, coords, objective)
912 integer :: size
913 real(real64) :: coords(size)
914 real(real64) :: objective
915
916 integer :: getgrad
917 real(real64), allocatable :: df(:)
918
919 push_sub(calc_point_ng)
920
921 assert(size == g_opt%size)
922
923 getgrad = 0
924 safe_allocate(df(1:size))
925 df = m_zero
926
927 call calc_point(size, coords, objective, getgrad, df)
928 safe_deallocate_a(df)
929
930 pop_sub(calc_point_ng)
931 end subroutine calc_point_ng
932
933
934 ! ---------------------------------------------------------
936 subroutine write_iter_info(geom_iter, size, energy, maxdx, maxdf, coords)
937 integer, intent(in) :: geom_iter
938 integer, intent(in) :: size
939 real(real64), intent(in) :: energy, maxdx, maxdf
940 real(real64), intent(in) :: coords(size)
941
942 character(len=256) :: c_geom_iter, title, c_forces_iter
943 integer :: iunit
944
945 push_sub(write_iter_info)
946
947 write(c_geom_iter, '(a,i4.4)') "go.", geom_iter
948 write(title, '(f16.10,2x,a)') units_from_atomic(units_out%energy, energy), trim(units_abbrev(units_out%energy))
949 call io_mkdir('geom', g_opt%ions%namespace)
950 call g_opt%ions%write_xyz('geom/'//trim(c_geom_iter), comment = trim(title))
951 call g_opt%ions%write_xyz('./last')
952
953 if(g_opt%periodic_dim > 0) then
954 call g_opt%ions%write_xyz('geom/'//trim(c_geom_iter), comment = 'Reduced coordinates', reduce_coordinates = .true.)
955 call write_xsf_geometry_file('geom', trim(c_geom_iter), g_opt%ions%space, g_opt%ions%latt, &
956 g_opt%ions%pos, g_opt%ions%atom, g_opt%syst%gr, g_opt%syst%namespace)
957 end if
958
959 if (g_opt%syst%outp%what(option__output__forces)) then
960 write(c_forces_iter, '(a,i4.4)') "forces.", geom_iter
961 if (bitand(g_opt%syst%outp%how(option__output__forces), option__outputformat__bild) /= 0) then
962 call g_opt%ions%write_bild_forces_file('forces', trim(c_forces_iter))
963 else
964 call write_xsf_geometry_file('forces', trim(c_forces_iter), g_opt%ions%space, g_opt%ions%latt, &
965 g_opt%ions%pos, g_opt%ions%atom, g_opt%syst%gr, g_opt%syst%namespace, total_forces=g_opt%ions%tot_force)
966 end if
967 end if
968
969 if (mpi_world%is_root()) then
970 iunit = io_open(trim('geom/optimization.log'), g_opt%syst%namespace, &
971 action = 'write', position = 'append')
972
973 if (geom_iter == 1) then
974 if (bitand(g_opt%type, go_cell) /= 0) then
975 write(iunit, '(a10,5(5x,a20),a)') '# iter','energy [' // trim(units_abbrev(units_out%energy)) // ']', &
976 'max_force [' // trim(units_abbrev(units_out%force)) // ']',&
977 ' max_dr [' // trim(units_abbrev(units_out%length)) // ']', &
978 ' a, b, c ['// trim(units_abbrev(units_out%length)) // ']', &
979 ' volume ['// trim(units_abbrev(units_out%length**3)) // ']',&
980 ' alpha, beta, gamma [degrees]'
981 else
982 write(iunit, '(a10,3(5x,a20))') '# iter','energy [' // trim(units_abbrev(units_out%energy)) // ']', &
983 'max_force [' // trim(units_abbrev(units_out%force)) // ']',&
984 ' max_dr [' // trim(units_abbrev(units_out%length)) // ']'
985 end if
986 end if
987
988 if (bitand(g_opt%type, go_cell) /= 0) then
989 write(iunit, '(i10,10f25.15)') geom_iter, units_from_atomic(units_out%energy, energy), &
990 units_from_atomic(units_out%force,maxdf), &
991 units_from_atomic(units_out%length,maxdx), &
992 units_from_atomic(units_out%length,norm2(g_opt%ions%latt%rlattice(1:3, 1))),&
993 units_from_atomic(units_out%length,norm2(g_opt%ions%latt%rlattice(1:3, 2))),&
994 units_from_atomic(units_out%length,norm2(g_opt%ions%latt%rlattice(1:3, 3))),&
995 units_from_atomic(units_out%length**3, g_opt%ions%latt%rcell_volume), &
996 g_opt%ions%latt%alpha, g_opt%ions%latt%beta, g_opt%ions%latt%gamma
997 else
998 write(iunit, '(i10,3f25.15)') geom_iter, units_from_atomic(units_out%energy, energy), &
999 units_from_atomic(units_out%force,maxdf), &
1000 units_from_atomic(units_out%length,maxdx)
1001 end if
1002
1003 call io_close(iunit)
1004 end if
1005
1007 call messages_new_line()
1008
1009 call messages_write("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++", new_line = .true.)
1010
1011 call messages_write("+++++++++++++++++++++ MINIMIZATION ITER #:")
1012 call messages_write(geom_iter, fmt = "I5")
1013 call messages_write(" ++++++++++++++++++++++", new_line = .true.)
1014
1015 call messages_write(" Energy = ")
1016 call messages_write(energy, units = units_out%energy, fmt = "f16.10,1x", print_units = .true., new_line = .true.)
1017
1018 if (g_opt%periodic_dim == 0) then
1019 if (maxdf > m_zero) then
1020 call messages_write(" Max force = ")
1021 call messages_write(maxdf, units = units_out%force, fmt = "f16.10,1x", print_units = .true., new_line = .true.)
1022 end if
1023
1024 call messages_write(" Max dr = ")
1025 call messages_write(maxdx, units = units_out%length, fmt = "f16.10,1x", print_units = .true., new_line = .true.)
1026 else
1027 if (maxdf > m_zero) then
1028 call messages_write(" Max reduced force = ")
1029 call messages_write(maxdf, fmt = "f16.10,1x", print_units = .false., new_line = .true.)
1030 end if
1032 call messages_write(" Max reduced dr = ")
1033 call messages_write(maxdx, fmt = "f16.10,1x", print_units = .false., new_line = .true.)
1034 end if
1035
1036 call messages_write("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++", new_line = .true.)
1037 call messages_write("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++", new_line = .true.)
1038 call messages_new_line()
1039 call messages_new_line()
1040 call messages_info()
1041
1042 pop_sub(write_iter_info)
1043 end subroutine write_iter_info
1044
1045 ! ---------------------------------------------------------
1047 subroutine to_coords(gopt, coords)
1048 type(geom_opt_t), intent(in) :: gopt
1049 real(real64), intent(out) :: coords(:)
1050
1051 integer :: iatom, idir, jdir, icoord
1052 real(real64) :: tmp_pos(gopt%dim), strain(g_opt%periodic_dim,g_opt%periodic_dim)
1053
1054 push_sub(to_coords)
1055
1056 icoord = 1
1057 ! Ion dynamics
1058 if (bitand(g_opt%type, go_ions) /= 0) then
1059 do iatom = 1, gopt%ions%natoms
1060 if (gopt%fixed_atom == iatom) cycle
1061 if (gopt%ions%fixed(iatom)) cycle
1062 tmp_pos = gopt%ions%pos(1:gopt%dim, iatom)
1063 if (gopt%fixed_atom > 0) tmp_pos = tmp_pos - gopt%ions%pos(1:gopt%dim, gopt%fixed_atom)
1064 tmp_pos = gopt%ions%latt%cart_to_red(tmp_pos)
1065 do idir = 1, gopt%dim
1066 coords(icoord) = tmp_pos(idir)
1067 icoord = icoord + 1
1068 end do
1069 end do
1070 end if
1071
1072 ! Cell dynamics
1073 if (bitand(g_opt%type, go_cell) /= 0) then
1074 ! We compute the change in the right stretch tensor U, defined as h = U h_0
1075 strain = matmul(gopt%ions%latt%rlattice, g_opt%inv_initial_rlattice)
1076 do idir = 1, g_opt%periodic_dim
1077 do jdir = idir, g_opt%periodic_dim
1078 coords(icoord) = strain(idir, jdir)
1079 icoord = icoord + 1
1080 end do
1081 end do
1082 end if
1083
1084 ! Volume dynamics
1085 if (bitand(g_opt%type, go_volume) /= 0) then
1086 do idir = 1, g_opt%periodic_dim
1087 coords(icoord) = norm2(gopt%ions%latt%rlattice(1:g_opt%periodic_dim, idir))/g_opt%initial_length(idir)
1088 icoord = icoord + 1
1089 end do
1090 end if
1091
1092
1093 pop_sub(to_coords)
1094 end subroutine to_coords
1095
1096 ! ---------------------------------------------------------
1098 subroutine to_grad(gopt, grad)
1099 type(geom_opt_t), intent(in) :: gopt
1100 real(real64), intent(out) :: grad(:)
1101
1102 integer :: iatom, idir, jdir, icoord
1103 real(real64) :: tmp_force(1:gopt%dim)
1104
1105 push_sub(to_grad)
1106
1107 icoord = 1
1108 ! Ion dynamics
1109 if (bitand(g_opt%type, go_ions) /= 0) then
1110 do iatom = 1, gopt%ions%natoms
1111 if (gopt%fixed_atom == iatom) cycle
1112 if (gopt%ions%fixed(iatom)) cycle
1113 do idir = 1, gopt%dim
1114 if (abs(gopt%ions%atom(iatom)%c(idir)) <= m_epsilon) then
1115 tmp_force(idir) = -gopt%ions%tot_force(idir, iatom)
1116 else
1117 tmp_force(idir) = m_zero
1118 end if
1119 if (gopt%fixed_atom > 0) then
1120 tmp_force(idir) = tmp_force(idir) + gopt%ions%tot_force(idir, gopt%fixed_atom)
1121 end if
1122 end do
1123 tmp_force = gopt%ions%latt%cart_to_red(tmp_force)
1124 do idir = 1, gopt%dim
1125 grad(icoord) = tmp_force(idir)
1126 icoord = icoord + 1
1127 end do
1128 end do
1129 end if
1130
1131 ! Cell dynamics
1132 if (bitand(g_opt%type, go_cell) /= 0) then
1133 do idir = 1, g_opt%periodic_dim
1134 do jdir = idir, g_opt%periodic_dim
1135 grad(icoord) = -g_opt%cell_force(idir, jdir)
1136 icoord = icoord + 1
1137 end do
1138 end do
1139 end if
1140
1141 ! Volume dynamics
1142 if (bitand(g_opt%type, go_volume) /= 0) then
1143 do idir = 1, g_opt%periodic_dim
1144 grad(icoord) = -g_opt%cell_force(idir, 1)
1145 icoord = icoord + 1
1146 end do
1147 end if
1148
1149
1150 pop_sub(to_grad)
1151 end subroutine to_grad
1152
1153 ! ---------------------------------------------------------
1155 subroutine from_coords(gopt, coords)
1156 type(geom_opt_t), intent(inout) :: gopt
1157 real(real64), intent(in) :: coords(:)
1158
1159 integer :: iatom, idir, jdir, icoord
1160 real(real64) :: tmp_pos(gopt%dim, gopt%ions%natoms), strain(g_opt%periodic_dim,g_opt%periodic_dim)
1161
1162 push_sub(from_coords)
1163
1164 ! Get the new reduced atomic coordinates
1165 tmp_pos = m_zero
1166 icoord = 1
1167 ! Ion dynamics
1168 if (bitand(g_opt%type, go_ions) /= 0) then
1169 do iatom = 1, gopt%ions%natoms
1170 if (gopt%fixed_atom == iatom) cycle
1171 if (gopt%ions%fixed(iatom)) cycle
1172 do idir = 1, gopt%dim
1173 tmp_pos(idir, iatom) = coords(icoord)
1174 icoord = icoord + 1
1175 end do
1176 end do
1177 else
1178 do iatom = 1, gopt%ions%natoms
1179 tmp_pos(:, iatom) = gopt%ions%latt%cart_to_red(gopt%ions%pos(:, iatom))
1180 end do
1181 end if
1182
1183 ! Updating the lattice vectors
1184 if (bitand(g_opt%type, go_cell) /= 0) then
1185 do idir = 1, g_opt%periodic_dim
1186 do jdir = idir, g_opt%periodic_dim
1187 strain(idir, jdir) = coords(icoord)
1188 icoord = icoord + 1
1189 end do
1190 end do
1191 call upper_triangular_to_hermitian(g_opt%periodic_dim, strain)
1192
1193 ! Get the new lattice vectors from the new right stretch tensor U
1194 ! The strain tensor is defined as A = U * A_0
1195 gopt%ions%latt%rlattice = matmul(strain, g_opt%initial_rlattice)
1196 end if
1197
1198 ! Updating the lattice vectors
1199 if (bitand(g_opt%type, go_volume) /= 0) then
1200 do idir = 1, g_opt%periodic_dim
1201 gopt%ions%latt%rlattice(1:g_opt%periodic_dim, idir) = coords(icoord) &
1202 * gopt%initial_rlattice(1:g_opt%periodic_dim, idir)
1203 icoord = icoord + 1
1204 end do
1205 end if
1206
1207 ! Symmetrize and update the lattice vectors
1208 if (bitand(g_opt%type, go_cell) /= 0 .or. bitand(g_opt%type, go_volume) /= 0) then
1209 call g_opt%syst%gr%symmetrizer%symmetrize_lattice_vectors(g_opt%periodic_dim, g_opt%initial_rlattice, &
1210 gopt%ions%latt%rlattice, gopt%symmetrize)
1211 call gopt%ions%update_lattice_vectors(gopt%ions%latt, gopt%symmetrize)
1212 end if
1213
1214 ! Ion dynamics
1215 if (bitand(g_opt%type, go_ions) /= 0) then
1216 ! To Cartesian coordinates
1217 do iatom = 1, gopt%ions%natoms
1218 if (gopt%fixed_atom == iatom) cycle
1219 if (gopt%ions%fixed(iatom)) cycle
1220 tmp_pos(:, iatom) = gopt%ions%latt%red_to_cart(tmp_pos(:, iatom))
1221 do idir = 1, gopt%dim
1222 if (abs(gopt%ions%atom(iatom)%c(idir)) <= m_epsilon) then
1223 gopt%ions%pos(idir, iatom) = tmp_pos(idir, iatom)
1224 end if
1225 end do
1226 if (gopt%fixed_atom > 0) then
1227 gopt%ions%pos(:, iatom) = gopt%ions%pos(:, iatom) + gopt%ions%pos(:, gopt%fixed_atom)
1228 end if
1229 end do
1230 else
1231 do iatom = 1, gopt%ions%natoms
1232 gopt%ions%pos(:, iatom) = gopt%ions%latt%red_to_cart(tmp_pos(:, iatom))
1233 end do
1234 end if
1235
1236 if (gopt%symmetrize) then
1237 call gopt%ions%symmetrize_atomic_coord()
1238 end if
1239
1240 if (debug%info) then
1241 call gopt%ions%print_spacegroup()
1242 end if
1243
1244 pop_sub(from_coords)
1245 end subroutine from_coords
1246
1247 ! ---------------------------------------------------------
1249 subroutine write_iter_info_ng(geom_iter, size, energy, maxdx, coords)
1250 integer, intent(in) :: geom_iter
1251 integer, intent(in) :: size
1252 real(real64), intent(in) :: energy, maxdx
1253 real(real64), intent(in) :: coords(size)
1254
1255 push_sub(write_iter_info_ng)
1256 call write_iter_info(geom_iter, size, energy, maxdx, -m_one, coords)
1257
1258 pop_sub(write_iter_info_ng)
1259 end subroutine write_iter_info_ng
1260
1261end module geom_opt_oct_m
1262
1263!! Local Variables:
1264!! mode: f90
1265!! coding: utf-8
1266!! End:
subroutine init_(fromscratch)
Definition: geom_opt.F90:364
subroutine end_()
Definition: geom_opt.F90:825
pure logical function, public accel_is_enabled()
Definition: accel.F90:380
type(debug_t), save, public debug
Definition: debug.F90:158
This module implements a calculator for the density and defines related functions.
Definition: density.F90:122
subroutine, public density_calc(st, gr, density, istin)
Computes the density from the orbitals in st.
Definition: density.F90:612
subroutine, public energy_calc_total(namespace, space, hm, gr, st, ext_partners, iunit, full)
This subroutine calculates the total energy of the system. Basically, it adds up the KS eigenvalues,...
subroutine, public forces_set_total_to_zero(ions, force)
Definition: forces.F90:545
subroutine, public geom_opt_run(system, from_scratch)
Definition: geom_opt.F90:213
subroutine calc_point_ng(size, coords, objective)
Same as calc_point, but without the gradients. No intents here is unfortunately required because the ...
Definition: geom_opt.F90:1007
subroutine to_grad(gopt, grad)
Transfer data from the forces to the work array for the gradients (grad)
Definition: geom_opt.F90:1194
integer, parameter go_cell
Definition: geom_opt.F90:204
integer, parameter minwhat_forces
Definition: geom_opt.F90:200
subroutine write_iter_info_ng(geom_iter, size, energy, maxdx, coords)
Same as write_iter_info, but without the gradients.
Definition: geom_opt.F90:1345
subroutine write_iter_info(geom_iter, size, energy, maxdx, maxdf, coords)
Output the information after each iteration of the geometry optimization.
Definition: geom_opt.F90:1032
subroutine calc_point(size, coords, objective, getgrad, df)
Note: you might think it would be better to change the arguments with '(size)' below to '(:)'....
Definition: geom_opt.F90:849
subroutine to_coords(gopt, coords)
Transfer the data from the data structures to the work array (coords)
Definition: geom_opt.F90:1143
integer, parameter go_volume
Definition: geom_opt.F90:204
subroutine from_coords(gopt, coords)
Transfer the data from the work array (coords) to the actual data structures.
Definition: geom_opt.F90:1251
subroutine geom_opt_run_legacy(sys, fromscratch)
Definition: geom_opt.F90:231
real(real64), parameter, public m_zero
Definition: global.F90:191
real(real64), parameter, public m_epsilon
Definition: global.F90:207
real(real64), parameter, public m_half
Definition: global.F90:197
real(real64), parameter, public m_one
Definition: global.F90:192
subroutine, public hamiltonian_elec_epot_generate(this, namespace, space, gr, ions, ext_partners, st, time)
subroutine, public write_xsf_geometry_file(dir, fname, space, latt, pos, atoms, mesh, namespace, total_forces)
Definition: io.F90:116
subroutine, public io_close(iunit, grp)
Definition: io.F90:467
subroutine, public io_rm(fname, namespace)
Definition: io.F90:392
subroutine, public io_mkdir(fname, namespace, parents)
Definition: io.F90:361
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:402
subroutine, public electrons_lattice_vectors_update(namespace, gr, space, psolver, kpoints, mc, qtot, new_latt)
subroutine, public ion_dynamics_box_update(namespace, gr, space, new_latt)
real(real64) function, dimension(1:n, 1:n), public lalg_remove_rotation(n, A)
Remove rotation from affine transformation A by computing the polar decomposition and discarding the ...
Definition: lalg_adv.F90:738
subroutine, public lcao_run(namespace, space, gr, ions, ext_partners, st, ks, hm, st_start, lmm_r, known_lower_bound)
Definition: lcao.F90:770
System information (time, memory, sysname)
Definition: loct.F90:117
subroutine, public loct_strerror(errno, res)
Definition: loct.F90:384
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:117
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:120
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1091
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:525
subroutine, public messages_obsolete_variable(namespace, name, rep)
Definition: messages.F90:1023
subroutine, public messages_new_line()
Definition: messages.F90:1112
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:410
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:691
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1063
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:594
integer, parameter, public minmethod_nmsimplex
Definition: minimizer.F90:136
integer, parameter, public minmethod_fire
Definition: minimizer.F90:136
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:272
This module implements the basic mulsisystem class, a container system for other systems.
logical function, public parse_is_defined(namespace, name)
Definition: parser.F90:455
integer, parameter, public read_coords_err
for read_coords_info::file_type
subroutine, public read_coords_init(gf)
subroutine, public read_coords_end(gf)
subroutine, public read_coords_read(what, gf, space, namespace)
integer, parameter, public restart_gs
Definition: restart.F90:156
integer, parameter, public restart_type_dump
Definition: restart.F90:183
integer, parameter, public restart_type_load
Definition: restart.F90:183
pure subroutine, public scf_set_lower_bound_is_known(scf, known_lower_bound)
Set the flag lower_bound_is_known.
Definition: scf.F90:1645
subroutine, public scf_print_mem_use(namespace)
Definition: scf.F90:1525
subroutine, public scf_mix_clear(scf)
Definition: scf.F90:557
integer, parameter, public verb_compact
Definition: scf.F90:206
subroutine, public scf_init(scf, namespace, gr, ions, st, mc, hm, space)
Definition: scf.F90:257
subroutine, public scf_end(scf)
Definition: scf.F90:527
subroutine, public scf_run(scf, namespace, space, mc, gr, ions, ext_partners, st, ks, hm, outp, verbosity, iters_done, restart_dump)
Legacy version of the SCF code.
Definition: scf.F90:801
subroutine, public states_elec_deallocate_wfns(st)
Deallocates the KS wavefunctions defined within a states_elec_t structure.
subroutine, public states_elec_allocate_wfns(st, mesh, wfs_type, skip, packed)
Allocates the KS wavefunctions defined within a states_elec_t structure.
This module handles reading and writing restart information for the states_elec_t.
subroutine, public states_elec_load(restart, namespace, space, st, mesh, kpoints, ierr, iter, lr, lowest_missing, label, verbose, skip)
returns in ierr: <0 => Fatal error, or nothing read =0 => read all wavefunctions >0 => could only rea...
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:134
character(len=20) pure function, public units_abbrev(this)
Definition: unit.F90:225
This module defines the unit system, used for input and output.
type(unit_t), public unit_femtosecond
Time in femtoseconds.
type(unit_t), public unit_amu
Mass in atomic mass units (AKA Dalton).
type(unit_system_t), public units_out
type(unit_system_t), public units_inp
the units systems for reading and writing
subroutine, public v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, calc_eigenval, time, calc_energy, calc_current, force_semilocal)
Definition: v_ks.F90:752
subroutine, public v_ks_h_setup(namespace, space, gr, ions, ext_partners, st, ks, hm, calc_eigenval, calc_current)
Definition: v_ks.F90:698
An abstract type for all electron species.
Class describing the electron system.
Definition: electrons.F90:220
Container class for lists of system_oct_m::system_t.
int true(void)