LCOV - code coverage report
Current view: top level - src/78_effpot - m_lattice_ncfile.F90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 98.6 % 69 68
Test Date: 2026-11-08 04:19:34 Functions: 83.3 % 6 5
Branches: 77.0 % 126 97

             Branch data     Line data    Source code
       1                 :             : !!****m* ABINIT/m_lattice_ncfile
       2                 :             : !! NAME
       3                 :             : !! m_lattice_ncfile
       4                 :             : !!
       5                 :             : !! FUNCTION
       6                 :             : !! This module contains the subroutines for output netcdf file
       7                 :             : !!
       8                 :             : !! Datatypes:
       9                 :             : !! lattice_ncfile_t: store data to calculate lattice_ncfile
      10                 :             : !!
      11                 :             : !! COPYRIGHT
      12                 :             : !! Copyright (C) 2001-2026 ABINIT group (hexu)
      13                 :             : !! This file is distributed under the terms of the
      14                 :             : !! GNU General Public License, see ~abinit/COPYING
      15                 :             : !! or http://www.gnu.org/copyleft/gpl.txt .
      16                 :             : !! For the initials of contributors, see ~abinit/doc/developers/contributors.txt .
      17                 :             : !!
      18                 :             : !! SOURCE
      19                 :             : 
      20                 :             : 
      21                 :             : #if defined HAVE_CONFIG_H
      22                 :             : #include "config.h"
      23                 :             : #endif
      24                 :             : 
      25                 :             : #include "abi_common.h"
      26                 :             : 
      27                 :             : 
      28                 :             : module m_lattice_ncfile
      29                 :             : 
      30                 :             :   use defs_basis
      31                 :             :   use m_abicore
      32                 :             :   use m_errors
      33                 :             :   use m_xmpi
      34                 :             :   use m_nctk
      35                 :             :   use m_lattice_harmonic_primitive_potential, only: lattice_harmonic_primitive_potential_t
      36                 :             :   use m_lattice_harmonic_potential , only: lattice_harmonic_potential_t
      37                 :             :   use m_multibinit_dataset, only: multibinit_dtset_type
      38                 :             :   use m_multibinit_cell, only: mbcell_t, mbsupercell_t
      39                 :             :   use netcdf
      40                 :             : 
      41                 :             :   implicit none
      42                 :             : 
      43                 :             :   private
      44                 :             :   !!***
      45                 :             :   type, public :: lattice_ncfile_t
      46                 :             :      logical :: isopen=.False.  ! if the file is open
      47                 :             :      ! dimensions
      48                 :             :      integer :: three, natom
      49                 :             :      ! three: 3
      50                 :             : 
      51                 :             :      ! file id
      52                 :             :      integer :: ncerr, ncid
      53                 :             :      ! variable id
      54                 :             :      integer :: vcart_id, xcart_id, cell_id, time_id, itime_id, etotal_id, ekin_id, entropy_id
      55                 :             :      integer :: ilatt_prim_id, rvec_id, zion_id, masses_id, ref_xcart_id, ref_cell_id
      56                 :             :      integer :: itime, ntime,ncell
      57                 :             :      ! itime: time index
      58                 :             :      integer :: write_traj=1
      59                 :             :      !whether to write the trajectory
      60                 :             :      character(len=fnlen) :: filename
      61                 :             :      ! netcdf filename
      62                 :             :    contains
      63                 :             :      ! initialize
      64                 :             :      procedure :: initialize
      65                 :             :      procedure :: finalize
      66                 :             :      procedure :: def_lattice_var
      67                 :             :      procedure :: write_cell
      68                 :             :      procedure :: write_one_step
      69                 :             :   end type lattice_ncfile_t
      70                 :             : 
      71                 :             : contains
      72                 :           3 :   subroutine initialize(self, filename, write_traj)
      73                 :             :     class(lattice_ncfile_t) :: self
      74                 :             :     character(len=*),intent(in) :: filename
      75                 :             :     integer, intent(in) :: write_traj
      76                 :             :     integer :: ncerr
      77                 :           3 :     self%itime=0
      78                 :           3 :     self%write_traj=write_traj
      79                 :           3 :     self%filename=trim(filename)
      80                 :           3 :     write(std_out,*) "Write iteration in lattice history file "//trim(self%filename)//"."
      81                 :             :     !  Create netCDF file
      82                 :           3 :     ncerr = nf90_create(path=trim(filename), cmode=NF90_CLOBBER, ncid=self%ncid)
      83         [ -  + ]:           3 :     NCF_CHECK_MSG(ncerr, "Error when creating netcdf history file")
      84                 :           3 :     self%isopen=.True.
      85                 :           3 :     ncerr =nf90_enddef(self%ncid)
      86         [ -  + ]:           3 :     NCF_CHECK_MSG(ncerr, "Error when ending def mode in lattice netcdf history file")
      87         [ +  - ]:           3 :   end subroutine initialize
      88                 :             : 
      89                 :           3 :   subroutine finalize(self)
      90                 :             :     class(lattice_ncfile_t), intent(inout) :: self
      91                 :             :     integer :: ncerr
      92         [ +  - ]:           3 :     if (self%isopen) then
      93                 :           3 :        write(std_out, *) "Closing lattice history file "//trim(self%filename)//"."
      94                 :           3 :        ncerr=nf90_close(self%ncid)
      95   [ -  +  -  - ]:           3 :        NCF_CHECK_MSG(ncerr, "close netcdf lattice history file"//trim(self%filename)//".")
      96                 :             :     end if
      97                 :             : 
      98                 :           3 :   end subroutine finalize
      99                 :             : 
     100                 :           3 :   subroutine write_cell(self, supercell)
     101                 :             :     class(lattice_ncfile_t), intent(inout) :: self
     102                 :             :     type(mbsupercell_t), intent(in) :: supercell
     103                 :             :     integer :: ncerr
     104                 :             : 
     105                 :           3 :     ncerr = nf90_redef(self%ncid)
     106         [ -  + ]:           3 :     NCF_CHECK_MSG(ncerr, "Error when starting defining trajectory variables in lattice history file.")
     107                 :             :     ! define dimensions
     108                 :           3 :     ncerr=nf90_def_dim(self%ncid, "three", 3, self%three)
     109         [ -  + ]:           3 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension three in history file.")
     110                 :             : 
     111                 :           3 :     ncerr=nf90_def_dim(self%ncid, "natom", supercell%lattice%natom, self%natom)
     112         [ -  + ]:           3 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension natom in history file.")
     113                 :             : 
     114                 :             :     call ab_define_var(self%ncid, [self%three, self%natom], &
     115         [ +  + ]:           9 :          &         self%rvec_id, NF90_INT, "lattice_rvec", "R-vectors for LATTICE ", "dimensionless")
     116                 :             : 
     117                 :             :     call ab_define_var(self%ncid, [self%natom], &
     118                 :             :          &         self%ilatt_prim_id, NF90_INT, "ilatt_prim", &
     119         [ +  + ]:           6 :          & "index of lattice in primitive cell", "dimensionless")
     120                 :             : 
     121                 :             : 
     122                 :             :     call ab_define_var(self%ncid, [self%three,self%three], &
     123                 :             :          &         self%ref_cell_id, NF90_DOUBLE, "ref_cell", &
     124         [ +  + ]:           9 :          & "REFerence CELL", "bohr")
     125                 :             : 
     126                 :             : 
     127                 :             : 
     128                 :             :     call ab_define_var(self%ncid, [self%three,self%natom], &
     129                 :             :          &         self%ref_xcart_id, NF90_DOUBLE, "ref_xcart", &
     130         [ +  + ]:           9 :          & "REFerence XCART", "bohr")
     131                 :             : 
     132                 :             :     call ab_define_var(self%ncid, [self%natom], &
     133                 :             :          &         self%zion_id, NF90_INT, "zion", &
     134         [ +  + ]:           6 :          & "ZION", "dimensionless")
     135                 :             : 
     136                 :             :     call ab_define_var(self%ncid, [self%natom], &
     137                 :             :          &         self%masses_id, NF90_DOUBLE, "masses", &
     138         [ +  + ]:           6 :          & "MASSES", "dimensionless")
     139                 :             : 
     140                 :             : 
     141                 :           3 :     Ncerr=nf90_enddef(self%ncid)
     142                 :             : 
     143                 :             :     ! ncerr=nf90_put_var(self%ncid, self%ilatt_prim_id, [supercell%lattice%ilatt_prim], &
     144                 :             :     !      &      start=[1], count=[supercell%lattice%natom])
     145                 :             :     ! NCF_CHECK_MSG(ncerr, "Error when writting ilatt_prim in lattice history file.")
     146                 :             : 
     147                 :             :     ! ncerr=nf90_put_var(self%ncid, self%rvec_id, [supercell%lattice%rvec], &
     148                 :             :     !      &      start=[1,1], count=[3, supercell%lattice%natom])
     149                 :             :     ! NCF_CHECK_MSG(ncerr, "Error when writting ilatt_prim in lattice history file.")
     150                 :             : 
     151                 :             :      ncerr=nf90_put_var(self%ncid, self%zion_id, [supercell%lattice%zion], &
     152   [ +  -  +  +  :        4969 :           &      start=[1], count=[supercell%lattice%natom])
          +  -  +  +  +  
                      + ]
     153         [ -  + ]:           3 :      NCF_CHECK_MSG(ncerr, "Error when writting zion in lattice history file.")
     154                 :             : 
     155                 :             :      ncerr=nf90_put_var(self%ncid, self%masses_id, [supercell%lattice%masses], &
     156   [ +  -  +  +  :        4969 :           &      start=[1], count=[supercell%lattice%natom])
          +  -  +  +  +  
                      + ]
     157         [ -  + ]:           3 :      NCF_CHECK_MSG(ncerr, "Error when writting masses in lattice history file.")
     158                 :             : 
     159                 :             :      ncerr=nf90_put_var(self%ncid, self%ref_xcart_id, [supercell%lattice%xcart], &
     160   [ +  -  +  +  :       17372 :           &      start=[1,1], count=[3,supercell%lattice%natom])
          +  +  +  -  +  
                +  +  + ]
     161         [ -  + ]:           3 :      NCF_CHECK_MSG(ncerr, "Error when writting ref_xcart in lattice history file.")
     162                 :             : 
     163                 :             :      ncerr=nf90_put_var(self%ncid, self%ref_cell_id, [supercell%lattice%cell], &
     164   [ +  +  +  +  :          66 :           &      start=[1,1], count=[3,3])
                   +  + ]
     165         [ -  + ]:           3 :      NCF_CHECK_MSG(ncerr, "Error when writting ref_cell in lattice history file.")
     166                 :             : 
     167                 :           3 :   end subroutine write_cell
     168                 :             : 
     169                 :           3 :   subroutine def_lattice_var(self)
     170                 :             :     class(lattice_ncfile_t), intent(inout) :: self
     171                 :             :     !type(lattice_hist_t),intent(in) :: hist
     172                 :             :     integer :: ncerr
     173                 :             : 
     174                 :           3 :     ncerr = nf90_redef(self%ncid)
     175         [ -  + ]:           3 :     NCF_CHECK_MSG(ncerr, "Error when starting defining trajectory variables in lattice history file.")
     176                 :             :     ! define dimensions
     177                 :           3 :     ncerr=nf90_def_dim(self%ncid, "ntime", nf90_unlimited, self%ntime)
     178         [ -  + ]:           3 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension ntime in lattice history file.")
     179                 :             :     !call ab_define_var(ncid,dim1,typat_id,NF90_DOUBLE,&
     180                 :             :     !     &  "typat","types of atoms","dimensionless" )
     181                 :             : 
     182                 :             :     !if(self%write_traj==1) then
     183                 :             :        call ab_define_var(self%ncid, (/self%three, self%natom, self%ntime /), &
     184         [ +  + ]:          12 :             &         self%xcart_id, NF90_DOUBLE, "xcart", "lattice amplitude", "bohr")
     185                 :             :        call ab_define_var(self%ncid, (/self%three, self%natom, self%ntime /), &
     186         [ +  + ]:          12 :             &         self%vcart_id, NF90_DOUBLE, "vcart", "cartesian velocity", "bohr/a.u.")
     187                 :             :        call ab_define_var(self%ncid, (/self%three, self%three, self%ntime /), &
     188         [ +  + ]:          12 :             &         self%cell_id, NF90_DOUBLE, "cell", "cell parameters", "bohr")
     189                 :             : 
     190                 :             : 
     191                 :             :     !endif
     192                 :             : 
     193                 :             :     !call ab_define_var(self%ncid, (/ self%ntime /), &
     194                 :             :     !     &         self%time_id, NF90_DOUBLE, "time", "time", "s")
     195                 :             :     call ab_define_var(self%ncid, (/ self%ntime /), &
     196         [ +  + ]:           6 :          &         self%etotal_id, NF90_DOUBLE, "etotal", "TOTAL energy", "Ha")
     197                 :             :     call ab_define_var(self%ncid, (/ self%ntime /), &
     198         [ +  + ]:           6 :          &         self%ekin_id, NF90_DOUBLE, "ekin", "Kinetic energy", "Ha")
     199                 :             : 
     200                 :             :     !call ab_define_var(self%ncid, (/ self%ntime /), &
     201                 :             :     !     &         self%entropy_id, NF90_DOUBLE, "entropy", "Entropy", "Joule/K")
     202                 :             : 
     203                 :             :     call ab_define_var(self%ncid, (/ self%ntime /), &
     204         [ +  + ]:           6 :          &         self%itime_id, NF90_INT, "itime", "index of time in timeline", "1")
     205                 :             : 
     206                 :           3 :     ncerr=nf90_enddef(self%ncid)
     207         [ -  + ]:           3 :     NCF_CHECK_MSG(ncerr, "Error when finishing defining variables in lattice history file.")
     208                 :             : 
     209                 :           3 :   end subroutine def_lattice_var
     210                 :             : 
     211                 :             : 
     212                 :             : 
     213   [ -  +  -  + ]:         213 :   subroutine write_one_step(self, xcart, vcart, Etotal, Ek)
     214                 :             :     class(lattice_ncfile_t), intent(inout) :: self
     215                 :             :     !type(lattice_hist_t), intent(in) :: hist
     216                 :             :     real(dp) , intent(in) :: Etotal, Ek, xcart(:,:), vcart(:,:)
     217                 :             :     integer :: ncerr, itime, natom
     218                 :         213 :     natom=size(xcart, 2)
     219                 :         213 :     self%itime=self%itime+1
     220                 :         213 :     itime = self%itime
     221                 :             : 
     222                 :             :     !if(self%write_traj ==1) then
     223                 :             :     ncerr=nf90_put_var(self%ncid, self%xcart_id, xcart, &
     224   [ +  +  +  + ]:        1491 :             &      start=[1,1, itime], count=[3,natom, 1])
     225                 :             : 
     226                 :             :     ncerr=nf90_put_var(self%ncid, self%vcart_id, vcart, &
     227   [ +  +  +  + ]:        1491 :          &      start=[1,1, itime], count=[3,natom, 1])
     228                 :             : 
     229         [ -  + ]:         213 :     NCF_CHECK_MSG(ncerr, "Error when writting lattice amplitudes in lattice history file.")
     230                 :             :     !end if
     231                 :             : 
     232                 :             :     ncerr=nf90_put_var(self%ncid, self%etotal_id, [Etotal], &
     233   [ +  +  +  + ]:         639 :          &      start=[itime], count=[1])
     234         [ -  + ]:         213 :     NCF_CHECK_MSG(ncerr, "Error when writting total energy in lattice history file.")
     235                 :             : 
     236                 :             :     ncerr=nf90_put_var(self%ncid, self%ekin_id, [Ek], &
     237   [ +  +  +  + ]:         639 :          &      start=[itime], count=[1])
     238         [ -  + ]:         213 :     NCF_CHECK_MSG(ncerr, "Error when writting kinetic energy in lattice history file.")
     239                 :             : 
     240                 :             :     ncerr=nf90_put_var(self%ncid, self%itime_id, [self%itime], &
     241   [ +  +  +  + ]:         639 :          &      start=[itime], count=[1])
     242         [ -  + ]:         213 :     NCF_CHECK_MSG(ncerr, "Error when writting itime in lattice history file.")
     243                 :             : 
     244                 :         213 :   end subroutine write_one_step
     245                 :             : 
     246                 :             :   subroutine close(self)
     247                 :             :     class(lattice_ncfile_t) :: self
     248                 :             :     self%isopen=.False.
     249                 :             :   end subroutine close
     250                 :             : 
     251                 :             : 
     252                 :           0 : end module m_lattice_ncfile
        

Generated by: LCOV version 2.3-1