Dice Fortran Backend Documentation
restart_fns.f90
Go to the documentation of this file.
1 ! ---------------------------------------------------------------------------------------
2 ! Dice Quantum Monte Carlo
3 ! ---------------------------------------------------------------------------------------
4 ! MODULE: restart functions
5 !
6 ! DESCRIPTION:
9 !
13 ! ---------------------------------------------------------------------------------------
14 
16 
17  use shared_data
18  use netcdf
19 
20  implicit none
21 
22  ! Variables for Error Handling
23  integer :: fu
24  integer :: rc
25 
26 contains
27 
28 
29 ! ---------------------------------------------------------------------------------------
30 ! SECTION: Restart Writers
31 ! ---------------------------------------------------------------------------------------
32 
33 
34  ! -----------------------------------------------------------------------------------
35  ! ROUTINE: write_res_nml
36  !
37  ! DESCRIPTION:
40  !
41  ! PARAMETERS:
46  ! -----------------------------------------------------------------------------------
47  subroutine write_res_nml(i_qoi,i_par,auto,guess)
48 
49  ! Uses the following global variables: p_system, p
50  ! Local variables
51  integer(int32), intent(in) :: i_qoi ! current index on alpha (for QHO) or bond (for Hyd) grid
52  integer(int32), optional, intent(in) :: i_par ! current index of c (for H2plus) or beta (for H2)
53  logical, optional,intent(in) :: auto ! indicates if auto_params is turned on, for H2plus or H2
54  real(real64), optional, intent(in) :: guess ! for auto-search, current guess value for c (for H2plus) or beta (for H2)
55  type(restart_type) :: res
56 
57  ! Define the namelists
58  namelist /restart/ res
59 
60  ! Store grid point of quantity of interest (qoi)
61  res%i_qoi = i_qoi
62 
63  ! Check if the required optional parameters are present, and store them in res
64  if (p_system=='H2plus' .or. p_system=='H2') then
65 
66  if (.not.(present(i_par))) error stop 'Error: Missing "i_par" input to function write_res_nml().'
67  res%i_par = i_par
68 
69  if (.not.(present(auto))) error stop 'Error: Missing "auto" input to function write_res_nml().'
70  res%auto = auto
71 
72  if (auto) then
73  if (.not.(present(guess))) error stop 'Error: Missing "guess" input to function write_res_nml().'
74  res%guess = guess
75  endif
76 
77  endif
78 
79  ! Open the res_nml.txt file
80  open(action='write', file=resfile_nml, iostat=rc, newunit=fu)
81 
82  ! Write the RESTART namelist
83  write(nml=restart, iostat=rc, unit=fu)
84  if (rc /= 0) then
85  error stop 'Error: Invalid RESTART namelist defined in function write_res_nml().'
86  end if
87 
88  ! Close the res_nml.txt file
89  close(fu)
90 
91  end subroutine write_res_nml
92 
93 
94 ! ---------------------------------------------------------------------------------------
95 ! SECTION: Restart Readers
96 ! ---------------------------------------------------------------------------------------
97 
98 
99  ! -----------------------------------------------------------------------------------
100  ! ROUTINE: read_res_nml
101  !
102  ! DESCRIPTION:
106  !
107  ! PARAMETERS:
109  ! -----------------------------------------------------------------------------------
110  subroutine read_res_nml(res)
111 
112  ! Local variables
113  type(restart_type), intent(out) :: res
114 
115  ! Define the namelist
116  namelist /restart/ res
117 
118  ! Check if file exists
119  inquire(file=resfile_nml, iostat=rc)
120  if (rc /= 0) then
121  print*, 'Searched for file ', resfile_nml, ' .'
122  error stop 'Error: This restart file is not present in the current directory.'
123  end if
124 
125  ! Open the file
126  open(action='read', file=resfile_nml, iostat=rc, newunit=fu)
127 
128  ! Read the RESTART namelist
129  read(nml=restart, iostat=rc, unit=fu)
130  if (rc /= 0) then
131  error stop 'Error: Invalid RESTART namelist in "res_nml.txt".'
132  end if
133 
134  ! Close the file
135  close(fu)
136 
137  end subroutine read_res_nml
138 
139  ! -----------------------------------------------------------------------------------
140  ! ROUTINE: error_check
141  !
142  ! DESCRIPTION:
145  !
148  !
149  ! PARAMETERS:
151  ! -----------------------------------------------------------------------------------
152  subroutine error_check(ierr)
153  integer,intent(in) :: ierr
154  if (ierr /= nf90_noerr) then
155  print*, trim(nf90_strerror(ierr))
156  return
157  endif
158  end subroutine error_check
159 
160 
161  ! -----------------------------------------------------------------------------------
162  ! ROUTINE: read_energies
163  !
164  ! DESCRIPTION:
167  !
169  !
170  ! PARAMETERS:
175  ! -----------------------------------------------------------------------------------
176  subroutine read_energies(filename, energies, variances, accept_rates)
177 
178  ! Local variables
179  character(len=*), intent(in) :: filename
180  real(real64), dimension(:), intent(out) :: energies, variances, accept_rates
181  integer :: ierr, file_id, var_id
182 
183  ! Check if file exists
184  inquire(file=filename, iostat=rc)
185  if (rc /= 0) then
186  print*, 'Searched for file ', filename, ' .'
187  error stop 'Error: This restart file is not present in the current directory.'
188  end if
189 
190  ! Open restart netCDF file
191  ierr = nf90_open(filename, nf90_nowrite, file_id)
192  call error_check(ierr)
193 
194  ! Get var_id for energies
195  ierr = nf90_inq_varid(file_id, "total_energies", var_id)
196  call error_check(ierr)
197 
198  ! Read the energies
199  ierr = nf90_get_var(file_id, var_id, energies)
200  call error_check(ierr)
201 
202  ! Get var_id for variances
203  ierr = nf90_inq_varid(file_id, "Uncertainties", var_id)
204  call error_check(ierr)
205 
206  ! Read the variances
207  ierr = nf90_get_var(file_id, var_id, variances)
208  call error_check(ierr)
209 
210  ! Get attribute accept_rate
211  ierr = nf90_get_att(file_id, nf90_global, "accept_rate", accept_rates)
212  call error_check(ierr)
213 
214  ! Close the file
215  ierr = nf90_close(file_id)
216  call error_check(ierr)
217 
218  end subroutine read_energies
219 
220 
221  ! -----------------------------------------------------------------------------------
222  ! ROUTINE: read_energies_h2plus
223  !
224  ! DESCRIPTION:
228  !
230  !
231  ! PARAMETERS:
237  ! -----------------------------------------------------------------------------------
238  subroutine read_energies_h2plus(filename, energies, variances, accept_rates, c_array)
239 
240  ! Local variables
241  character(len=*), intent(in) :: filename
242  real(real64), dimension(:), intent(out) :: energies, variances, accept_rates, c_array
243  integer :: ierr, file_id, var_id
244 
245  ! Check if file exists
246  inquire(file=filename, iostat=rc)
247  if (rc /= 0) then
248  print*, 'Searched for file ', filename, ' .'
249  error stop 'Error: This restart file is not present in the current directory.'
250  end if
251 
252  ! Open restart netCDF file
253  ierr = nf90_open(filename, nf90_nowrite, file_id)
254  call error_check(ierr)
255 
256  ! Get var_id for energies
257  ierr = nf90_inq_varid(file_id, "total_energies", var_id)
258  call error_check(ierr)
259 
260  ! Read the energies
261  ierr = nf90_get_var(file_id, var_id, energies)
262  call error_check(ierr)
263 
264  ! Get var_id for variances
265  ierr = nf90_inq_varid(file_id, "Uncertainties", var_id)
266  call error_check(ierr)
267 
268  ! Read the variances
269  ierr = nf90_get_var(file_id, var_id, variances)
270  call error_check(ierr)
271 
272  ! Get var_id for c_array
273  ierr = nf90_inq_varid(file_id, "Parameter_array", var_id)
274  call error_check(ierr)
275 
276  ! Read the c_array
277  ierr = nf90_get_var(file_id, var_id, c_array)
278  call error_check(ierr)
279 
280  ! Get attribute accept_rate
281  ierr = nf90_get_att(file_id, nf90_global, "accept_rate", accept_rates)
282  call error_check(ierr)
283 
284  ! Close the file
285  ierr = nf90_close(file_id)
286  call error_check(ierr)
287 
288  end subroutine read_energies_h2plus
289 
290 
291  ! -----------------------------------------------------------------------------------
292  ! ROUTINE: read_energies_h2
293  !
294  ! DESCRIPTION:
298  !
300  !
301  ! PARAMETERS:
308  ! -----------------------------------------------------------------------------------
309  subroutine read_energies_h2(filename, energies, variances, accept_rates, a_array, b_array)
310 
311  ! Local variables
312  character(len=*), intent(in) :: filename
313  real(real64), dimension(:), intent(out) :: energies, variances, accept_rates, a_array, b_array
314  integer :: ierr, file_id, var_id
315 
316  ! Check if file exists
317  inquire(file=filename, iostat=rc)
318  if (rc /= 0) then
319  print*, 'Searched for file ', filename, ' .'
320  error stop 'Error: This restart file is not present in the current directory.'
321  end if
322 
323  ! Open restart netCDF file
324  ierr = nf90_open(filename, nf90_nowrite, file_id)
325  call error_check(ierr)
326 
327  ! Get var_id for energies
328  ierr = nf90_inq_varid(file_id, "total_energies", var_id)
329  call error_check(ierr)
330 
331  ! Read the energies
332  ierr = nf90_get_var(file_id, var_id, energies)
333  call error_check(ierr)
334 
335  ! Get var_id for variances
336  ierr = nf90_inq_varid(file_id, "Uncertainties", var_id)
337  call error_check(ierr)
338 
339  ! Read the variances
340  ierr = nf90_get_var(file_id, var_id, variances)
341  call error_check(ierr)
342 
343  ! Get var_id for a_array
344  ierr = nf90_inq_varid(file_id, "Parameter_a_array", var_id)
345  call error_check(ierr)
346 
347  ! Read the a_array
348  ierr = nf90_get_var(file_id, var_id, a_array)
349  call error_check(ierr)
350 
351  ! Get var_id for b_array
352  ierr = nf90_inq_varid(file_id, "Parameter_beta_array", var_id)
353  call error_check(ierr)
354 
355  ! Read the b_array
356  ierr = nf90_get_var(file_id, var_id, b_array)
357  call error_check(ierr)
358 
359  ! Get attribute accept_rate
360  ierr = nf90_get_att(file_id, nf90_global, "accept_rate", accept_rates)
361  call error_check(ierr)
362 
363  ! Close the file
364  ierr = nf90_close(file_id)
365  call error_check(ierr)
366 
367  end subroutine read_energies_h2
368 
369 
370 end module restart_fns
Contains routines to read and write restart files.
Definition: restart_fns.f90:15
subroutine write_res_nml(i_qoi, i_par, auto, guess)
Writes the restart file res_nml.txt, using the current states of simulation variables.
Definition: restart_fns.f90:48
subroutine read_energies_h2plus(filename, energies, variances, accept_rates, c_array)
Reads restart files with energies, variances and acceptence arrays. Also reads one extra array of opt...
subroutine error_check(ierr)
Checks error status at each point of the NetCDF read.
subroutine read_res_nml(res)
Reads the RESTART namelist in res_nml.txt. Assigns the input data to the corresponding variables in t...
subroutine read_energies(filename, energies, variances, accept_rates)
Reads restart files with energies, variances and acceptence arrays.
subroutine read_energies_h2(filename, energies, variances, accept_rates, a_array, b_array)
Reads restart files with energies, variances and acceptence arrays. Also reads two extra arrays of op...
integer rc
To capture error states during file i/o.
Definition: restart_fns.f90:24
integer fu
To store LUN of files during i/o.
Definition: restart_fns.f90:23
Contains derived types, and global variables to store input values of simulation parameters.
Definition: shared_data.f90:49
character(len= *), parameter resfile_nml
Name of restart file storing RESTART namelist.
character(len=10) p_system
System of interest: QHO, H2plus, H2.
Derived Type to store restart data.