1 |
C $Header: /u/gcmpack/MITgcm_contrib/mlosch/optim_m1qn3/optim_sub.F,v 1.3 2012/05/07 12:07:09 mlosch Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
c Include ECCO_CPPOPTIONS because the ecco_ctrl,cost files |
5 |
c have headers with options for OBCS masks. |
6 |
#include "ECCO_CPPOPTIONS.h" |
7 |
|
8 |
subroutine optim_sub( |
9 |
I nn, ff |
10 |
& ) |
11 |
|
12 |
c ================================================================== |
13 |
c subroutine optim_sub |
14 |
c ================================================================== |
15 |
c |
16 |
c o This is the main driver routine for the offline version of |
17 |
c m1qn3 (m1qn3_offline). It |
18 |
c - sets all m1qn3 relevant parameters |
19 |
c - reads the model state (control vector, cost function, |
20 |
c and gradient) |
21 |
c - reads the state of m1qn3_offline |
22 |
c - calls m1qn3_offline |
23 |
c - saves model state (control vector) and state of m1qn3_offline |
24 |
c The routine is somewhat lengthy and could be split into separate |
25 |
c subroutines, but I felt that it was easier to write and test it |
26 |
c in this form |
27 |
c Martin Losch (Martin.Losch@awi.de), Apr, 2012 |
28 |
c |
29 |
c ================================================================== |
30 |
|
31 |
implicit none |
32 |
|
33 |
c == global variables == |
34 |
|
35 |
#include "EEPARAMS.h" |
36 |
#include "SIZE.h" |
37 |
|
38 |
#include "ctrl.h" |
39 |
#include "optim.h" |
40 |
#include "m1qn3_common.h" |
41 |
|
42 |
c == routine arguments == |
43 |
|
44 |
integer nn |
45 |
_RL ff |
46 |
|
47 |
c == local variables == |
48 |
|
49 |
_RL objf |
50 |
|
51 |
#ifdef DYNAMIC |
52 |
_RL, dimension(:), allocatable :: xx, adxx |
53 |
#else |
54 |
integer nmax |
55 |
parameter( nmax = MAX_INDEPEND ) |
56 |
_RL xx(nmax) |
57 |
_RL adxx(nmax) |
58 |
#endif |
59 |
|
60 |
CML logical coldStart |
61 |
c formal parameters of m1qn3 |
62 |
integer reverse |
63 |
integer impres,imode(3),omode,niter,nsim,iz(5),indic |
64 |
_RL dxmin,df1 |
65 |
character*3 normtype |
66 |
c work arrays |
67 |
integer ndz |
68 |
CML _RL dz(ndz) |
69 |
double precision, dimension(:), allocatable :: dz |
70 |
c extra dummy variables |
71 |
integer izs(1) |
72 |
_RS rzs(1) |
73 |
_RL dzs(1) |
74 |
integer, parameter :: io = 60 |
75 |
character*(*), parameter :: fname_m1qn3 = 'm1qn3_output.txt' |
76 |
c end of m1qn3 parameters |
77 |
|
78 |
integer i |
79 |
|
80 |
c == external == |
81 |
|
82 |
external simul_rc,euclid,ctonbe,ctcabe |
83 |
|
84 |
c == end of interface == |
85 |
|
86 |
c-- Allocate memory for the control variables and the gradient vector. |
87 |
#if defined(DYNAMIC) |
88 |
allocate( xx(nn) ) |
89 |
allocate( adxx(nn) ) |
90 |
#endif |
91 |
|
92 |
#ifndef DYNAMIC |
93 |
if (nn .gt. nmax) then |
94 |
print*,' OPTIMUM: Not enough space.' |
95 |
print*,' nmax = ',nmax |
96 |
print*,' nn = ',nn |
97 |
print* |
98 |
print*,' Set MAX_INDEPEND in Makefile .ge. ',nn |
99 |
print* |
100 |
stop ' ... stopped in OPTIMUM.' |
101 |
endif |
102 |
#endif |
103 |
|
104 |
print*, ' OPTIM_SUB: Calling m1qn3_optim for iteration: ', |
105 |
& optimcycle |
106 |
print*, ' OPTIM_SUB: with nn, REAL_BYTE = ', nn, REAL_BYTE |
107 |
|
108 |
c can be 'two','sup','dfn', see m1qn3 documentation for details |
109 |
normtype='two' |
110 |
c after reading data.optim some of these parameter values can be guessed |
111 |
c impres=6, impres determines the amount of m1qn3-output see documentation |
112 |
impres=iprint |
113 |
c these should strictly be different (nsim>niter), but in practice |
114 |
c it does not matter |
115 |
niter = numiter |
116 |
nsim = nfunc*niter |
117 |
c epsg=1.d-8 |
118 |
dxmin=epsx |
119 |
c will be set later |
120 |
df1=-UNSET_RL |
121 |
c |
122 |
imode=(/0,1,0/) |
123 |
omode=-1 |
124 |
c initialise work array |
125 |
ndz = 3*nn+nupdate*(3*nn+1) |
126 |
do i=1,5 |
127 |
iz(i)=0 |
128 |
enddo |
129 |
allocate(dz(ndz)) |
130 |
do i=1,ndz |
131 |
dz(i) = 0. |
132 |
enddo |
133 |
c these alway have to be set like this |
134 |
reverse=1 |
135 |
indic=4 |
136 |
c initialise the dummy arguments that are not used |
137 |
izs(1)=UNSET_I |
138 |
rzs(1)=UNSET_RS |
139 |
dzs(1)=UNSET_RL |
140 |
|
141 |
c-- first read the model output into xx, adxx, and cost function |
142 |
c value into objf |
143 |
do i = 1,nn |
144 |
xx(i) = 0. |
145 |
adxx(i) = 0. |
146 |
enddo |
147 |
c |
148 |
print *, ' OPTIM_SUB: read model state' |
149 |
call optim_readdata( nn, ctrlname, .false., objf, xx ) |
150 |
call optim_readdata( nn, costname, .false., objf, adxx ) |
151 |
print *, ' OPTIM_SUB after reading ', |
152 |
& ctrlname, ' and ', costname, ':' |
153 |
print *, ' OPTIM_SUB nn = ', nn |
154 |
print *, ' OPTIM_SUB objf = ', objf |
155 |
print *, ' OPTIM_SUB xx(1) = ', xx(1) |
156 |
print *, ' OPTIM_SUB adxx(1) = ', adxx(1) |
157 |
|
158 |
c compute expected decrease of cost function from objf and fmin; |
159 |
c this value is only used for a cold start of m1qn3_offline, for a |
160 |
c warm start df1 is overwritten with data from a restart file |
161 |
df1=objf-fmin |
162 |
if ( df1 .le. 0. ) then |
163 |
print *, ' OPTIM_SUB: df1 = objf-fmin = ', df1, ' should be > 0.' |
164 |
stop 'ABNORMAL in S/R OPTIM_SUB' |
165 |
endif |
166 |
|
167 |
c global variable coldStart is set in s/r optim_readparms |
168 |
c the default is false, always set it to true for the 0th cycle |
169 |
if ( optimcycle .eq. 0 ) coldStart=.true. |
170 |
if ( coldStart ) then |
171 |
c-- cold start |
172 |
print *, ' OPTIM_SUB: cold start, optimcycle =', optimcycle |
173 |
imode(2) = 0 |
174 |
c this variable is the only one of the m1qn3-related common blocks |
175 |
c that needs to be initialized here to make sure that we have a |
176 |
c clean start |
177 |
reentry = 0 |
178 |
c open output file for m1qn3 |
179 |
open(io,file=fname_m1qn3,status='unknown') |
180 |
else |
181 |
c-- warm restart |
182 |
c requires restoring the state of m1qn3 with pickup file |
183 |
print *, ' OPTIM_SUB: warm start, optimcycle =', optimcycle |
184 |
imode(2) = 1 |
185 |
call optim_store_m1qn3(ndz,iz,dz,niter,nsim,epsg,df1, |
186 |
I optimcycle, |
187 |
I .false.) |
188 |
c re-open output file for m1qn3 |
189 |
open(io,file=fname_m1qn3,status='old',position='append') |
190 |
endif |
191 |
|
192 |
c-- call the minimizer, a slightly modified version of m1qn3 v3.3 |
193 |
c (Gilbert & Lemarechal, 1989), downloaded in April 2012. |
194 |
c simul_rc,euclid,ctonbe,ctcabe are external subroutines that |
195 |
c are provided within m1qn3. euclid, ctonbe, ctcabe can be replaced |
196 |
c by something more efficient, simul_rc is a dummy routine for |
197 |
c the reverse communication mode and should not be changed. |
198 |
print *, ' OPTIM_SUB: call m1qn3_offline' |
199 |
call m1qn3_offline (simul_rc,euclid,ctonbe,ctcabe, |
200 |
& nn,xx,objf,adxx,dxmin,df1, |
201 |
& epsg,normtype,impres,io,imode,omode,niter,nsim, |
202 |
& iz,dz,ndz,reverse,indic,izs,rzs,dzs) |
203 |
close(io) |
204 |
print *, ' OPTIM_SUB: returned from m1qn3_offline' |
205 |
|
206 |
c write state of m1qn3 into pickup file for warm restart |
207 |
call optim_store_m1qn3(ndz,iz,dz,niter,nsim,epsg,df1, |
208 |
I optimcycle, |
209 |
I .true.) |
210 |
c write model control vector |
211 |
print *,' OPTIMS_SUB: writing ', nn,' sized control to file ', |
212 |
& ctrlname |
213 |
c give the cost function a funny value to make sure that nobody |
214 |
c mistakes it for the real one |
215 |
call optim_writedata( nn, ctrlname, .false., -9999., xx ) |
216 |
|
217 |
c clean up |
218 |
#ifdef DYNAMIC |
219 |
deallocate(xx, adxx) |
220 |
#endif /* DYNAMIC */ |
221 |
deallocate(dz) |
222 |
|
223 |
c stopping criterion |
224 |
if ( reverse .lt. 0 ) then |
225 |
print *, ' OPTIM_SUB: reverse = ', reverse |
226 |
print *, ' OPTIM_SUB: optimization terminated with omode = ', |
227 |
& omode |
228 |
stop 'ABNORMAL in S/R OPTIM_SUB' |
229 |
endif |
230 |
|
231 |
return |
232 |
end |