36 use,
intrinsic :: iso_fortran_env
70 real(real64),
allocatable :: rho(:)
71 real(real64),
allocatable :: td_fitness(:)
72 real(real64) :: density_weight
73 real(real64) :: curr_weight
74 integer :: strt_iter_curr_tg
75 real(real64),
allocatable :: spatial_curr_wgt(:)
91 subroutine target_init_density(tg, gr, kpoints, namespace, space, ions, qcs, td, w0, oct, ep, restart)
92 class(target_density_t),
intent(inout) :: tg
93 type(grid_t),
intent(in) :: gr
94 type(kpoints_t),
intent(in) :: kpoints
95 type(namespace_t),
intent(in) :: namespace
96 class(space_t),
intent(in) :: space
97 type(ions_t),
intent(in) :: ions
98 type(opt_control_state_t),
intent(inout) :: qcs
99 type(td_t),
intent(in) :: td
100 real(real64),
intent(in) :: w0
101 type(oct_t),
intent(in) :: oct
102 type(epot_t),
intent(inout) :: ep
103 type(restart_t),
intent(inout) :: restart
105 integer :: ip, ist, jst, cstr_dim(space%dim), ib, idim, jj, no_constraint, no_ptpair, iqn
107 real(real64) :: psi_re, psi_im, xx(space%dim), rr, fact, xend, xstart
108 real(real64),
allocatable :: xp(:), tmp_box(:, :)
109 complex(real64),
allocatable :: rotation_matrix(:, :), psi(:, :)
110 type(states_elec_t) :: tmp_st
111 character(len=1024) :: expression
112 type(states_elec_t),
pointer :: stin
118 message(1) =
'Info: Target is a combination of a density and/or a current functional.'
121 tg%move_ions = td%ions_dyn%ions_move()
126 tg%strt_iter_curr_tg = 0
152 tg%density_weight =
m_one
153 safe_allocate(tg%rho(1:gr%np))
155 call parse_variable(namespace,
'OCTTargetDensity',
"0", expression)
157 if (trim(expression) ==
'OCTTargetDensityFromState')
then
159 if (
parse_block(namespace,
'OCTTargetDensityFromState', blk) == 0)
then
165 safe_allocate(rotation_matrix(1:tmp_st%nst, 1:tmp_st%nst))
169 do ist = 1, tg%st%nst
175 safe_allocate(psi(1:gr%np, 1:tg%st%d%dim))
177 do iqn = tg%st%d%kpt%start, tg%st%d%kpt%end
185 do ist = tg%st%st_start, tg%st%st_end
192 safe_deallocate_a(rotation_matrix)
193 safe_deallocate_a(psi)
198 tg%rho(ip) = sum(tg%st%rho(ip, 1:tg%st%d%spin_channels))
209 call mesh_r(gr, ip, rr, coords = xx)
216 tg%rho = (-tg%st%val_charge) * tg%rho/rr
220 tg%density_weight =
m_zero
304 select case (tg%curr_functional)
307 if (.not.
allocated(stin%current))
then
308 safe_allocate(stin%current( 1:gr%np_part, 1:space%dim, 1:stin%d%nspin))
314 write(
message(1),
'(a,i3)')
'Info: OCTCurrentFunctional = ', tg%curr_functional
315 write(
message(2),
'(a,f8.3)')
'Info: OCTCurrentWeight = ', tg%curr_weight
319 call parse_variable(namespace,
'OCTStartIterCurrTg', 0, tg%strt_iter_curr_tg)
320 if (tg%strt_iter_curr_tg < 0)
then
322 elseif (tg%strt_iter_curr_tg >= td%max_iter)
then
326 write(
message(2),
'(a,i8)')
'Info: OCTStartIterCurrTg = ', tg%strt_iter_curr_tg
329 safe_allocate(tg%td_fitness(0:td%max_iter))
332 tg%strt_iter_curr_tg = 0
336 if (
parse_block(namespace,
'OCTSpatialCurrWeight', blk) == 0)
then
337 safe_allocate(tg%spatial_curr_wgt(1:gr%np_part))
338 safe_allocate(xp(1:gr%np_part))
339 safe_allocate(tmp_box(1:gr%np_part, 1:space%dim))
344 do ib = 1, no_constraint
346 if (idim <= 0 .or. idim > space%dim)
then
347 write(
message(1),
'(a,i3)')
'Error in "OCTSpatialCurrWeight" block, line:', ib
348 write(
message(2),
'(a)' )
'"dimension" has to be positive'
349 write(
message(3),
'(a)' )
'and must not exceed dimensions of the system.'
353 xp(1:gr%np_part) = gr%x_t(1:gr%np_part, idim)
358 if (mod(no_ptpair,2) /= 0)
then
359 write(
message(1),
'(a,i3)')
'Error in "OCTSpatialCurrWeight" block, line:', ib
360 write(
message(2),
'(a)' )
'Each interval needs start and end point!'
364 do jj= 2, no_ptpair, 2
368 if (xstart >= xend)
then
369 write(
message(1),
'(a,i3)')
'Error in "OCTSpatialCurrWeight" block, line:', ib
370 write(
message(2),
'(a)' )
'Set "startpoint" < "endpoint" '
374 do ip = 1, gr%np_part
375 tmp_box(ip,idim) = tmp_box(ip,idim) +
m_one/(
m_one+
exp(-fact*(xp(ip)-xstart))) - &
382 do idim = 1, space%dim
383 if (cstr_dim(idim) == 0) tmp_box(:,idim) =
m_one
385 tg%spatial_curr_wgt(1:gr%np_part) = product(tmp_box(1:gr%np_part, 1:space%dim),2)
386 safe_deallocate_a(xp)
387 safe_deallocate_a(tmp_box)
404 type(
oct_t),
intent(in) :: oct
408 safe_deallocate_a(tg%rho)
409 safe_deallocate_a(tg%spatial_curr_wgt)
410 select case (tg%curr_functional)
412 safe_deallocate_a(tg%td_fitness)
423 class(
space_t),
intent(in) :: space
424 type(
grid_t),
intent(in) :: gr
425 character(len=*),
intent(in) :: dir
426 type(
ions_t),
intent(in) :: ions
435 if (tg%density_weight >
m_zero)
then
437 tg%rho,
units_out%length**(-space%dim), ierr, pos=ions%pos, atoms=ions%atom)
448 real(real64) function target_j1_density(tg, namespace, gr, kpoints, qcpsi, ions) result(j1)
451 type(
grid_t),
intent(in) :: gr
454 type(
ions_t),
optional,
intent(in) :: ions
456 integer :: ip, maxiter
457 real(real64) :: currfunc_tmp
458 real(real64),
allocatable :: local_function(:)
461 push_sub(target_j1_density)
465 if (tg%density_weight >
m_zero)
then
466 safe_allocate(local_function(1:gr%np))
468 local_function(ip) = - (
sqrt(psi%rho(ip, 1)) -
sqrt(tg%rho(ip)) )**2
471 safe_deallocate_a(local_function)
482 maxiter =
size(tg%td_fitness) - 1
483 currfunc_tmp =
m_half * tg%dt * tg%td_fitness(tg%strt_iter_curr_tg) + &
484 m_half * tg%dt * tg%td_fitness(maxiter) + &
485 tg%dt * sum(tg%td_fitness(tg%strt_iter_curr_tg+1:maxiter-1))
487 if (
conf%devel_version)
then
488 write(
message(1),
'(6x,a,f12.5)')
" => Other functional = ", j1
489 write(
message(2),
'(6x,a,f12.5)')
" => Current functional = ", currfunc_tmp
493 j1 = j1 + currfunc_tmp
497 pop_sub(target_j1_density)
505 type(namespace_t),
intent(in) :: namespace
506 type(grid_t),
intent(in) :: gr
507 type(kpoints_t),
intent(in) :: kpoints
508 type(opt_control_state_t),
target,
intent(inout) :: qcpsi_in
509 type(opt_control_state_t),
target,
intent(inout) :: qcchi_out
510 type(ions_t),
intent(in) :: ions
512 integer :: no_electrons, ip, ist, ib, ik
513 complex(real64),
allocatable :: zpsi(:, :)
514 type(states_elec_t),
pointer :: psi_in, chi_out
518 psi_in => opt_control_point_qs(qcpsi_in)
519 chi_out => opt_control_point_qs(qcchi_out)
521 do ik = chi_out%d%kpt%start, chi_out%d%kpt%end
522 do ib = chi_out%group%block_start, chi_out%group%block_end
523 call batch_set_zero(chi_out%group%psib(ib, ik))
527 no_electrons = -nint(psi_in%val_charge)
529 if (tg%density_weight > m_zero)
then
531 select case (psi_in%d%ispin)
533 assert(psi_in%nik == 1)
535 safe_allocate(zpsi(1:gr%np, 1))
537 if (no_electrons == 1)
then
539 call states_elec_get_state(psi_in, gr, 1, 1, zpsi)
542 zpsi(ip, 1) =
sqrt(tg%rho(ip))*
exp(m_zi*
atan2(aimag(zpsi(ip, 1)), real(zpsi(ip, 1),real64) ))
545 call states_elec_set_state(chi_out, gr, 1, 1, zpsi)
548 do ist = psi_in%st_start, psi_in%st_end
550 call states_elec_get_state(psi_in, gr, ist, 1, zpsi)
553 if (psi_in%rho(ip, 1) > 1.0e-8_real64)
then
554 zpsi(ip, 1) = psi_in%occ(ist, 1)*
sqrt(tg%rho(ip)/psi_in%rho(ip, 1))*zpsi(ip, 1)
560 call states_elec_set_state(chi_out, gr, ist, 1, zpsi)
565 case (spin_polarized)
566 message(1) =
'Error in target.target_chi_density: spin_polarized.'
567 call messages_fatal(1)
569 message(1) =
'Error in target.target_chi_density: spinors.'
570 call messages_fatal(1)
575 if (tg%curr_functional /= oct_no_curr)
then
576 if (target_mode(tg) == oct_targetmode_static)
then
577 call chi_current(tg, gr, kpoints, m_one, psi_in, chi_out)
590 subroutine target_tdcalc_density(tg, namespace, space, hm, gr, ions, ext_partners, psi, time, max_time)
592 type(namespace_t),
intent(in) :: namespace
593 class(space_t),
intent(in) :: space
594 type(hamiltonian_elec_t),
intent(inout) :: hm
595 type(grid_t),
intent(in) :: gr
596 type(ions_t),
intent(inout) :: ions
597 type(partner_list_t),
intent(in) :: ext_partners
598 type(states_elec_t),
intent(inout) :: psi
599 integer,
intent(in) :: time
600 integer,
intent(in) :: max_time
604 tg%td_fitness(time) = m_zero
606 if (time >= tg%strt_iter_curr_tg)
then
620 type(states_elec_t),
intent(inout) :: psi
621 type(grid_t),
intent(in) :: gr
622 type(kpoints_t),
intent(in) :: kpoints
623 real(real64),
intent(in) :: time
624 type(states_elec_t),
intent(inout) :: inh
625 integer,
intent(in) :: iter
631 do ik = inh%d%kpt%start, inh%d%kpt%end
632 do ib = inh%group%block_start, inh%group%block_end
633 call batch_set_zero(inh%group%psib(ib, ik))
637 if (abs(nint(time/tg%dt)) >= tg%strt_iter_curr_tg)
then
638 call chi_current(tg, gr, kpoints, -1.0_real64, psi, inh)
649 real(real64) function jcurr_functional(tg, gr, kpoints, psi) result(jcurr)
651 type(grid_t),
intent(in) :: gr
652 type(kpoints_t),
intent(in) :: kpoints
653 type(states_elec_t),
intent(inout) :: psi
656 real(real64),
allocatable :: semilocal_function(:)
662 safe_allocate(semilocal_function(1:gr%np))
663 semilocal_function = m_zero
665 select case (tg%curr_functional)
667 semilocal_function = m_zero
669 case (oct_curr_square,oct_curr_square_td)
670 call states_elec_calc_quantities(gr, psi, kpoints, .false., paramagnetic_current=psi%current)
672 semilocal_function(ip) = sum(psi%current(ip, 1:gr%box%dim, 1)**2)
675 case (oct_max_curr_ring)
676 call states_elec_calc_quantities(gr, psi, kpoints, .false., paramagnetic_current=psi%current)
678 if (gr%box%dim /= 2)
then
679 call messages_not_implemented(
'Target for dimension != 2')
684 semilocal_function(ip) = psi%current(ip, 2, 1) * gr%x(1, ip) - &
685 psi%current(ip, 1, 1) * gr%x(2, ip)
688 message(1) =
'Error in target.jcurr_functional: chosen target does not exist'
689 call messages_fatal(1)
695 semilocal_function(ip) = semilocal_function(ip) * tg%spatial_curr_wgt(ip)
699 jcurr = tg%curr_weight * dmf_integrate(gr, semilocal_function)
701 safe_deallocate_a(semilocal_function)
710 subroutine chi_current(tg, gr, kpoints, factor, psi_in, chi)
712 type(grid_t),
intent(in) :: gr
713 type(kpoints_t),
intent(in) :: kpoints
714 real(real64),
intent(in) :: factor
715 type(states_elec_t),
intent(inout) :: psi_in
716 type(states_elec_t),
intent(inout) :: chi
718 complex(real64),
allocatable :: grad_psi_in(:,:,:), zpsi(:, :), zchi(:, :)
719 real(real64),
allocatable :: div_curr_psi_in(:,:)
720 integer :: ip, ist, idim
723 safe_allocate(grad_psi_in(1:gr%np_part, 1:gr%box%dim, 1))
725 if (target_mode(tg) == oct_targetmode_td)
then
726 call states_elec_calc_quantities(gr, psi_in, kpoints, .false., paramagnetic_current=psi_in%current)
729 select case (tg%curr_functional)
733 case (oct_curr_square,oct_curr_square_td)
737 do idim = 1, gr%box%dim
738 do ip = 1, gr%np_part
739 psi_in%current(ip, idim, 1) = psi_in%current(ip, idim, 1) * tg%spatial_curr_wgt(ip)
744 safe_allocate(div_curr_psi_in(1:gr%np_part, 1))
745 safe_allocate(zpsi(1:gr%np_part, 1:psi_in%d%dim))
746 safe_allocate(zchi(1:gr%np_part, 1:psi_in%d%dim))
748 call dderivatives_div(gr%der, psi_in%current(:, :, 1), div_curr_psi_in(:,1))
751 do ist = psi_in%st_start, psi_in%st_end
753 call states_elec_get_state(psi_in, gr, ist, 1, zpsi)
754 call states_elec_get_state(chi, gr, ist, 1, zchi)
756 call zderivatives_grad(gr%der, zpsi(:, 1), grad_psi_in(:, :, 1))
758 do idim = 1, psi_in%d%dim
760 zchi(ip, idim) = zchi(ip, idim) - factor*m_zi*tg%curr_weight* &
761 (m_two*sum(psi_in%current(ip, 1:gr%box%dim, 1)*grad_psi_in(ip, 1:gr%box%dim, 1)) + &
762 div_curr_psi_in(ip, 1)*zpsi(ip, idim))
766 call states_elec_set_state(chi, gr, ist, 1, zchi)
770 safe_deallocate_a(div_curr_psi_in)
771 safe_deallocate_a(zpsi)
772 safe_deallocate_a(zchi)
774 case (oct_max_curr_ring)
776 safe_allocate(zpsi(1:gr%np_part, 1:psi_in%d%dim))
777 safe_allocate(zchi(1:gr%np_part, 1:psi_in%d%dim))
779 do ist = psi_in%st_start, psi_in%st_end
781 call states_elec_get_state(psi_in, gr, ist, 1, zpsi)
782 call states_elec_get_state(chi, gr, ist, 1, zchi)
784 call zderivatives_grad(gr%der, zpsi(:, 1), grad_psi_in(:, :, 1))
789 zchi(ip, 1) = zchi(ip, 1) + factor*m_zi*tg%curr_weight*tg%spatial_curr_wgt(ip)* &
790 (grad_psi_in(ip, 1, 1)*gr%x(2, ip) - grad_psi_in(ip, 2, 1)*gr%x(1, ip))
796 zchi(ip, 1) = zchi(ip, 1) + factor*m_zi*tg%curr_weight* &
797 (grad_psi_in(ip, 1, 1)*gr%x(2, ip) - grad_psi_in(ip, 2, 1)*gr%x(1, ip))
802 call states_elec_set_state(chi, gr, ist, 1, zchi)
806 safe_deallocate_a(zpsi)
807 safe_deallocate_a(zchi)
810 message(1) =
'Error in target.chi_current: chosen target does not exist'
811 call messages_fatal(1)
814 safe_deallocate_a(grad_psi_in)
821 logical pure function is_spatial_curr_wgt(tg)
double exp(double __x) __attribute__((__nothrow__
double sqrt(double __x) __attribute__((__nothrow__
double atan2(double __y, double __x) __attribute__((__nothrow__
This module implements common operations on batches of mesh functions.
This module implements a calculator for the density and defines related functions.
subroutine, public density_calc(st, gr, density, istin)
Computes the density from the orbitals in st.
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
real(real64), parameter, public m_zero
type(conf_t), public conf
Global instance of Octopus configuration.
complex(real64), parameter, public m_z1
real(real64), parameter, public m_half
real(real64), parameter, public m_one
This module implements the underlying real-space grid.
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.
subroutine, public io_mkdir(fname, namespace, parents)
This module is intended to contain "only mathematical" functions and procedures.
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
pure subroutine, public mesh_r(mesh, ip, rr, origin, coords)
return the distance to the origin for a given grid point
subroutine, public messages_variable_is_block(namespace, name)
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
subroutine, public messages_input_error(namespace, var, details, row, column)
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
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.
type(states_elec_t) function, pointer, public opt_control_point_qs(ocs)
this module contains the low-level part of the output system
logical function, public parse_is_defined(namespace, name)
integer function, public parse_block(namespace, name, blk, check_varinfo_)
pure logical function, public states_are_real(st)
subroutine, public states_elec_end(st)
finalize the states_elec_t object
subroutine, public states_elec_deallocate_wfns(st)
Deallocates the KS wavefunctions defined within a states_elec_t structure.
subroutine, public states_elec_copy(stout, stin, exclude_wfns, exclude_eigenval, special)
make a (selective) copy of a states_elec_t object
This module handles reading and writing restart information for the states_elec_t.
subroutine, public states_elec_look_and_load(restart, namespace, space, st, mesh, kpoints, fixed_occ, is_complex, packed)
subroutine, public conv_to_c_string(str)
converts to c string
logical pure function is_spatial_curr_wgt(tg)
subroutine target_chi_density(tg, namespace, gr, kpoints, qcpsi_in, qcchi_out, ions)
subroutine target_tdcalc_density(tg, namespace, space, hm, gr, ions, ext_partners, psi, time, max_time)
subroutine target_inh_density(tg, psi, gr, kpoints, time, inh, iter)
Inhomogeneous term for the current density target.
subroutine target_end_density(tg, oct)
real(real64) function target_j1_density(tg, namespace, gr, kpoints, qcpsi, ions)
subroutine target_init_density(tg, gr, kpoints, namespace, space, ions, qcs, td, w0, oct, ep, restart)
subroutine chi_current(tg, gr, kpoints, factor, psi_in, chi)
subroutine target_output_density(tg, namespace, space, gr, dir, ions, hm, outp)
real(real64) function jcurr_functional(tg, gr, kpoints, psi)
Calculates a current functional that may be combined with other functionals found in function target_...
Optimal-control targets: abstract base class and public interface.
integer, parameter, public oct_targetmode_static
integer, parameter, public oct_targetmode_td
integer, parameter, public oct_max_curr_ring
integer, parameter, public oct_no_curr
integer, parameter, public oct_curr_square_td
integer, parameter, public oct_curr_square
integer pure function, public target_mode(tg)
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
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.
!brief The oct_t datatype stores the basic information about how the OCT run is done.
This is the datatype that contains the objects that are propagated: in principle this could be both t...
The states_elec_t class contains all electronic wave functions.
Target on the density and/or the current.
Abstract optimal-control target.