LCOV - code coverage report
Current view: top level - src/78_effpot - m_lwf_ncfile.F90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 99.0 % 97 96
Test Date: 2026-09-21 13:49:52 Functions: 71.4 % 7 5

            Line data    Source code
       1              : !!****m* ABINIT/m_lwf_ncfile
       2              : !! NAME
       3              : !! m_lwf_ncfile
       4              : !!
       5              : !! FUNCTION
       6              : !! This module contains the subroutines for output netcdf file
       7              : !!
       8              : !!
       9              : !! Datatypes:
      10              : !! lwf_ncfile_t: store data to calculate lwf_ncfile
      11              : !!
      12              : !!
      13              : !! COPYRIGHT
      14              : !! Copyright (C) 2001-2026 ABINIT group (hexu)
      15              : !! This file is distributed under the terms of the
      16              : !! GNU General Public License, see ~abinit/COPYING
      17              : !! or http://www.gnu.org/copyleft/gpl.txt .
      18              : !! For the initials of contributors, see ~abinit/doc/developers/contributors.txt .
      19              : !!
      20              : !! SOURCE
      21              : 
      22              : 
      23              : #if defined HAVE_CONFIG_H
      24              : #include "config.h"
      25              : #endif
      26              : 
      27              : #include "abi_common.h"
      28              : 
      29              : module m_lwf_ncfile
      30              : 
      31              :   use defs_basis
      32              :   use m_abicore
      33              :   use m_errors
      34              :   use m_xmpi
      35              :   use m_nctk
      36              :   use m_lwf_hist , only: lwf_hist_t
      37              :   use m_lwf_primitive_potential, only: lwf_primitive_potential_t
      38              :   use m_lwf_potential , only: lwf_potential_t
      39              :   use m_multibinit_dataset, only: multibinit_dtset_type
      40              :   use m_multibinit_cell, only: mbcell_t, mbsupercell_t
      41              :   use m_lwf_observables, only : lwf_observables_t
      42              : #if defined HAVE_NETCDF
      43              :   use netcdf
      44              : #endif
      45              :   implicit none
      46              : 
      47              :   private
      48              :   !!***
      49              :   type, public :: lwf_ncfile_t
      50              :      logical :: isopen=.False.  ! if the file is open
      51              :      ! dimensions
      52              :      integer :: three, nlwf
      53              :      ! three: 3
      54              : 
      55              :      ! file id
      56              :      integer :: ncerr, ncid
      57              :      ! variable id
      58              :      integer :: lwf_id, time_id, itime_id, etotal_id, entropy_id, displacement_id
      59              :      integer :: ilwf_prim_id, rvec_id, lwf_masses_id, natom_id
      60              :      integer :: itime, ntime,ncell
      61              :      ! itime: time index
      62              :      integer :: write_traj=1
      63              :      !whether to write the trajectory
      64              :      character(len=fnlen) :: filename
      65              : 
      66              :      ! netcdf filename
      67              :      logical :: has_constrain=.False.
      68              :      integer :: n_fixed_lwf
      69              :      integer, allocatable :: fixed_lwf_ids(:)
      70              :      real(dp), allocatable :: fixed_lwf_values(:)
      71              :    contains
      72              :      ! initialize
      73              :      procedure :: initialize
      74              :      procedure :: finalize
      75              :      procedure :: def_lwf_var
      76              :      procedure :: write_cell
      77              :      procedure :: write_one_step
      78              :   end type lwf_ncfile_t
      79              : 
      80              : contains
      81            7 :   subroutine initialize(self, filename, write_traj)
      82              :     class(lwf_ncfile_t) :: self
      83              :     character(len=*),intent(in) :: filename
      84              :     integer, intent(in) :: write_traj
      85              :     integer :: ncerr
      86            7 :     self%itime=0
      87            7 :     self%write_traj=write_traj
      88            7 :     self%filename=trim(filename)
      89              : #if defined HAVE_NETCDF
      90            7 :     write(std_out,*) "Write iteration in lwf history file "//trim(self%filename)//"."
      91              :     !  Create netCDF file
      92            7 :     ncerr = nf90_create(path=trim(self%filename), cmode=NF90_NETCDF4, ncid=self%ncid)
      93            7 :     NCF_CHECK_MSG(ncerr, "Error when creating netcdf history file")
      94            7 :     self%isopen=.True.
      95            7 :     ncerr =nf90_enddef(self%ncid)
      96            7 :     NCF_CHECK_MSG(ncerr, "Error when ending def mode in lwf netcdf history file")
      97              : #endif
      98            7 :   end subroutine initialize
      99              : 
     100            7 :   subroutine finalize(self)
     101              :     class(lwf_ncfile_t), intent(inout) :: self
     102              : #if defined HAVE_NETCDF
     103              :     integer :: ncerr
     104            7 :     if (self%isopen) then
     105            7 :        write(std_out, *) "Closing lwf history file "//trim(self%filename)//"."
     106            7 :        ncerr=nf90_close(self%ncid)
     107            7 :        NCF_CHECK_MSG(ncerr, "close netcdf lwf history file"//trim(self%filename)//".")
     108              :     end if
     109              : #endif
     110            7 :   end subroutine finalize
     111              : 
     112            7 :   subroutine write_cell(self, supercell)
     113              :     class(lwf_ncfile_t), intent(inout) :: self
     114              :     type(mbsupercell_t), intent(in) :: supercell
     115              :     integer :: ncerr, natom3, nnz, id_nnz,  id_natom3, id_map_ilwf, id_map_idisp, id_map_val
     116              :     integer :: latt_rvec_id,  ilatt_prim_id, ref_cell_id, ref_xcart_id, zion_id, masses_id
     117              : #if defined HAVE_NETCDF
     118            7 :     ncerr = nf90_redef(self%ncid)
     119            7 :     NCF_CHECK_MSG(ncerr, "Error when starting defining trajectory variables in lwf history file.")
     120              :     ! define dimensions
     121            7 :     ncerr=nf90_def_dim(self%ncid, "three", 3, self%three)
     122            7 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension three in history file.")
     123              : 
     124            7 :     ncerr=nf90_def_dim(self%ncid, "nlwf", supercell%lwf%nlwf, self%nlwf)
     125            7 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension nlwf in history file.")
     126              : 
     127              : 
     128            7 :     ncerr=nf90_def_dim(self%ncid, "natom", supercell%lattice%natom, self%natom_id)
     129            7 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension natom in history file.")
     130              : 
     131              : 
     132            7 :     natom3=supercell%lwf%lwf_latt_coeffs%coeffs%mshape(1)
     133            7 :     ncerr=nf90_def_dim(self%ncid, "natom3", natom3, id_natom3)
     134            7 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension natom3 in history file.")
     135              : 
     136            7 :     nnz=supercell%lwf%lwf_latt_coeffs%coeffs%nnz
     137            7 :     ncerr=nf90_def_dim(self%ncid, "lwf_latt_map_nnz",nnz, id_nnz)
     138            7 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension nlwf in history file.")
     139              : 
     140              :     !ncerr=nf90_def_dim(self%ncid, "nR", 3, self%nlwf)
     141              :     !NCF_CHECK_MSG(ncerr, "Error when defining dimension three in history file.")
     142              : 
     143              :     call ab_define_var(self%ncid, [self%three, self%nlwf], &
     144           21 :          &         self%rvec_id, NF90_INT, "lwf_rvec", "R-vectors for LWF ", "dimensionless")
     145              : 
     146              :     call ab_define_var(self%ncid, [self%nlwf], &
     147              :          &         self%ilwf_prim_id, NF90_INT, "ilwf_prim", &
     148           14 :          & "index of lwf in primitive cell", "dimensionless")
     149              : 
     150              :     call ab_define_var(self%ncid, [self%nlwf], &
     151           14 :          &         self%lwf_masses_id, NF90_DOUBLE, "lwf_masses", "LWF MASSES", "dimensionless")
     152              : 
     153              : 
     154              :     ! Lattice
     155              :     call ab_define_var(self%ncid, [self%three, self%natom_id], &
     156           21 :          &         latt_rvec_id, NF90_INT, "lattice_rvec", "R-vectors for LATTICE ", "dimensionless")
     157              : 
     158              :     call ab_define_var(self%ncid, [self%natom_id], &
     159              :          &         ilatt_prim_id, NF90_INT, "ilatt_prim", &
     160           14 :          & "index of lattice in primitive cell", "dimensionless")
     161              : 
     162              : 
     163              :     call ab_define_var(self%ncid, [self%three,self%three], &
     164              :          &         ref_cell_id, NF90_DOUBLE, "ref_cell", &
     165           21 :          & "REFerence CELL", "bohr")
     166              : 
     167              :     call ab_define_var(self%ncid, [self%three,self%natom_id], &
     168              :          &         ref_xcart_id, NF90_DOUBLE, "ref_xcart", &
     169           21 :          & "REFerence XCART", "bohr")
     170              : 
     171              :     call ab_define_var(self%ncid, [self%natom_id], &
     172              :          &         zion_id, NF90_INT, "zion", &
     173           14 :          & "ZION", "dimensionless")
     174              : 
     175              :     call ab_define_var(self%ncid, [self%natom_id], &
     176              :          &         masses_id, NF90_DOUBLE, "masses", &
     177           14 :          & "MASSES", "dimensionless")
     178              : 
     179              : 
     180              : 
     181              :     ! define vars for lwf lattice displacement mapping in the format of a COO matrix.
     182              :     call ab_define_var(self%ncid, [id_nnz], id_map_idisp, &
     183              :          & NF90_INT, "lwf_latt_map_id_displacement", &
     184           14 :          & "LWF lattice mapping coefficient COO matrix displacement id",  "dimensionless")
     185              : 
     186            7 :     ncerr=nf90_def_var_deflate(self%ncid, id_map_idisp, shuffle=1, deflate=1, deflate_level=2)
     187            7 :     NCF_CHECK_MSG(ncerr, "Error when defining deflating for variable id_map_idisp")
     188              : 
     189              : 
     190              : 
     191              :     call ab_define_var(self%ncid, [id_nnz], id_map_ilwf,&
     192              :          & NF90_INT, "lwf_latt_map_id_lwf", &
     193           14 :          & "LWF lattice mapping coefficient COO matrix LWF id","dimensionless")
     194              : 
     195            7 :     ncerr=nf90_def_var_deflate(self%ncid, id_map_ilwf, shuffle=1, deflate=1, deflate_level=2)
     196            7 :     NCF_CHECK_MSG(ncerr, "Error when defining delfating for variable id_map_ilwf")
     197              : 
     198              : 
     199              :     call ab_define_var(self%ncid, [id_nnz], id_map_val, &
     200              :          & NF90_DOUBLE, "lwf_latt_map_values", &
     201           14 :          & "LWF lattice mapping coefficient COO matrix values","dimensionless")
     202              : 
     203            7 :     ncerr=nf90_def_var_deflate(self%ncid, id_map_val, shuffle=1, deflate=1, deflate_level=2)
     204            7 :     NCF_CHECK_MSG(ncerr, "Error when defining delfating for variable id_map_val")
     205              : 
     206            7 :     ncerr=nf90_enddef(self%ncid)
     207              : 
     208              : 
     209              : 
     210              :     ncerr=nf90_put_var(self%ncid, zion_id, [supercell%lattice%zion], &
     211        43029 :          &      start=[1], count=[supercell%lattice%natom])
     212            7 :     NCF_CHECK_MSG(ncerr, "Error when writting zion in lattice history file.")
     213              : 
     214              :     ncerr=nf90_put_var(self%ncid, masses_id, [supercell%lattice%masses], &
     215        43029 :          &      start=[1], count=[supercell%lattice%natom])
     216            7 :     NCF_CHECK_MSG(ncerr, "Error when writting masses in lattice history file.")
     217              : 
     218              :     ncerr=nf90_put_var(self%ncid, ref_xcart_id, [supercell%lattice%xcart], &
     219       150556 :          &      start=[1,1], count=[3,supercell%lattice%natom])
     220            7 :     NCF_CHECK_MSG(ncerr, "Error when writting ref_xcart in lattice history file.")
     221              : 
     222              :     ncerr=nf90_put_var(self%ncid, ref_cell_id, [supercell%lattice%cell], &
     223          154 :          &      start=[1,1], count=[3,3])
     224            7 :     NCF_CHECK_MSG(ncerr, "Error when writting ref_cell in lattice history file.")
     225              : 
     226              : 
     227              : 
     228              :     ncerr=nf90_put_var(self%ncid, self%ilwf_prim_id, [supercell%lwf%ilwf_prim], &
     229        14357 :          &      start=[1], count=[supercell%lwf%nlwf])
     230            7 :     NCF_CHECK_MSG(ncerr, "Error when writting ilwf_prim in lwf history file.")
     231              : 
     232              :     ncerr=nf90_put_var(self%ncid, self%rvec_id, [supercell%lwf%rvec], &
     233        50204 :          &      start=[1,1], count=[3, supercell%lwf%nlwf])
     234            7 :     NCF_CHECK_MSG(ncerr, "Error when writting lwf rvec in lwf history file.")
     235              : 
     236              :     ncerr=nf90_put_var(self%ncid, self%lwf_masses_id, [supercell%lwf%lwf_masses], &
     237        14357 :          &      start=[1], count=[supercell%lwf%nlwf])
     238            7 :     NCF_CHECK_MSG(ncerr, "Error when writting lwf_masses in lwf history file.")
     239              : 
     240              :     ncerr=nf90_put_var(self%ncid, id_map_idisp,  &
     241              :          &         supercell%lwf%lwf_latt_coeffs%coeffs%ind%data(1, 1:nnz), &
     242           14 :          &      start=[1], count=[nnz])
     243            7 :     NCF_CHECK_MSG(ncerr, "Error when writting id_map_idisp in lwf history file.")
     244              : 
     245              : 
     246              :     ncerr=nf90_put_var(self%ncid, id_map_ilwf,  &
     247              :          &         supercell%lwf%lwf_latt_coeffs%coeffs%ind%data(2, 1:nnz), &
     248           14 :          &      start=[1], count=[nnz])
     249            7 :     NCF_CHECK_MSG(ncerr, "Error when writting id_map_ilwf in lwf history file.")
     250              : 
     251              : 
     252              :     ncerr=nf90_put_var(self%ncid, id_map_val,  &
     253              :          &         supercell%lwf%lwf_latt_coeffs%coeffs%val%data(1:nnz), &
     254           14 :          &      start=[1], count=[nnz])
     255            7 :     NCF_CHECK_MSG(ncerr, "Error when writting id_map_ilwf in lwf history file.")
     256              : 
     257              : 
     258              : #endif
     259              : 
     260            7 :   end subroutine write_cell
     261              : 
     262            7 :   subroutine def_lwf_var(self, hist)
     263              :     class(lwf_ncfile_t), intent(inout) :: self
     264              :     type(lwf_hist_t),intent(in) :: hist
     265              :     integer :: ncerr
     266            7 :     ABI_UNUSED_A(hist)
     267              : 
     268              : #if defined HAVE_NETCDF
     269            7 :     ncerr = nf90_redef(self%ncid)
     270            7 :     NCF_CHECK_MSG(ncerr, "Error when starting defining trajectory variables in lwf history file.")
     271              :     ! define dimensions
     272            7 :     ncerr=nf90_def_dim(self%ncid, "ntime", nf90_unlimited, self%ntime)
     273            7 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension ntime in lwf history file.")
     274              :     !call ab_define_var(ncid,dim1,typat_id,NF90_DOUBLE,&
     275              :     !     &  "typat","types of atoms","dimensionless" )
     276              : 
     277              :     !if(self%write_traj==1) then
     278              :        call ab_define_var(self%ncid, (/ self%nlwf, self%ntime /), &
     279           21 :             &         self%lwf_id, NF90_DOUBLE, "lwf", "lwf amplitude", "dimensionless")
     280              : 
     281            7 :         ncerr=nf90_def_var_deflate(self%ncid, self%lwf_id, shuffle=1, deflate=1, deflate_level=2)
     282            7 :         NCF_CHECK_MSG(ncerr, "Error when defining deflating for variable lwf")
     283              : 
     284              :     !endif
     285              : 
     286              :     !call ab_define_var(self%ncid, (/ self%ntime /), &
     287              :     !     &         self%time_id, NF90_DOUBLE, "time", "time", "s")
     288              :     call ab_define_var(self%ncid, (/ self%ntime /), &
     289           14 :          &         self%etotal_id, NF90_DOUBLE, "etotal", "TOTAL energy", "Ha")
     290              :     !call ab_define_var(self%ncid, (/ self%ntime /), &
     291              :     !     &         self%entropy_id, NF90_DOUBLE, "entropy", "Entropy", "Joule/K")
     292              : 
     293              :     call ab_define_var(self%ncid, (/ self%ntime /), &
     294           14 :          &         self%itime_id, NF90_INT, "itime", "index of time in timeline", "1")
     295              : 
     296            7 :     ncerr=nf90_enddef(self%ncid)
     297            7 :     NCF_CHECK_MSG(ncerr, "Error when finishing defining variables in lwf history file.")
     298              : #endif
     299              : 
     300            7 :   end subroutine def_lwf_var
     301              : 
     302              : 
     303              : 
     304          217 :   subroutine write_one_step(self, hist)
     305              :     class(lwf_ncfile_t), intent(inout) :: self
     306              :     type(lwf_hist_t), intent(in) :: hist
     307              :     integer :: ncerr, itime
     308          217 :     self%itime=self%itime+1
     309          217 :     itime = self%itime
     310              : 
     311              : #if defined HAVE_NETCDF
     312              :     !if(self%write_traj ==1) then
     313              :     !print *, "Write one step of netcdf."
     314              :     ncerr=nf90_put_var(self%ncid, self%lwf_id, hist%current_lwf, &
     315         1085 :             &      start=[1, itime], count=[hist%nlwf, 1])
     316          217 :     NCF_CHECK_MSG(ncerr, "Error when writting lwf amplitudes in lwf history file.")
     317              :     !end if
     318              : 
     319              :     ncerr=nf90_put_var(self%ncid, self%etotal_id, [hist%current_energy], &
     320          651 :          &      start=[itime], count=[1])
     321          217 :     NCF_CHECK_MSG(ncerr, "Error when writting energy in lwf history file.")
     322              : 
     323              :     ncerr=nf90_put_var(self%ncid, self%itime_id, [self%itime], &
     324          651 :          &      start=[itime], count=[1])
     325          217 :     NCF_CHECK_MSG(ncerr, "Error when writting energy in lwf history file.")
     326              : 
     327              :     !ncerr=nf90_put_var(self%ncid, self%displacement_id, [displacement], &
     328              :     !     &      start=[1,itime], count=[3, hist%natom, 1])
     329              :     !NCF_CHECK_MSG(ncerr, "Error when writting energy in lwf history file.")
     330              : 
     331              : #endif
     332              : 
     333          217 :   end subroutine write_one_step
     334              : 
     335              : 
     336            0 : end module m_lwf_ncfile
        

Generated by: LCOV version 2.3-1