EMPIRE DA  v1.9.1
Data assimilation codes using EMPIRE communication
 All Classes Files Functions Variables Pages
var_data.f90
Go to the documentation of this file.
1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2 !!! Time-stamp: <2016-10-18 14:56:12 pbrowne>
3 !!!
4 !!! module to store data for variational methods
5 !!! Copyright (C) 2015 Philip A. Browne
6 !!!
7 !!! This program is free software: you can redistribute it and/or modify
8 !!! it under the terms of the GNU General Public License as published by
9 !!! the Free Software Foundation, either version 3 of the License, or
10 !!! (at your option) any later version.
11 !!!
12 !!! This program is distributed in the hope that it will be useful,
13 !!! but WITHOUT ANY WARRANTY; without even the implied warranty of
14 !!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 !!! GNU General Public License for more details.
16 !!!
17 !!! You should have received a copy of the GNU General Public License
18 !!! along with this program. If not, see <http://www.gnu.org/licenses/>.
19 !!!
20 !!! Email: p.browne @ reading.ac.uk
21 !!! Mail: School of Mathematical and Physical Sciences,
22 !!! University of Reading,
23 !!! Reading, UK
24 !!! RG6 6BB
25 !!!
26 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27 
29 module var_data
30 
31  implicit none
32  type, public :: var_control_type
33  character(6) :: opt_method
39  integer :: cg_method
44  real(kind=kind(1.0d0)) :: cg_eps
47 
48  real(kind=kind(1.0d0)) :: lbfgs_factr
49 
50 
65 
66  real(kind=kind(1.0d0)) :: lbfgs_pgtol
67 
77 
78  real(kind=kind(1.0d0)), dimension(:), allocatable :: l,u,x0
79  integer, dimension(:), allocatable :: nbd
80 
81  integer :: n
82  integer :: total_timesteps
84 
85  integer, allocatable, dimension(:) :: ny
91 
92  end type var_control_type
93  type(var_control_type), save :: vardata
94 
95 
96 contains
98  subroutine set_var_controls
99  use output_empire, only : emp_o
100 
101  write(emp_o,'(A)') 'Opening namelist file to read var_params'
102 
103  call parse_vardata
104 
105  write(emp_o,'(A)') 'var_params successfully read from nml file to control pf code.'
106 
107  end subroutine set_var_controls
108 
109 
139  subroutine parse_vardata
140  use output_empire, only : unit_vardata,emp_e
141  implicit none
142  character(*), parameter :: filename='vardata.nml'
143  character(*), parameter :: filename2='empire.nml'
144  integer :: ios
145 
146  character(6) :: opt_method='CG'
147  integer :: cg_method=2
148  real(kind=kind(1.0d0)) :: cg_eps=1.0d-5
149  real(kind=kind(1.0d0)) :: lbfgs_factr=1.0d+7
150  real(kind=kind(1.0d0)) :: lbfgs_pgtol=1.0d-5
151  integer :: n=-1
152  integer :: total_timesteps = -1
153 
154  namelist/var_params/opt_method,&
155  &cg_method,&
156  &cg_eps,&
157  &lbfgs_factr,&
158  &lbfgs_pgtol,&
159  &total_timesteps,&
160  &n
161 
162  !set defaults:
163  vardata%opt_method = opt_method
164  vardata%cg_method = cg_method
165  vardata%cg_eps = cg_eps
166  vardata%lbfgs_factr = lbfgs_factr
167  vardata%lbfgs_pgtol = lbfgs_pgtol
168  vardata%n = n
169  vardata%total_timesteps = total_timesteps
170 
171 
172 
173  open(unit_vardata,file=filename,iostat=ios,action='read',status='old')
174  if(ios .ne. 0) then
175  write(*,*) 'Cannot open ',filename
176 
177  open(unit_vardata,file=filename2,iostat=ios,action='read',status='old')
178 
179  if(ios .ne. 0) then
180  write(emp_e,*) 'Cannot open ',filename2
181  write(emp_e,*) 'var_data ERROR: no .nml file found. STOPPING.'
182  stop '-65'
183  end if
184 
185  read(unit_vardata,nml=var_params,iostat=ios)
186  if(ios .ne. 0) then
187  write(emp_e,*) 'var_data ERROR: no var_params namelist found in &
188  &',filename,' or ',filename2,'. STOPPING.'
189  stop '-66'
190  end if
191 
192  else
193  read(unit_vardata,nml=var_params,iostat=ios)
194  if(ios .ne. 0) then
195  close(unit_vardata)
196  open(unit_vardata,file=filename2,iostat=ios,action='read',status='old')
197  if(ios .ne. 0) then
198  write(emp_e,*) 'var_data ERROR: no var_params namelist found &
199  &in &
200  &',filename,' and could not open ',filename2,'. STOPPING.'
201  stop '-67'
202  end if
203 
204  read(unit_vardata,nml=var_params,iostat=ios)
205  if(ios .ne. 0) then
206  write(emp_e,*) 'var_data ERROR: no var_params namelist found in &
207  &',filename,' or ',filename2,'. STOPPING.'
208  stop '-68'
209  end if
210  end if
211  end if
212  close(unit_vardata)
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230  select case (opt_method)
231  case('cg')
232  write(*,*) 'VAR_DATA: Nonlinear Conjugate Gradient Method selected'
233  vardata%opt_method = opt_method
234  case('lbfgs')
235  write(*,*) 'VAR_DATA: Unconstrained L-BFGS Method selected'
236  vardata%opt_method = opt_method
237  case('lbfgsb')
238  write(*,*) 'VAR_DATA: Bound constrained L-BFGS Method selected'
239  vardata%opt_method = opt_method
240  case default
241  write(emp_e,*) 'VAR_DATA ERROR: opt_method in ',filename,' incorrect&
242  &ly given as ',opt_method
243  write(emp_e,*) 'VAR_DATA ERROR: Correct inputs are:'
244  write(emp_e,*) 'VAR_DATA ERROR: cg'
245  write(emp_e,*) 'VAR_DATA ERROR: lbfgs'
246  write(emp_e,*) 'VAR_DATA ERROR: lbfgsb'
247  stop 3
248  end select
249 
250  if(cg_method .lt. 1 .or. cg_method .gt. 3) then
251  write(emp_e,*) 'VAR_DATA ERROR: cg_method in ',filename, ' incorrect&
252  &ly given as ',cg_method
253  write(emp_e,*) 'VAR_DATA ERROR: Correct inputs are:'
254  write(emp_e,*) 'VAR_DATA ERROR: 1 FLETCHER-REEVES '
255  write(emp_e,*) 'VAR_DATA ERROR: 2 POLAK-RIBIERE (DEFAULT)'
256  write(emp_e,*) 'VAR_DATA ERROR: 3 POSITIVE POLAK-RIBIERE ( BETA=MAX{BETA,0} )'
257  stop 4
258  else
259  vardata%cg_method = cg_method
260  if(vardata%opt_method .eq. 'cg') then
261  select case (vardata%cg_method)
262  case(1)
263  write(*,*) 'CG METHOD 1: FLETCHER-REEVES '
264  case(2)
265  write(*,*) 'CG METHOD 2: POLAK-RIBIERE (DEFAULT)'
266  case(3)
267  write(*,*) 'CG METHOD 3: POSITIVE POLAK-RIBIERE'
268  case default
269  end select
270  end if
271  end if
272 
273 
274  if(cg_eps .ne. 1.0d-5) then
275  if(cg_eps .lt. 0.0d0) then
276  write(emp_e,*) 'VAR_DATA ERROR: cg_eps read as negative: ',cg_eps
277  write(emp_e,*) 'VAR_DATA ERROR: Please make cg_eps positive (small).'
278  stop 5
279  elseif(cg_eps .ge. 0.5d0) then
280  write(*,*) 'VAR_DATA WARNING: cg_eps read as "large": '&
281  &,cg_eps
282  write(*,*) 'VAR_DATA WARNING: cg_eps default is 1.0d-5'
283  else
284  write(*,*) 'VAR_DATA: CG tolerance read in as ',cg_eps
285  vardata%cg_eps = cg_eps
286  end if
287  else
288  vardata%cg_eps = 1.0d-5
289  end if
290 
291  if(lbfgs_factr .ne. 1.0d+7) then
292  write(*,*) 'VAR_DATA: LBFGS factr read in as ',lbfgs_factr
293  vardata%lbfgs_factr = lbfgs_factr
294  end if
295 
296 
297  if(lbfgs_pgtol .ne. 1.0d-5) then
298  write(*,*) 'VAR_DATA: LBFGS pgtol read in as ',lbfgs_pgtol
299  vardata%lbfgs_pgtol = lbfgs_pgtol
300  end if
301 
302 
303  ! if(n .lt. 1) then
304  ! write(*,*) 'VAR_DATA ERROR: n < 1'
305  ! stop 2
306  ! else
307  ! vardata%n = n
308  ! end if
309  !print*,'nens = ',nens
310  !vardata%n = nens-1
311 
312  !vardata%n = -2
313 
314 
315  if(total_timesteps .lt. 1) then
316  write(emp_e,*) 'VAR_DATA ERROR: total_timesteps < 1'
317  stop 6
318  else
319  vardata%total_timesteps = total_timesteps
320  end if
321 
322 
323 
324  end subroutine parse_vardata
325 
326 
328  subroutine allocate_vardata
329  allocate(vardata%x0(vardata%n))
330 
331  allocate(vardata%ny(vardata%total_timesteps))
332 
333 
334  if(vardata%opt_method == 'lbfgsb') then
335  allocate(vardata%nbd(vardata%n))
336  allocate(vardata%l(vardata%n))
337  allocate(vardata%u(vardata%n))
338  end if
339 
340  end subroutine allocate_vardata
341 
344  if(allocated(vardata%x0)) deallocate(vardata%x0)
345  if(allocated(vardata%ny)) deallocate(vardata%ny)
346  if(allocated(vardata%nbd)) deallocate(vardata%nbd)
347  if(allocated(vardata%l)) deallocate(vardata%l)
348  if(allocated(vardata%u)) deallocate(vardata%u)
349  end subroutine deallocate_vardata
350 
351 
354  end subroutine read_lbfgsb_bounds
355 
358  vardata%ny = 0
359  vardata%ny(8) = 3
360  vardata%ny(16) = 3
361  vardata%ny(24) = 3
362  print*,'vardata%ny = ',vardata%ny
363  end subroutine read_observation_numbers
364 
365 
366 end module var_data
367 
368 
369 
subroutine allocate_vardata
subroutine to allocate space for 4denvar
Definition: var_data.f90:328
subroutine parse_vardata
subroutine to read the namelist file and save it to vardata datatype Here we read vardata...
Definition: var_data.f90:139
subroutine deallocate_vardata
subroutine to deallocate space for 4denvar
Definition: var_data.f90:343
Module that stores the information about the outputting from empire.
subroutine read_observation_numbers
subroutine to somehow read in observation numbers
Definition: var_data.f90:357
subroutine read_lbfgsb_bounds
subroutine to somehow read in bounds data
Definition: var_data.f90:353
subroutine set_var_controls
subroutine to ensure vardata is ok
Definition: var_data.f90:98
module holding data for variational problems
Definition: var_data.f90:29