Octopus
magnetic.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#include "global.h"
20
21module magnetic_oct_m
23 use comm_oct_m
24 use debug_oct_m
27 use global_oct_m
28 use grid_oct_m
29 use ions_oct_m
32 use mesh_oct_m
34 use mpi_oct_m
43 use unit_oct_m
45
46 implicit none
47
48 private
49 public :: &
58
59contains
60
61 ! ---------------------------------------------------------
62 subroutine magnetic_density(mesh, std, rho, md)
63 class(mesh_t), intent(in) :: mesh
64 type(states_elec_dim_t), intent(in) :: std
65 real(real64), intent(in) :: rho(:,:)
66 real(real64), intent(out) :: md(:,:)
67
68 push_sub(magnetic_density)
69
70 select case (std%ispin)
71 case (unpolarized)
72 md = m_zero
73
74 case (spin_polarized)
75 md = m_zero
76 md(1:mesh%np, 3) = rho(1:mesh%np, 1) - rho(1:mesh%np, 2)
77
78 case (spinors)
79 md(1:mesh%np, 1) = m_two*rho(1:mesh%np, 3)
80 md(1:mesh%np, 2) = -m_two*rho(1:mesh%np, 4)
81 md(1:mesh%np, 3) = rho(1:mesh%np, 1) - rho(1:mesh%np, 2)
82 end select
83
84 pop_sub(magnetic_density)
85 end subroutine magnetic_density
86
87
88 ! ---------------------------------------------------------
89 subroutine magnetic_moment(mesh, st, rho, mm)
90 class(mesh_t), intent(in) :: mesh
91 type(states_elec_t), intent(in) :: st
92 real(real64), intent(in) :: rho(:,:)
93 real(real64), intent(out) :: mm(3)
94
95 real(real64), allocatable :: md(:,:)
96
97 push_sub(states_elec_magnetic_moment)
98
99 safe_allocate(md(1:mesh%np, 1:3))
100 call magnetic_density(mesh, st%d, rho, md)
101
102 mm = dmf_integrate(mesh, 3, md)
103
104 safe_deallocate_a(md)
105
106 pop_sub(states_elec_magnetic_moment)
107 end subroutine magnetic_moment
108
109
110 ! ---------------------------------------------------------
111 subroutine write_magnetic_moments(mesh, st, ions, boundaries, lmm_r, iunit, namespace)
112 class(mesh_t), intent(in) :: mesh
113 type(states_elec_t), intent(in) :: st
114 type(ions_t), intent(in) :: ions
115 type(boundaries_t), intent(in) :: boundaries
116 real(real64), intent(in) :: lmm_r
117 integer, optional, intent(in) :: iunit
118 type(namespace_t), optional, intent(in) :: namespace
119
120 integer :: ia
121 real(real64) :: mm(max(mesh%box%dim, 3))
122 real(real64), allocatable :: lmm(:,:)
123
124 push_sub(write_magnetic_moments)
125
126 call magnetic_moment(mesh, st, st%rho, mm)
127 safe_allocate(lmm(1:max(mesh%box%dim, 3), 1:ions%natoms))
128 call magnetic_local_moments(mesh, st, ions, boundaries, st%rho, lmm_r, lmm)
129
130 message(1) = 'Total Magnetic Moment:'
131 call messages_info(1, iunit=iunit, namespace=namespace)
132 if (st%d%ispin == spin_polarized) then ! collinear spin
133 write(message(1), '(a,f10.6)') ' mz = ', mm(3)
134 call messages_info(1, iunit=iunit, namespace=namespace)
135 else if (st%d%ispin == spinors) then ! non-collinear
136 write(message(1), '(1x,3(a,f10.6,3x))') 'mx = ', mm(1),'my = ', mm(2),'mz = ', mm(3)
137 call messages_info(1, iunit=iunit, namespace=namespace)
138 end if
139
140 write(message(1), '(a,a,a,f7.3,a)') 'Local Magnetic Moments (sphere radius [', &
141 trim(units_abbrev(units_out%length)),'] = ', units_from_atomic(units_out%length, lmm_r), '):'
142 call messages_info(1, iunit=iunit, namespace=namespace)
143 if (st%d%ispin == spin_polarized) then ! collinear spin
144 write(message(1),'(a,6x,14x,a)') ' Ion','mz'
145 call messages_info(1, iunit=iunit, namespace=namespace)
146 do ia = 1, ions%natoms
147 write(message(1),'(i4,a10,f15.6)') ia, trim(ions%atom(ia)%species%get_label()), lmm(3, ia)
148 call messages_info(1, iunit=iunit, namespace=namespace)
149 end do
150 else if (st%d%ispin == spinors) then ! non-collinear
151 write(message(1),'(a,8x,13x,a,13x,a,13x,a)') ' Ion','mx','my','mz'
152 call messages_info(1, iunit=iunit, namespace=namespace)
153 do ia = 1, ions%natoms
154 write(message(1),'(i4,a10,9f15.6)') ia, trim(ions%atom(ia)%species%get_label()), lmm(:, ia)
155 call messages_info(1, iunit=iunit, namespace=namespace)
156 end do
157 end if
158
159 safe_deallocate_a(lmm)
160
162 end subroutine write_magnetic_moments
163
164 ! ---------------------------------------------------------
165 subroutine magnetic_local_moments(mesh, st, ions, boundaries, rho, rr, lmm)
166 class(mesh_t), intent(in) :: mesh
167 type(states_elec_t), intent(in) :: st
168 type(ions_t), intent(in) :: ions
169 type(boundaries_t), intent(in) :: boundaries
170 real(real64), intent(in) :: rho(:,:)
171 real(real64), intent(in) :: rr
172 real(real64), intent(out) :: lmm(max(mesh%box%dim, 3), ions%natoms)
173
174 integer :: ia, idir, is
175 real(real64), allocatable :: md(:, :)
176 type(submesh_t) :: sphere
177 real(real64) :: cosqr, sinqr
178 complex(real64), allocatable :: phase_spiral(:)
179
180 push_sub(magnetic_local_moments)
181
182 safe_allocate(md(1:mesh%np, 1:max(mesh%box%dim, 3)))
183
184 call magnetic_density(mesh, st%d, rho, md)
185 lmm = m_zero
186 do ia = 1, ions%natoms
187 call submesh_init(sphere, ions%space, mesh, ions%latt, ions%pos(:, ia), rr)
188
189 if (boundaries%spiral) then
190 safe_allocate(phase_spiral(1:sphere%np))
191 do is = 1, sphere%np
192 phase_spiral(is) = exp(+m_zi*sum((sphere%rel_x(:,is) + sphere%center - mesh%x(sphere%map(is),:)) &
193 *boundaries%spiral_q(1:mesh%box%dim)))
194 end do
195
196 if (mesh%box%dim>= 3) then
197 lmm(1,ia) = m_zero
198 lmm(2,ia) = m_zero
199
200 do is = 1, sphere%np
201 !There is a factor of 1/2 in phase_spiral
202 cosqr = real(phase_spiral(is), real64)
203 sinqr = aimag(phase_spiral(is))
204 lmm(1,ia) = lmm(1,ia)+md(sphere%map(is),1)*cosqr - md(sphere%map(is),2)*sinqr
205 lmm(2,ia) = lmm(2,ia)+md(sphere%map(is),1)*sinqr + md(sphere%map(is),2)*cosqr
206 end do
207 lmm(1,ia) = lmm(1,ia)*mesh%volume_element
208 lmm(2,ia) = lmm(2,ia)*mesh%volume_element
209 lmm(3,ia) = dsm_integrate_frommesh(mesh, sphere, md(1:mesh%np,3), reduce = .false.)
210 else
211 assert(.not. boundaries%spiral)
212 end if
213
214 safe_deallocate_a(phase_spiral)
215 else
216 do idir = 1, max(mesh%box%dim, 3)
217 lmm(idir, ia) = dsm_integrate_frommesh(mesh, sphere, md(1:mesh%np,idir), reduce = .false.)
218 end do
219 end if
220
221 call submesh_end(sphere)
222 end do
223
224 call mesh%allreduce(lmm)
225
226 safe_deallocate_a(md)
227
229 end subroutine magnetic_local_moments
230
231 ! ---------------------------------------------------------
232 subroutine magnetic_total_magnetization(mesh, st, qq, trans_mag)
233 class(mesh_t), intent(in) :: mesh
234 type(states_elec_t), intent(in) :: st
235 real(real64), intent(in) :: qq(:)
236 complex(real64), intent(out) :: trans_mag(6)
237
238 integer :: ip
239 complex(real64), allocatable :: tmp(:,:)
240 real(real64), allocatable :: md(:, :)
241 complex(real64) :: expqr
242
244
245 call profiling_in("TOTAL_MAGNETIZATION")
246
247 safe_allocate(tmp(1:mesh%np, 1:6))
248 safe_allocate(md(1:mesh%np, 1:max(mesh%box%dim, 3)))
249
250 call magnetic_density(mesh, st%d, st%rho, md)
251 do ip = 1, mesh%np
252 expqr = exp(-m_zi*sum(mesh%x(ip, 1:mesh%box%dim)*qq(1:mesh%box%dim)))
253 tmp(ip,1) = expqr*md(ip,1)
254 tmp(ip,2) = expqr*md(ip,2)
255 tmp(ip,3) = expqr*md(ip,3)
256 tmp(ip,4) = conjg(expqr)*md(ip,1)
257 tmp(ip,5) = conjg(expqr)*md(ip,2)
258 tmp(ip,6) = conjg(expqr)*md(ip,3)
259 end do
260
261 trans_mag = zmf_integrate(mesh, 6, tmp)
262
263 safe_deallocate_a(md)
264 safe_deallocate_a(tmp)
265
266 call profiling_out("TOTAL_MAGNETIZATION")
267
269 end subroutine magnetic_total_magnetization
270
271 ! ---------------------------------------------------------
275 subroutine magnetic_induced(namespace, gr, st, psolver, kpoints, a_ind, b_ind)
276 type(namespace_t), intent(in) :: namespace
277 type(grid_t), intent(in) :: gr
278 type(states_elec_t), intent(inout) :: st
279 type(poisson_t), intent(in) :: psolver
280 type(kpoints_t), intent(in) :: kpoints
281 real(real64), contiguous, intent(out) :: a_ind(:, :)
282 real(real64), contiguous, intent(out) :: b_ind(:, :)
283
285
286 integer :: idir
287 real(real64), allocatable :: jj(:, :, :)
288
289 push_sub(magnetic_induced)
290
291 ! If the states are real, we should never have reached here, but
292 ! just in case we return zero.
293 if (states_are_real(st)) then
294 a_ind = m_zero
295 b_ind = m_zero
296 pop_sub(magnetic_induced)
297 return
298 end if
299
300 safe_allocate(jj(1:gr%np_part, 1:gr%der%dim, 1:st%d%nspin))
301 call states_elec_calc_quantities(gr, st, kpoints, .false., paramagnetic_current = jj)
302
303 !We sum the current for up and down, valid for collinear and noncollinear spins
304 if (st%d%nspin > 1) then
305 do idir = 1, gr%der%dim
306 jj(:, idir, 1) = jj(:, idir, 1) + jj(:, idir, 2)
307 end do
308 end if
309
310 a_ind = m_zero
311 do idir = 1, gr%der%dim
312 call dpoisson_solve(psolver, namespace, a_ind(:, idir), jj(:, idir, 1))
313 end do
314 ! This minus sign is introduced here because the current that has been used
315 ! before is the "number-current density", and not the "charge-current density",
316 ! and therefore there is a minus sign missing (electrons are negative charges...)
317 a_ind(1:gr%np, 1:gr%der%dim) = - a_ind(1:gr%np, 1:gr%der%dim) / p_c
318
319 call dderivatives_curl(gr%der, a_ind, b_ind)
320
321 safe_deallocate_a(jj)
322 pop_sub(magnetic_induced)
323 end subroutine magnetic_induced
324
325 subroutine write_total_xc_torque(iunit, mesh, vxc, st)
326 integer, intent(in) :: iunit
327 class(mesh_t), intent(in) :: mesh
328 real(real64), intent(in) :: vxc(:,:)
329 type(states_elec_t), intent(in) :: st
330
331 real(real64), allocatable :: torque(:,:)
332 real(real64) :: tt(3)
333
334 push_sub(write_total_xc_torque)
335
336 safe_allocate(torque(1:mesh%np, 1:3))
337
338 call calc_xc_torque(mesh, vxc, st, torque)
339
340 tt = dmf_integrate(mesh, 3, torque)
341
342 if (mpi_grp_is_root(mpi_world)) then
343 write(iunit, '(a)') 'Total xc torque:'
344 write(iunit, '(1x,3(a,es10.3,3x))') 'Tx = ', tt(1),'Ty = ', tt(2),'Tz = ', tt(3)
345 end if
346
347 safe_deallocate_a(torque)
348
349 pop_sub(write_total_xc_torque)
350 end subroutine write_total_xc_torque
351
352 ! ---------------------------------------------------------
353 subroutine calc_xc_torque(mesh, vxc, st, torque)
354 class(mesh_t), intent(in) :: mesh
355 real(real64), intent(in) :: vxc(:,:)
356 type(states_elec_t), intent(in) :: st
357 real(real64), intent(inout) :: torque(:,:)
358
359 real(real64) :: mag(3), bxc(3)
360 integer :: ip
361
362 push_sub(calc_xc_torque)
363
364 assert(st%d%ispin == spinors)
365
366 do ip = 1, mesh%np
367 mag(1) = m_two * st%rho(ip, 3)
368 mag(2) = -m_two * st%rho(ip, 4)
369 mag(3) = st%rho(ip, 1) - st%rho(ip, 2)
370 bxc(1) = -m_two * vxc(ip, 3)
371 bxc(2) = m_two * vxc(ip, 4)
372 bxc(3) = -(vxc(ip, 1) - vxc(ip, 2))
373 torque(ip, 1) = mag(2) * bxc(3) - mag(3) * bxc(2)
374 torque(ip, 2) = mag(3) * bxc(1) - mag(1) * bxc(3)
375 torque(ip, 3) = mag(1) * bxc(2) - mag(2) * bxc(1)
376 end do
377
378 pop_sub(calc_xc_torque)
379 end subroutine calc_xc_torque
380
381
382
383end module magnetic_oct_m
384
385!! Local Variables:
386!! mode: f90
387!! coding: utf-8
388!! End:
double exp(double __x) __attribute__((__nothrow__
Module implementing boundary conditions in Octopus.
Definition: boundaries.F90:122
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
subroutine, public dderivatives_curl(der, ff, op_ff, ghost_update, set_bc)
apply the curl operator to a vector of mesh functions
integer, parameter, public unpolarized
Parameters...
integer, parameter, public spinors
integer, parameter, public spin_polarized
real(real64), parameter, public m_two
Definition: global.F90:190
real(real64), parameter, public m_zero
Definition: global.F90:188
complex(real64), parameter, public m_zi
Definition: global.F90:202
real(real64), parameter, public p_c
Electron gyromagnetic ratio, see Phys. Rev. Lett. 130, 071801 (2023)
Definition: global.F90:224
This module implements the underlying real-space grid.
Definition: grid.F90:117
subroutine, public magnetic_local_moments(mesh, st, ions, boundaries, rho, rr, lmm)
Definition: magnetic.F90:259
subroutine, public write_magnetic_moments(mesh, st, ions, boundaries, lmm_r, iunit, namespace)
Definition: magnetic.F90:205
subroutine, public calc_xc_torque(mesh, vxc, st, torque)
Definition: magnetic.F90:447
subroutine, public magnetic_moment(mesh, st, rho, mm)
Definition: magnetic.F90:183
subroutine, public magnetic_total_magnetization(mesh, st, qq, trans_mag)
Definition: magnetic.F90:326
subroutine, public write_total_xc_torque(iunit, mesh, vxc, st)
Definition: magnetic.F90:419
subroutine, public magnetic_density(mesh, std, rho, md)
Definition: magnetic.F90:156
subroutine, public magnetic_induced(namespace, gr, st, psolver, kpoints, a_ind, b_ind)
This subroutine receives as input a current, and produces as an output the vector potential that it i...
Definition: magnetic.F90:369
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:118
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:160
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:616
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
subroutine, public dpoisson_solve(this, namespace, pot, rho, all_nodes, kernel, reset)
Calculates the Poisson equation. Given the density returns the corresponding potential.
Definition: poisson.F90:892
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:623
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:552
pure logical function, public states_are_real(st)
This module handles spin dimensions of the states and the k-point distribution.
subroutine, public states_elec_calc_quantities(gr, st, kpoints, nlcc, kinetic_energy_density, paramagnetic_current, density_gradient, density_laplacian, gi_kinetic_energy_density, st_end)
calculated selected quantities
real(real64) function, public dsm_integrate_frommesh(mesh, sm, ff, reduce)
Definition: submesh.F90:1206
subroutine, public submesh_end(this)
Definition: submesh.F90:736
subroutine, public submesh_init(this, space, mesh, latt, center, rc)
Definition: submesh.F90:280
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_system_t), public units_out
This class contains information about the boundary conditions.
Definition: boundaries.F90:157
Description of the grid, containing information on derivatives, stencil, and symmetries.
Definition: grid.F90:169
Describes mesh distribution to nodes.
Definition: mesh.F90:186
The states_elec_t class contains all electronic wave functions.
A submesh is a type of mesh, used for the projectors in the pseudopotentials It contains points on a ...
Definition: submesh.F90:175