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