Branch data Line data Source code
1 : : !!****m* ABINIT/m_spin_ncfile
2 : : !! NAME
3 : : !! m_spin_ncfile
4 : : !!
5 : : !! FUNCTION
6 : : !! This module contains the wrapper for writting spin hist netcdf file.
7 : : !! Unlike the m_spin_potential, inside netcdf, there should be not only the
8 : : !! data of magnetic atoms, but also the whole lattice (which do not move).
9 : : !!
10 : : !! Datatypes:
11 : : !! spin_ncfile_t
12 : : !!
13 : : !! Subroutines:
14 : : !! * spin_ncfile_t_init
15 : : !! * spin_ncfile_t_write_parameters (write parameters)
16 : : !! * spin_ncfile_t_def_sd (define spin dynamics related dimensions and ids)
17 : : !! * spin_ncfile_t_write_primitive_cell (write primitive cell information)
18 : : !! * spin_ncfile_t_write_supercell (write supercell information)
19 : : !! * spin_ncfile_t_write_one_step (write one step of spin dynamics)
20 : : !! * spin_ncfile_t_close (close and save netcdf file)
21 : : !!
22 : : !! TODO hexu: should consider carefully what to write.
23 : : !!
24 : : !! COPYRIGHT
25 : : !! Copyright (C) 2001-2026 ABINIT group (hexu)
26 : : !! This file is distributed under the terms of the
27 : : !! GNU General Public License, see ~abinit/COPYING
28 : : !! or http://www.gnu.org/copyleft/gpl.txt .
29 : : !! For the initials of contributors, see ~abinit/doc/developers/contributors.txt .
30 : : !!
31 : : !! SOURCE
32 : :
33 : : #if defined HAVE_CONFIG_H
34 : : #include "config.h"
35 : : #endif
36 : :
37 : :
38 : : #include "abi_common.h"
39 : :
40 : : module m_spin_ncfile
41 : : use defs_basis
42 : : use m_abicore
43 : : use m_errors
44 : : use m_xmpi
45 : : use m_nctk
46 : : use m_spin_hist , only: spin_hist_t
47 : : use m_spin_primitive_potential, only: spin_primitive_potential_t
48 : : use m_spin_potential , only: spin_potential_t
49 : : use m_multibinit_dataset, only: multibinit_dtset_type
50 : : !use m_multibinit_supercell, only: mb_supercell_t
51 : : use m_multibinit_cell, only: mbcell_t, mbsupercell_t
52 : : use m_spin_observables, only : spin_observable_t
53 : : use netcdf
54 : :
55 : : implicit none
56 : :
57 : : !!***
58 : :
59 : : type spin_ncfile_t
60 : : logical :: isopen=.False. ! if the file is open
61 : : ! dimensions
62 : : integer :: three, nspin, natoms, ntime, ntypat, nsublatt
63 : : ! three: 3
64 : : ! nspin: number of spin
65 : : ! natoms: number of atoms in a structure >=nspin
66 : : ! ntime: number of time step
67 : : ! ntypat: number of
68 : : ! nsublatt: number of spin sublattice
69 : :
70 : : ! file id
71 : : integer :: ncerr, ncid
72 : : ! variable id
73 : : integer :: xred_id, typat_id, znucl_id, label_id, spin_index_id
74 : : integer :: acell_id, rprimd_id
75 : : ! variable ids for spin dynamics
76 : : integer :: entropy_id, etotal_id, S_id, snorm_id, dsdt_id
77 : : integer :: heff_id, time_id, itime_id
78 : : ! entropy
79 : : ! etotal: total energy
80 : : ! S: spin
81 : : ! snorm: magnetic moment. (norm of S)
82 : : ! dsdt: dS/dt
83 : : ! heff: effective magnetic field from derivative of E
84 : : ! time: time
85 : : ! iteme: index of time step
86 : : integer :: Mst_sub_id, Mst_sub_norm_id, Mst_norm_total_id, Snorm_total_id
87 : : ! Mst_sub: magnetic moment of every sub lattice, \sum_(i in I) Si, where I is a sublattice
88 : : ! Mst_sub_norm: norm of magnetic momemt of every sub lattice |\sum_(i in I) Si|
89 : : ! Mst_norm_total: sum of the norm of magnetic moment of every sublattice \sum_I |\sum_(i in I) Si|
90 : : ! Snorm_total: \sum_i |Si|
91 : :
92 : : ! thermo obs
93 : : integer :: chi_id, binderU4_id, Cv_id
94 : : !chi: susceptibility
95 : : ! binder U4:
96 : : ! Cv: Specific heat
97 : :
98 : : ! variable ids for spin/lattice coupling
99 : : ! TODO: How to do this?
100 : : integer :: ihist_g_id
101 : :
102 : : integer :: itime
103 : : ! itime: time index
104 : :
105 : : integer :: write_traj=0
106 : : !whether to write the trajectory (S(t))
107 : :
108 : : character(len=fnlen) :: filename
109 : : ! netcdf filename
110 : : contains
111 : : ! initialize
112 : : procedure :: initialize
113 : : ! define variables for trajectory
114 : : procedure :: def_spindynamics_var
115 : : ! define variables for observables
116 : : procedure :: def_observable_var
117 : : ! write one step of hist
118 : : procedure :: write_one_step
119 : : ! write the primitive cell information
120 : : procedure :: write_primitive_cell
121 : : ! write supercell information
122 : : procedure :: write_supercell
123 : : ! write parameters related to simulation
124 : : procedure :: write_parameters
125 : : ! close netcdf file
126 : : procedure :: close
127 : : end type spin_ncfile_t
128 : :
129 : : contains
130 : :
131 : : !-----------------------------------------------------------------------
132 : : !> @brief initialize
133 : : !> open netcdf file
134 : : !> @param [in] filename: the netcdf filename
135 : : !> @param [in] write_traj: whether to write full trajectory
136 : : !-----------------------------------------------------------------------
137 : :
138 : 2 : subroutine initialize(self, filename, write_traj)
139 : :
140 : : class(spin_ncfile_t), intent(inout):: self
141 : : character(len=*),intent(in) :: filename
142 : : integer, intent(in) :: write_traj
143 : : integer :: ncerr
144 : 2 : self%itime=0
145 : 2 : self%write_traj=write_traj
146 : 2 : self%filename=trim(filename)
147 : 2 : self%isopen=.False.
148 : :
149 : 2 : write(std_out,*) "Write iteration in spin history file "//trim(self%filename)//"."
150 : : ! Create netCDF file
151 : 2 : ncerr = nf90_create(path=trim(filename), cmode=NF90_CLOBBER, ncid=self%ncid)
152 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when creating netcdf history file")
153 : 2 : self%isopen=.True.
154 : 2 : ncerr =nf90_enddef(self%ncid)
155 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when ending def mode in spin netcdf history file")
156 [ + - ]: 2 : end subroutine initialize
157 : :
158 : :
159 : : !-----------------------------------------------------------------------
160 : : !> @brief define variables for spin dynamics trajectory (and energy)
161 : : !> @param [in] hist: the spin hist object (not histfile!)
162 : : !-----------------------------------------------------------------------
163 : 2 : subroutine def_spindynamics_var(self, hist)
164 : : class(spin_ncfile_t), intent(inout) :: self
165 : : type(spin_hist_t),intent(in) :: hist
166 : : integer :: ncerr
167 : :
168 : 2 : ncerr = nf90_redef(self%ncid)
169 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when starting defining trajectory variables in spin history file.")
170 : : ! define dimensions
171 : 2 : ncerr=nf90_def_dim(self%ncid, "three", 3, self%three)
172 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when defining dimension three in spin history file.")
173 : 2 : ncerr=nf90_def_dim(self%ncid, "nspin", hist%nspin, self%nspin )
174 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when defining dimension nspin in spin history file.")
175 : 2 : ncerr=nf90_def_dim(self%ncid, "ntime", nf90_unlimited, self%ntime)
176 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when defining dimension ntime in spin history file.")
177 : : !call ab_define_var(ncid,dim1,typat_id,NF90_DOUBLE,&
178 : : ! & "typat","types of atoms","dimensionless" )
179 : :
180 [ + - ]: 2 : if(self%write_traj==1) then
181 : : call ab_define_var(self%ncid, (/ self%three, self%nspin, self%ntime /), &
182 [ + + ]: 8 : & self%S_id, NF90_DOUBLE, "S", "Spin orientations", "dimensionless")
183 : :
184 : : call ab_define_var(self%ncid, (/ self%nspin, self%ntime /), &
185 [ + + ]: 6 : & self%snorm_id, NF90_DOUBLE, "snorm", "Spin norm2", "Mu_B")
186 : : call ab_define_var(self%ncid, (/ self%three, self%nspin, self%ntime /), &
187 [ + + ]: 8 : & self%dsdt_id, NF90_DOUBLE, "dsdt", "Spin orientations derivative to time", "1/s")
188 : :
189 : : call ab_define_var(self%ncid, (/ self%three, self%nspin, self%ntime /), &
190 [ + + ]: 8 : & self%Heff_id, NF90_DOUBLE, "Heff", "Effective spin torque", "Tesla")
191 : : endif
192 : :
193 : : call ab_define_var(self%ncid, (/ self%ntime /), &
194 [ + + ]: 4 : & self%time_id, NF90_DOUBLE, "time", "time", "s")
195 : : call ab_define_var(self%ncid, (/ self%ntime /), &
196 [ + + ]: 4 : & self%etotal_id, NF90_DOUBLE, "etotal", "TOTAL energy", "Joule")
197 : : call ab_define_var(self%ncid, (/ self%ntime /), &
198 [ + + ]: 4 : & self%entropy_id, NF90_DOUBLE, "entropy", "Entropy", "Joule/K")
199 : :
200 : : call ab_define_var(self%ncid, (/ self%ntime /), &
201 [ + + ]: 4 : & self%itime_id, NF90_INT, "itime", "index of time in spin timeline", "1")
202 : :
203 : 2 : ncerr=nf90_enddef(self%ncid)
204 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when finishing defining variables in spin history file.")
205 : :
206 : 2 : end subroutine def_spindynamics_var
207 : :
208 : :
209 : : !-----------------------------------------------------------------------
210 : : !> @brief define varibles of observables
211 : : !> @param [in] ob: the spin observalble object
212 : : !-----------------------------------------------------------------------
213 : 2 : subroutine def_observable_var(self, ob)
214 : : class(spin_ncfile_t), intent(inout) :: self
215 : : type(spin_observable_t), intent(in) :: ob
216 : : integer ncerr
217 : :
218 : 2 : ncerr = nf90_redef(self%ncid)
219 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when defining observable variables in spin history file.")
220 : 2 : ncerr = nf90_def_dim(self%ncid, "nsublatt", ob%nsublatt, self%nsublatt)
221 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when defining dimension nsublatt in spin history file.")
222 : : call ab_define_var(self%ncid, (/self%three, self%nsublatt, self%ntime/),&
223 [ + + ]: 8 : & self%Mst_sub_id, NF90_DOUBLE, "Mst_sub", "Sublattice staggered M", "Bohr magneton")
224 : : call ab_define_var(self%ncid, (/ self%nsublatt, self%ntime/), &
225 : : & self%Mst_sub_norm_id, NF90_DOUBLE, "Mst_sub_norm", &
226 [ + + ]: 6 : & "Norm of sublattice staggered M", "Bohr magneton")
227 : : call ab_define_var(self%ncid, (/self%ntime/), self%Mst_norm_total_id, &
228 [ + + ]: 4 : & NF90_DOUBLE, "Mst_norm_total", "total Norm of sublattice M", "Bohr magneton")
229 : : call ab_define_var(self%ncid, (/self%ntime/), self%Snorm_total_id, &
230 [ + + ]: 4 : & NF90_DOUBLE, "Snorm_sub", "Snorm of sublattice", "Bohr magneton")
231 : :
232 [ + - ]: 2 : if(ob%calc_thermo_obs)then
233 : : call ab_define_var(self%ncid, (/self%ntime/), self%binderU4_id, &
234 [ + + ]: 4 : & NF90_DOUBLE, "BinderU4", "Binder U4", "1")
235 : : call ab_define_var(self%ncid, (/self%ntime/), self%Cv_id, &
236 [ + + ]: 4 : & NF90_DOUBLE, "Cv", "Specific heat", "Joule/K")
237 : : call ab_define_var(self%ncid, (/self%ntime/), self%chi_id, &
238 [ + + ]: 4 : &NF90_DOUBLE, "chi", "magnetic susceptibility", "1")
239 : : endif
240 : :
241 : : if(ob%calc_traj_obs)then
242 : : !TODO define traj obs here
243 : : endif
244 : : if(ob%calc_correlation_obs)then
245 : : !TODO define correlation obs here
246 : : endif
247 : :
248 : 2 : ncerr=nf90_enddef(self%ncid)
249 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when finishing defining observable variables in spin history file.")
250 : 2 : end subroutine def_observable_var
251 : :
252 : : !-----------------------------------------------------------------------
253 : : !> @brief write to netcdf after one step is done in a mover
254 : : !> @param [in] hist: the spin hist object
255 : : !> @param [in] ob : the spin observables
256 : : !-----------------------------------------------------------------------
257 : 202 : subroutine write_one_step(self, hist, ob)
258 : :
259 : : class(spin_ncfile_t), intent(inout) :: self
260 : : type(spin_hist_t), intent(in) :: hist
261 : : type(spin_observable_t), optional, intent(in) :: ob
262 : : integer :: ncerr, itime
263 : 202 : itime=self%itime+1
264 : :
265 : : !write(std_out, *) "writing spin dynamics step into spin hist netcdf file: itime: ", itime
266 [ + - ]: 202 : if(self%write_traj ==1) then
267 : : ncerr=nf90_put_var(self%ncid, self%S_id, hist%S(:,:,hist%ihist_prev), &
268 [ + + + + ]: 1414 : & start=[1, 1, itime], count=[3, hist%nspin, 1])
269 [ - + ]: 202 : NCF_CHECK_MSG(ncerr, "Error when writting Spin orientations in spin history file.")
270 : : ncerr=nf90_put_var(self%ncid, self%dsdt_id, &
271 : : & hist%dsdt(:,:,hist%ihist_prev), start=[1, 1, itime], &
272 [ + + + + ]: 1414 : & count=[3, hist%nspin, 1])
273 [ - + ]: 202 : NCF_CHECK_MSG(ncerr, "Error when writting dSdt in spin history file.")
274 : : ncerr=nf90_put_var(self%ncid, self%heff_id, &
275 [ + + + + ]: 1414 : & hist%heff(:,:,hist%ihist_prev), start=[1, 1, itime], count=[3, hist%nspin, 1])
276 [ - + ]: 202 : NCF_CHECK_MSG(ncerr, "Error when writting Heff in spin history file.")
277 : : ncerr=nf90_put_var(self%ncid, self%snorm_id, &
278 [ + - + + : 44642 : & hist%snorm(:,hist%ihist_prev)/mu_B, start=[1, itime], count=[hist%nspin, 1])
+ + + + ]
279 [ - + ]: 202 : NCF_CHECK_MSG(ncerr, "Error when writting Snorm in spin history file.")
280 : : end if
281 : : !ncerr=nf90_put_var(self%ncid, self%ihist_g_id, [hist%ihist_latt(hist%ihist_prev)], start=[itime], count=[1])
282 : : ncerr=nf90_put_var(self%ncid, self%itime_id, &
283 [ + + + + ]: 606 : & [hist%itime(hist%ihist_prev)], start=[itime], count=[1])
284 [ - + ]: 202 : NCF_CHECK_MSG(ncerr, "Error when writting itime in spin history file.")
285 : : ncerr=nf90_put_var(self%ncid, self%time_id, &
286 [ + + + + ]: 606 : & [hist%time(hist%ihist_prev)], start=[itime], count=[1])
287 [ - + ]: 202 : NCF_CHECK_MSG(ncerr, "Error when writting time in spin history file.")
288 : : ncerr=nf90_put_var(self%ncid, self%etotal_id, &
289 [ + + + + ]: 606 : & [hist%etot(hist%ihist_prev)], start=[itime], count=[1])
290 [ - + ]: 202 : NCF_CHECK_MSG(ncerr, "Error when writting etotal in spin history file.")
291 : 202 : self%itime=itime
292 : :
293 [ - + ]: 202 : if(present(ob)) then
294 : : ncerr=nf90_put_var(self%ncid, self%Mst_sub_id, ob%Mst_sub, &
295 [ # # # # ]: 0 : & start=[1, 1, itime], count=[3, ob%nsublatt, 1])
296 [ # # ]: 0 : NCF_CHECK_MSG(ncerr, "Error when writting Mst_sub in spin history file.")
297 : : ncerr=nf90_put_var(self%ncid, self%Mst_sub_norm_id, ob%Mst_sub_norm, &
298 [ # # # # ]: 0 : & start=[ 1, itime], count=[ob%nsublatt, 1])
299 : :
300 [ # # ]: 0 : NCF_CHECK_MSG(ncerr, "Error when writting Mst_sub_norm in spin history file.")
301 : : ncerr=nf90_put_var(self%ncid, self%Mst_norm_total_id, [ob%Mst_norm_total], &
302 [ # # # # ]: 0 : & start=[itime], count=[1])
303 [ # # ]: 0 : NCF_CHECK_MSG(ncerr, "Error when writting Mst_norm_total in spin history file.")
304 : : ncerr=nf90_put_var(self%ncid, self%Snorm_total_id, [ob%Snorm_total/mu_B], &
305 [ # # # # ]: 0 : & start=[itime], count=[1])
306 [ # # ]: 0 : NCF_CHECK_MSG(ncerr, "Error when writting Snorm_total in spin history file.")
307 : : if(ob%calc_traj_obs)then
308 : : endif
309 : : if(ob%calc_thermo_obs)then
310 : : endif
311 : : if(ob%calc_correlation_obs)then
312 : : endif
313 : : end if
314 : :
315 : 202 : end subroutine write_one_step
316 : :
317 : : !-----------------------------------------------------------------------
318 : : !> @brief write information about the primitive cell
319 : : !> Currently disabled.
320 : : !> @param [in] prim: the primitive cell
321 : : !-----------------------------------------------------------------------
322 : 2 : subroutine write_primitive_cell(self, prim)
323 : :
324 : : class(spin_ncfile_t), intent(inout) :: self
325 : : type(mbcell_t) :: prim
326 : :
327 : : !integer :: natom
328 : : integer :: nspin
329 : : integer :: ms_id, rprimd_id, spin_xcart_id, gyro_ratio_id, &
330 : : & gilbert_damping_id, ref_spin_orientation_id, &
331 : : & ref_spin_qpoint_id, ref_spin_rotate_axis_id
332 : : integer :: ncerr
333 : :
334 : :
335 : 2 : ncerr=nf90_redef(self%ncid)
336 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when starting defining primitive cell variables in spin history file.")
337 : :
338 : : ! lattice is not yet forced to be saved in prim
339 : : !ncerr=nf90_def_dim(self%ncid, "prim_natoms", prim%lattice%natom, natom )
340 : : !NCF_CHECK_MSG(ncerr, "Error when defining dimension prim_natoms in spin history file.")
341 : 2 : ncerr=nf90_def_dim(self%ncid, "prim_nspins", prim%spin%nspin, nspin)
342 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when defining dimension prim_nspins in spin history file.")
343 : :
344 : : call ab_define_var(self%ncid, [self%three, self%three], rprimd_id, &
345 [ + + ]: 6 : & NF90_DOUBLE, "prim_rprimd","PRIMitive cell Real space PRIMitive translations, Dimensional", "bohr")
346 : : call ab_define_var(self%ncid, [nspin], ms_id, &
347 [ + + ]: 4 : & NF90_DOUBLE, "prim_ms","PRIMitive cell Magnetic moment Scalar", "muB")
348 : : call ab_define_var(self%ncid, [self%three, nspin], spin_xcart_id, &
349 [ + + ]: 6 : & NF90_DOUBLE, "prim_spin_xcart","PRIMitive cell X Cartesian coordinates", "bohr")
350 : : call ab_define_var(self%ncid, [self%three, nspin], ref_spin_orientation_id, &
351 [ + + ]: 6 : & NF90_DOUBLE, "prim_ref_spin_orientation","PRIMitive cell REFerence SPIN ORIENTATION", "unitless")
352 : : call ab_define_var(self%ncid, [self%three], ref_spin_qpoint_id, &
353 [ + + ]: 4 : & NF90_DOUBLE, "prim_ref_spin_qpoint","PRIMitive cell REFerence SPIN QPOINT", "unitless")
354 : : call ab_define_var(self%ncid, [self%three], ref_spin_rotate_axis_id, &
355 [ + + ]: 4 : & NF90_DOUBLE, "prim_ref_spin_rotate_axis","PRIMitive cell REFerence SPIN ROTATE AXIS", "unitless")
356 : :
357 : : call ab_define_var(self%ncid, [nspin], gilbert_damping_id, &
358 [ + + ]: 4 : & NF90_DOUBLE, "prim_gilbert_damping","PRIMitive cell GILBERT DAMPING", "a.u.")
359 : :
360 : : call ab_define_var(self%ncid, [nspin], gyro_ratio_id, &
361 [ + + ]: 4 : & NF90_DOUBLE, "prim_gyro_ratio","PRIMitive cell GYROmagnetic RATIO", "a.u.")
362 : :
363 : 2 : ncerr=nf90_enddef(self%ncid)
364 : :
365 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when ending defining primitive cell variables in spin history file.")
366 : 2 : ncerr = nf90_put_var(self%ncid, rprimd_id, prim%spin%rprimd)
367 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting rprimd in spin history file.")
368 : 2 : ncerr = nf90_put_var(self%ncid, ms_id, prim%spin%ms)
369 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting ms in spin history file.")
370 : 2 : ncerr = nf90_put_var(self%ncid, spin_xcart_id, prim%spin%spin_positions)
371 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting xcart in spin history file.")
372 : 2 : ncerr = nf90_put_var(self%ncid, ref_spin_orientation_id, prim%spin%Sref)
373 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting ref_spin_orientation in spin history file.")
374 : 2 : ncerr = nf90_put_var(self%ncid, ref_spin_qpoint_id, prim%spin%ref_qpoint)
375 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting ref_spin_qpoint in spin history file.")
376 : 2 : ncerr = nf90_put_var(self%ncid, ref_spin_rotate_axis_id, prim%spin%ref_rotate_axis)
377 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting ref_spin_rotate_axis in spin history file.")
378 : 2 : ncerr = nf90_put_var(self%ncid, gilbert_damping_id, prim%spin%gilbert_damping)
379 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting gilbert_damping in spin history file.")
380 : 2 : ncerr = nf90_put_var(self%ncid, gyro_ratio_id, prim%spin%gyro_ratio)
381 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting gyro_ratio in spin history file.")
382 : :
383 : 2 : end subroutine write_primitive_cell
384 : :
385 : : !-----------------------------------------------------------------------
386 : : !> @brief write information of supercell
387 : : !> - cartesian coordinates of each spin in supercell
388 : : !> - R vector in supercell (as R in S_j e^iqR_j)
389 : : !> - the index of spin in primitive cell (as j in S_j e&iqR_j).
390 : : !> @param [in] supercell: The supercell object
391 : : !-----------------------------------------------------------------------
392 : 2 : subroutine write_supercell(self, supercell)
393 : :
394 : : class(spin_ncfile_t), intent(inout) :: self
395 : : type(mbsupercell_t), intent(in) :: supercell
396 : : integer :: pos_id, ispin_prim_id, rvec_id, ncerr
397 : : !integer :: rprimd_id, iatomsid
398 : : ! sc_matric
399 : :
400 : 2 : ncerr=nf90_redef(self%ncid)
401 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when starting to redefine supercell variables in spin history file.")
402 : : !call ab_define_var(self%ncid, (/self%three, self%three /), rprimd_id,&
403 : : ! & NF90_DOUBLE, "rprimd", "primitive cell vectors in real space with&
404 : : ! & units", "bohr")
405 : : call ab_define_var(self%ncid, (/self%three, self%nspin/), pos_id, &
406 [ + + ]: 6 : & NF90_DOUBLE, "xcart_spin","position of spin in cartesian coordinates", "bohr")
407 : : call ab_define_var(self%ncid, (/self%three, self%nspin/), rvec_id, &
408 [ + + ]: 6 : & NF90_INT, "Rvec", "R vector for spin in supercell", "dimensionless")
409 : : call ab_define_var(self%ncid, (/self%nspin/), ispin_prim_id,&
410 [ + + ]: 4 : & NF90_INT, "ispin_prim", "index of spin in primitive cell", "dimensionless")
411 : :
412 : 2 : ncerr=nf90_enddef(self%ncid)
413 [ - + ]: 2 : NCF_CHECK(ncerr)
414 : :
415 : : !ncerr=nf90_put_var(self%ncid, rprimd_id, scell%cell)
416 : 2 : ncerr=nf90_put_var(self%ncid, pos_id, supercell%spin%spin_positions)
417 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting xcart_spin in spin history file.")
418 : 2 : ncerr=nf90_put_var(self%ncid, ispin_prim_id, supercell%spin%ispin_prim)
419 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting ispin_prim in spin history file.")
420 : 2 : ncerr=nf90_put_var(self%ncid, rvec_id, supercell%spin%rvec)
421 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting Rvec in spin history file.")
422 : : ! ncerr=nf90_put_var(self%ncid, iatoms_id, scell%iatoms)
423 : 2 : end subroutine write_supercell
424 : :
425 : : !-----------------------------------------------------------------------
426 : : !> @brief write parameters into hist file
427 : : !> @param [in] params: parameters from input
428 : : !-----------------------------------------------------------------------
429 : 2 : subroutine write_parameters(self, params)
430 : : class(spin_ncfile_t), intent(inout) :: self
431 : : type(multibinit_dtset_type) :: params
432 : :
433 : : integer :: qpoint_id, temperature_id, dt_id, mfield_id, ncell_id
434 : : integer :: dim0(0)
435 : : integer :: ncerr
436 : 2 : ncerr=nf90_redef(self%ncid)
437 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when starting to redefining parameters in spin history file.")
438 : : ! dims
439 : : ! vars
440 : : call ab_define_var(self%ncid, (/self%three/), qpoint_id, NF90_DOUBLE,&
441 [ + + ]: 4 : & "spin_projection_qpoint", "spin QPOINT", "dimensionless")
442 : : ! TODO should change ncell to 3*3 matrix
443 : : call ab_define_var(self%ncid, (/self%three/), ncell_id, NF90_INT, "ncell",&
444 [ + + ]: 4 : & "supercell matrix (only diagonal)", "dimensionless")
445 : : call ab_define_var(self%ncid, dim0, temperature_id, NF90_DOUBLE,&
446 : 2 : & "spin_temperature", "Spin temperature", "Kelvin")
447 : : call ab_define_var(self%ncid, dim0, dt_id, NF90_DOUBLE, "spin_dt", "Spin&
448 : 2 : & time step", "second")
449 : : call ab_define_var(self%ncid, (/self%three/), mfield_id, NF90_DOUBLE,&
450 [ + + ]: 4 : & "spin_mag_field", "magnetic field for spin dynamics", "Tesla")
451 : : !ncerr=nf90_def_var(self%ncid, "ncell", NF90_INT, [self%three], ncell_id)
452 : : !ncerr=nf90_def_var(self%ncid, "spin_temperature", NF90_DOUBLE,
453 : : !temperature_id)
454 : : !ncerr=nf90_def_var(self%ncid, "spin_dt", NF90_DOUBLE, dt_id)
455 : : !ncerr=nf90_def_var(self%ncid, "spin_mag_field", NF90_DOUBLE, [self%three],
456 : : !mfield_id)
457 : :
458 : 2 : ncerr=nf90_enddef(self%ncid)
459 [ - + ]: 2 : NCF_CHECK(ncerr)
460 : : ! put vars
461 : 2 : ncerr=nf90_put_var(self%ncid, qpoint_id, params%spin_projection_qpoint)
462 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting spin_projection_qpoint in spin history file.")
463 : 2 : ncerr=nf90_put_var(self%ncid, ncell_id, params%ncell)
464 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting ncell in spin history file.")
465 : 2 : ncerr=nf90_put_var(self%ncid, temperature_id, params%spin_temperature*Ha_K)
466 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting spin_temperature in spin history file.")
467 : 2 : ncerr=nf90_put_var(self%ncid, dt_id, params%spin_dt*Time_Sec)
468 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting spin_dt in spin history file.")
469 [ + + ]: 8 : ncerr=nf90_put_var(self%ncid, mfield_id, params%spin_mag_field/Bfield_Tesla)
470 [ - + ]: 2 : NCF_CHECK_MSG(ncerr, "Error when writting spin_mag_field in spin history file.")
471 : 2 : end subroutine write_parameters
472 : :
473 : : !-----------------------------------------------------------------------
474 : : !> @brief close hist file
475 : : !-----------------------------------------------------------------------
476 : 2 : subroutine close(self)
477 : :
478 : : class(spin_ncfile_t), intent(inout) :: self
479 : : integer :: ncerr
480 [ + - ]: 2 : if (self%isopen) then
481 : 2 : write(std_out, *) "Closing spin history file "//trim(self%filename)//"."
482 : 2 : ncerr=nf90_close(self%ncid)
483 [ - + - - ]: 2 : NCF_CHECK_MSG(ncerr, "close netcdf spin history file"//trim(self%filename)//".")
484 : : end if
485 : 2 : end subroutine close
486 : :
487 : 0 : end module m_spin_ncfile
|