Octopus
target_tdlocal.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
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
20#include "global.h"
21
24 use debug_oct_m
26 use epot_oct_m
27 use global_oct_m
28 use grid_oct_m
31 use io_oct_m
32 use ions_oct_m
35 use, intrinsic :: iso_fortran_env
37 use mesh_oct_m
44 use parser_oct_m
47 use space_oct_m
49 use target_oct_m
50 use td_oct_m
51 use unit_oct_m
53
54
55 implicit none
56
57 private
58 public :: target_tdlocal_t
59
61 type, extends(target_t) :: target_tdlocal_t
62 private
63 real(real64), allocatable :: rho(:)
64 real(real64), allocatable :: td_fitness(:)
65 character(len=200) :: td_local_target
66 contains
67 procedure :: init => target_init_tdlocal
68 procedure :: cleanup => target_end_tdlocal
69 procedure :: j1 => target_j1_tdlocal
70 procedure :: apply_chi => target_chi_tdlocal
71 procedure :: output => target_output_tdlocal
72 procedure :: tdcalc => target_tdcalc_tdlocal
73 procedure :: inh => target_inh_tdlocal
74 end type target_tdlocal_t
75
76
77contains
78
79 ! ----------------------------------------------------------------------
81 subroutine target_init_tdlocal(tg, gr, kpoints, namespace, space, ions, qcs, td, w0, oct, ep, restart)
82 class(target_tdlocal_t), intent(inout) :: tg
83 type(grid_t), intent(in) :: gr
84 type(kpoints_t), intent(in) :: kpoints
85 type(namespace_t), intent(in) :: namespace
86 class(space_t), intent(in) :: space
87 type(ions_t), intent(in) :: ions
88 type(opt_control_state_t), intent(inout) :: qcs
89 type(td_t), intent(in) :: td
90 real(real64), intent(in) :: w0
91 type(oct_t), intent(in) :: oct
92 type(epot_t), intent(inout) :: ep
93 type(restart_t), intent(inout) :: restart
94
95 type(block_t) :: blk
96 push_sub(target_init_tdlocal)
97
98 tg%move_ions = td%ions_dyn%ions_move()
99 tg%dt = td%dt
100
101 !%Variable OCTTdTarget
102 !%Type block
103 !%Section Calculation Modes::Optimal Control
104 !%Description
105 !% (Experimental) If <tt>OCTTargetOperator = oct_tg_td_local</tt>, then you must supply
106 !% a OCTTdTarget block. The block should only contain one element, a string cotaining the
107 !% definition of the time-dependent local target, <i>i.e.</i> a function of x,y,z and t that
108 !% is to be maximized along the evolution.
109 !%End
110 if (parse_block(namespace, 'OCTTdTarget', blk) == 0) then
111 call parse_block_string(blk, 0, 0, tg%td_local_target, convert_to_c=.true.)
112 safe_allocate(tg%rho(1:gr%np))
113 call parse_block_end(blk)
114 else
115 message(1) = 'If OCTTargetOperator = oct_tg_td_local, you must supply a OCTTdTarget block.'
116 call messages_fatal(1, namespace=namespace)
117 end if
118 safe_allocate(tg%td_fitness(0:td%max_iter))
119 tg%td_fitness = m_zero
120 call target_build_tdlocal(tg, gr, m_zero)
121
122 pop_sub(target_init_tdlocal)
123 end subroutine target_init_tdlocal
124
125
126 ! ----------------------------------------------------------------------
128 subroutine target_end_tdlocal(tg, oct)
129 class(target_tdlocal_t), intent(inout) :: tg
130 type(oct_t), intent(in) :: oct
131
132 push_sub(target_end_tdlocal)
133
134 safe_deallocate_a(tg%rho)
135 safe_deallocate_a(tg%td_fitness)
136
137 pop_sub(target_end_tdlocal)
138 end subroutine target_end_tdlocal
139
140
141 ! ----------------------------------------------------------------------
142 subroutine target_output_tdlocal(tg, namespace, space, gr, dir, ions, hm, outp)
143 class(target_tdlocal_t), intent(inout) :: tg
144 type(namespace_t), intent(in) :: namespace
145 class(space_t), intent(in) :: space
146 type(grid_t), intent(in) :: gr
147 character(len=*), intent(in) :: dir
148 type(ions_t), intent(in) :: ions
149 type(hamiltonian_elec_t), intent(in) :: hm
150 type(output_t), intent(in) :: outp
151
152 integer :: ierr
153 push_sub(target_output_tdlocal)
154
155 call io_mkdir(trim(dir), namespace)
157 call dio_function_output(outp%how(0), trim(dir), 'td_local_target', namespace, space, gr, &
158 tg%rho, units_out%length**(-space%dim), ierr, pos=ions%pos, atoms=ions%atom)
161 end subroutine target_output_tdlocal
162 ! ----------------------------------------------------------------------
165 ! ----------------------------------------------------------------------
167 real(real64) function target_j1_tdlocal(tg, namespace, gr, kpoints, qcpsi, ions) result(j1)
168 class(target_tdlocal_t), intent(inout) :: tg
169 type(namespace_t), intent(in) :: namespace
170 type(grid_t), intent(in) :: gr
171 type(kpoints_t), intent(in) :: kpoints
172 type(opt_control_state_t), intent(inout) :: qcpsi
173 type(ions_t), optional, intent(in) :: ions
174
175 integer :: maxiter
177
178 maxiter = size(tg%td_fitness) - 1
179 j1 = m_half * tg%dt * tg%td_fitness(0) + &
180 m_half * tg%dt * tg%td_fitness(maxiter) + &
181 tg%dt * sum(tg%td_fitness(1:maxiter-1))
182
183
184 pop_sub(target_j1_tdlocal)
185 end function target_j1_tdlocal
186
187
188 ! ----------------------------------------------------------------------
190 subroutine target_chi_tdlocal(tg, namespace, gr, kpoints, qcpsi_in, qcchi_out, ions)
191 class(target_tdlocal_t), intent(inout) :: tg
192 type(namespace_t), intent(in) :: namespace
193 type(grid_t), intent(in) :: gr
194 type(kpoints_t), intent(in) :: kpoints
195 type(opt_control_state_t), target, intent(inout) :: qcpsi_in
196 type(opt_control_state_t), target, intent(inout) :: qcchi_out
197 type(ions_t), intent(in) :: ions
198
199 integer :: ik, ib
200 type(states_elec_t), pointer :: chi_out
201 push_sub(target_chi_tdlocal)
202
203 chi_out => opt_control_point_qs(qcchi_out)
204
205 !We assume that there is no time-independent operator.
206
207 do ik = chi_out%d%kpt%start, chi_out%d%kpt%end
208 do ib = chi_out%group%block_start, chi_out%group%block_end
209 call batch_set_zero(chi_out%group%psib(ib, ik))
210 end do
211 end do
212
213 nullify(chi_out)
214 pop_sub(target_chi_tdlocal)
215 end subroutine target_chi_tdlocal
216
217
218 ! ---------------------------------------------------------
221 subroutine target_tdcalc_tdlocal(tg, namespace, space, hm, gr, ions, ext_partners, psi, time, max_time)
222 class(target_tdlocal_t), intent(inout) :: tg
223 type(namespace_t), intent(in) :: namespace
224 class(space_t), intent(in) :: space
225 type(hamiltonian_elec_t), intent(inout) :: hm
226 type(grid_t), intent(in) :: gr
227 type(ions_t), intent(inout) :: ions
228 type(partner_list_t), intent(in) :: ext_partners
229 type(states_elec_t), intent(inout) :: psi
230 integer, intent(in) :: time
231 integer, intent(in) :: max_time
232
233 complex(real64), allocatable :: opsi(:, :), zpsi(:, :)
234 integer :: ist, ip
235 push_sub(target_tdcalc_tdlocal)
236
237 tg%td_fitness(time) = m_zero
238
239 safe_allocate(zpsi(1:gr%np, 1:psi%d%dim))
240
241 !!!! WARNING Here one should build the time-dependent target.
242 select case (psi%d%ispin)
243 case (unpolarized)
244 assert(psi%nik == 1)
245 safe_allocate(opsi(1:gr%np_part, 1))
246 opsi = m_z0
247 do ist = psi%st_start, psi%st_end
248
249 call states_elec_get_state(psi, gr, ist, 1, zpsi)
250
251 do ip = 1, gr%np
252 opsi(ip, 1) = tg%rho(ip)*zpsi(ip, 1)
253 end do
254
255 tg%td_fitness(time) = tg%td_fitness(time) + psi%occ(ist, 1)*real(zmf_dotp(gr, psi%d%dim, zpsi, opsi), real64)
256
257 end do
258 safe_deallocate_a(opsi)
259 case (spin_polarized)
260 message(1) = 'Error in target.target_tdcalc: spin_polarized.'
261 call messages_fatal(1)
262 case (spinors)
263 message(1) = 'Error in target.target_tdcalc: spinors.'
264 call messages_fatal(1)
265 end select
266
267 safe_deallocate_a(zpsi)
268
269 pop_sub(target_tdcalc_tdlocal)
270 end subroutine target_tdcalc_tdlocal
271 ! ----------------------------------------------------------------------
272
273
274 ! ---------------------------------------------------------------
276 subroutine target_inh_tdlocal(tg, psi, gr, kpoints, time, inh, iter)
277 class(target_tdlocal_t), intent(inout) :: tg
278 type(states_elec_t), intent(inout) :: psi
279 type(grid_t), intent(in) :: gr
280 type(kpoints_t), intent(in) :: kpoints
281 real(real64), intent(in) :: time
282 type(states_elec_t), intent(inout) :: inh
283 integer, intent(in) :: iter
284
285 integer :: ik, ist, idim
286 complex(real64), allocatable :: zpsi(:)
287
288 push_sub(target_inh_tdlocal)
289
290 safe_allocate(zpsi(1:gr%np))
291
292 call target_build_tdlocal(tg, gr, time)
293
294 do ik = inh%d%kpt%start, inh%d%kpt%end
295 do ist = inh%st_start, inh%st_end
296 do idim = 1, inh%d%dim
297 call states_elec_get_state(psi, gr, idim, ist, ik, zpsi)
298 zpsi(1:gr%np) = -psi%occ(ist, ik)*tg%rho(1:gr%np)*zpsi(1:gr%np)
299 call states_elec_set_state(inh, gr, idim, ist, ik, zpsi)
300 end do
301 end do
302 end do
303
304 safe_deallocate_a(zpsi)
305
306 pop_sub(target_inh_tdlocal)
307 end subroutine target_inh_tdlocal
308 !----------------------------------------------------------
309
310
311 !----------------------------------------------------------
312 subroutine target_build_tdlocal(tg, gr, time)
313 class(target_tdlocal_t), intent(inout) :: tg
314 type(grid_t), intent(in) :: gr
315 real(real64), intent(in) :: time
317 integer :: ip
318 real(real64) :: xx(gr%box%dim), rr, re, im
319
320 push_sub(target_build_tdlocal)
321
322 do ip = 1, gr%np
323 call mesh_r(gr, ip, rr, coords = xx)
324 call parse_expression(re, im, gr%box%dim, xx, rr, time, tg%td_local_target)
325 tg%rho(ip) = re
326 end do
327
328 pop_sub(target_build_tdlocal)
329 end subroutine target_build_tdlocal
330 !----------------------------------------------------------
331
332end module target_tdlocal_oct_m
333
334!! Local Variables:
335!! mode: f90
336!! coding: utf-8
337!! End:
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:118
real(real64), parameter, public m_zero
Definition: global.F90:200
real(real64), parameter, public m_half
Definition: global.F90:206
This module implements the underlying real-space grid.
Definition: grid.F90:119
This module defines classes and functions for interaction partners.
subroutine, public dio_function_output(how, dir, fname, namespace, space, mesh, ff, unit, ierr, pos, atoms, grp, root)
Top-level IO routine for functions defined on the mesh.
Definition: io.F90:116
subroutine, public io_mkdir(fname, namespace, parents)
Definition: io.F90:361
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:120
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:410
This module contains the definition of the oct_t data type, which contains some of the basic informat...
This module holds the "opt_control_state_t" datatype, which contains a quantum-classical state.
this module contains the low-level part of the output system
Definition: output_low.F90:117
subroutine, public parse_block_string(blk, l, c, res, convert_to_c)
Definition: parser.F90:818
integer function, public parse_block(namespace, name, blk, check_varinfo_)
Definition: parser.F90:623
Optimal-control targets: abstract base class and public interface.
Definition: target.F90:132
subroutine target_tdcalc_tdlocal(tg, namespace, space, hm, gr, ions, ext_partners, psi, time, max_time)
subroutine target_output_tdlocal(tg, namespace, space, gr, dir, ions, hm, outp)
subroutine target_chi_tdlocal(tg, namespace, gr, kpoints, qcpsi_in, qcchi_out, ions)
subroutine target_end_tdlocal(tg, oct)
subroutine target_inh_tdlocal(tg, psi, gr, kpoints, time, inh, iter)
Inhomogeneous term for the time-dependent local target.
real(real64) function target_j1_tdlocal(tg, namespace, gr, kpoints, qcpsi, ions)
subroutine target_init_tdlocal(tg, gr, kpoints, namespace, space, ions, qcs, td, w0, oct, ep, restart)
subroutine target_build_tdlocal(tg, gr, time)
Definition: td.F90:116
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:134
This module defines the unit system, used for input and output.
type(unit_system_t), public units_out
Description of the grid, containing information on derivatives, stencil, and symmetries.
Definition: grid.F90:171
This is the datatype that contains the objects that are propagated: in principle this could be both t...
Abstract optimal-control target.
Definition: target.F90:172
Time-dependent local target operator.
int true(void)