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