1 |
C $Header$ |
C $Header$ |
2 |
C $Name$ |
C $Name$ |
3 |
|
|
4 |
c Include ECCO_CPPOPTIONS because the ecco_ctrl,cost files |
C ECCO_CPPOPTIONS used to affect maxcvars and defined ALLOW_OBCS?_CONTROL |
5 |
c have headers with options for OBCS masks. |
C#include "ECCO_CPPOPTIONS.h" |
6 |
#include "ECCO_CPPOPTIONS.h" |
C now: |
7 |
|
C CTRL_OPTIONS affects maxcvars and may define ALLOW_OBCS?_CONTROL |
8 |
|
#include "CTRL_OPTIONS.h" |
9 |
|
|
10 |
subroutine optim_sub( |
subroutine optim_sub( |
11 |
I nn, ff |
I nn, ff |
50 |
|
|
51 |
_RL objf |
_RL objf |
52 |
|
|
53 |
#if defined (DYNAMIC) |
#ifdef DYNAMIC |
54 |
_RL xx(nn) |
_RL, dimension(:), allocatable :: xx, adxx |
|
_RL adxx(nn) |
|
|
#elif defined (USE_POINTER) || (MAX_INDEPEND == 0) |
|
|
_RL xx |
|
|
_RL adxx |
|
|
pointer (pxx,xx(1)) |
|
|
pointer (padxx,adxx(1)) |
|
55 |
#else |
#else |
56 |
integer nmax |
integer nmax |
57 |
parameter( nmax = MAX_INDEPEND ) |
parameter( nmax = MAX_INDEPEND ) |
58 |
_RL xx(nmax) |
_RL xx(nmax) |
59 |
_RL adxx(nmax) |
_RL adxx(nmax) |
60 |
#endif |
#endif |
61 |
|
_RL xxmean |
62 |
|
|
63 |
|
CML logical coldStart |
64 |
c formal parameters of m1qn3 |
c formal parameters of m1qn3 |
65 |
integer reverse |
integer reverse |
66 |
integer impres,imode(3),omode,niter,nsim,iz(5),indic |
integer impres,imode(3),omode,niter,nsim,iz(5),indic |
67 |
_RL dxmin,df1 |
_RL dxmin,df1 |
68 |
character*3 normtype |
character*3 normtype |
69 |
c work arrays |
c work arrays |
70 |
integer ndz, mupdate |
integer ndz |
71 |
CML _RL dz(ndz) |
CML _RL dz(ndz) |
72 |
double precision, dimension(:), allocatable :: dz |
double precision, dimension(:), allocatable :: dz |
73 |
c extra dummy variables |
c extra dummy variables |
88 |
|
|
89 |
c-- Allocate memory for the control variables and the gradient vector. |
c-- Allocate memory for the control variables and the gradient vector. |
90 |
#if defined(DYNAMIC) |
#if defined(DYNAMIC) |
91 |
#elif defined(USE_POINTER) || (MAX_INDEPEND == 0) |
allocate( xx(nn) ) |
92 |
call myalloc( pxx , nn*REAL_BYTE ) |
allocate( adxx(nn) ) |
|
call myalloc( padxx, nn*REAL_BYTE ) |
|
93 |
#endif |
#endif |
94 |
|
|
95 |
#if defined (DYNAMIC) |
#ifndef DYNAMIC |
|
#elif defined(USE_POINTER) || (MAX_INDEPEND == 0) |
|
|
#else |
|
96 |
if (nn .gt. nmax) then |
if (nn .gt. nmax) then |
97 |
print*,' OPTIMUM: Not enough space.' |
print*,' OPTIMUM: Not enough space.' |
98 |
print*,' nmax = ',nmax |
print*,' nmax = ',nmax |
141 |
rzs(1)=UNSET_RS |
rzs(1)=UNSET_RS |
142 |
dzs(1)=UNSET_RL |
dzs(1)=UNSET_RL |
143 |
|
|
144 |
if ( optimcycle .eq. 0 ) then |
c-- first read the model output into xx, adxx, and cost function |
145 |
|
c value into objf |
146 |
|
do i = 1,nn |
147 |
|
xx(i) = 0. |
148 |
|
adxx(i) = 0. |
149 |
|
enddo |
150 |
|
c |
151 |
|
print *, ' OPTIM_SUB: read model state' |
152 |
|
call optim_readdata( nn, ctrlname, .false., objf, xx ) |
153 |
|
call optim_readdata( nn, costname, .false., objf, adxx ) |
154 |
|
print *, ' OPTIM_SUB after reading ', |
155 |
|
& ctrlname, ' and ', costname, ':' |
156 |
|
print *, ' OPTIM_SUB nn = ', nn |
157 |
|
print *, ' OPTIM_SUB objf = ', objf |
158 |
|
print *, ' OPTIM_SUB xx(1) = ', xx(1) |
159 |
|
print *, ' OPTIM_SUB adxx(1) = ', adxx(1) |
160 |
|
|
161 |
|
if ( coldStart ) then |
162 |
c-- cold start |
c-- cold start |
163 |
print *, ' OPTIM_SUB: cold start, optimcycle =', optimcycle |
print *, ' OPTIM_SUB: cold start, optimcycle =', optimcycle |
164 |
imode(2) = 0 |
imode(2) = 0 |
166 |
c that needs to be initialized here to make sure that we have a |
c that needs to be initialized here to make sure that we have a |
167 |
c clean start |
c clean start |
168 |
reentry = 0 |
reentry = 0 |
169 |
c ff has be read in optim_readparms, so we do not read it here again |
c compute expected decrease of cost function from objf and fmin; |
170 |
objf = ff |
c this value is only used for a cold start of m1qn3_offline, for a |
171 |
df1 = objf-fmin |
c warm start df1 is overwritten with data from a restart file |
172 |
|
df1=objf-fmin |
173 |
|
if ( df1 .le. 0. ) then |
174 |
|
print *, ' OPTIM_SUB: df1 = objf-fmin = ', df1, |
175 |
|
& ' but should be > 0.' |
176 |
|
stop 'ABNORMAL in S/R OPTIM_SUB' |
177 |
|
endif |
178 |
|
|
179 |
c open output file for m1qn3 |
c open output file for m1qn3 |
180 |
open(io,file=fname_m1qn3,status='unknown') |
open(io,file=fname_m1qn3,status='unknown') |
181 |
else |
else |
189 |
c re-open output file for m1qn3 |
c re-open output file for m1qn3 |
190 |
open(io,file=fname_m1qn3,status='old',position='append') |
open(io,file=fname_m1qn3,status='old',position='append') |
191 |
endif |
endif |
|
c-- read the model output into xx,adxx |
|
|
if ( indic .eq. 4 ) then |
|
|
do i = 1,nn |
|
|
xx(i) = 0. |
|
|
adxx(i) = 0. |
|
|
enddo |
|
|
c |
|
|
print *, ' OPTIM_SUB: read model state' |
|
|
call optim_readdata( nn, ctrlname, .false., objf, xx ) |
|
|
call optim_readdata( nn, costname, .false., objf, adxx ) |
|
|
print *, ' OPTIM_SUB after reading nn, objf = ', nn, objf, |
|
|
& xx(1), adxx(1) |
|
|
else |
|
|
print *, ' OPTIM_SUB: indic = ', indic, ' is not possible' |
|
|
stop 'ABNORMAL in S/R OPTIM_SUB' |
|
|
endif |
|
192 |
|
|
193 |
c-- call the minimizer, a slightly modified version of m1qn3 v3.3 |
c-- call the minimizer, a slightly modified version of m1qn3 v3.3 |
194 |
c (Gilbert & Lemarechal, 1989), downloaded in April 2012. |
c (Gilbert & Lemarechal, 1989), downloaded in April 2012. |
196 |
c are provided within m1qn3. euclid, ctonbe, ctcabe can be replaced |
c are provided within m1qn3. euclid, ctonbe, ctcabe can be replaced |
197 |
c by something more efficient, simul_rc is a dummy routine for |
c by something more efficient, simul_rc is a dummy routine for |
198 |
c the reverse communication mode and should not be changed. |
c the reverse communication mode and should not be changed. |
199 |
print *, ' OPTIM_SUB: call m1qn3_offline' |
print *, ' OPTIM_SUB: call m1qn3_offline ........' |
200 |
call m1qn3_offline (simul_rc,euclid,ctonbe,ctcabe, |
call m1qn3_offline (simul_rc,euclid,ctonbe,ctcabe, |
201 |
& nn,xx,objf,adxx,dxmin,df1, |
& nn,xx,objf,adxx,dxmin,df1, |
202 |
& epsg,normtype,impres,io,imode,omode,niter,nsim, |
& epsg,normtype,impres,io,imode,omode,niter,nsim, |
203 |
& iz,dz,ndz,reverse,indic,izs,rzs,dzs) |
& iz,dz,ndz,reverse,indic,izs,rzs,dzs) |
204 |
close(io) |
close(io) |
205 |
|
print *, ' OPTIM_SUB: ...........................' |
206 |
print *, ' OPTIM_SUB: returned from m1qn3_offline' |
print *, ' OPTIM_SUB: returned from m1qn3_offline' |
207 |
|
print *, ' OPTIM_SUB: nn = ', nn |
208 |
|
print *, ' OPTIM_SUB: xx(1) = ', xx(1), xx(2) |
209 |
|
print *, ' OPTIM_SUB: adxx(1) = ', adxx(1), adxx(2) |
210 |
|
print *, ' OPTIM_SUB: omode = ', omode |
211 |
|
print *, ' OPTIM_SUB: niter = ', niter |
212 |
|
print *, ' OPTIM_SUB: nsim = ', nsim |
213 |
|
print *, ' OPTIM_SUB: reverse = ', reverse |
214 |
|
|
215 |
|
c compute min/max/mean/std of output vector see if it is within |
216 |
|
c reasonable bounds (prior to scaling) |
217 |
|
xxmean = sum(xx)/dble(nn) |
218 |
|
print * |
219 |
|
print *,' OPTIM_SUB: mean(xx) =', xxmean |
220 |
|
print *,' OPTIM_SUB: max(xx) =', maxval(xx) |
221 |
|
print *,' OPTIM_SUB: min(xx) =', minval(xx) |
222 |
|
print *,' OPTIM_SUB: std(xx) =',sum((xx-xxmean)**2)/dble(nn) |
223 |
|
print * |
224 |
c write state of m1qn3 into pickup file for warm restart |
c write state of m1qn3 into pickup file for warm restart |
225 |
call optim_store_m1qn3(ndz,iz,dz,niter,nsim,epsg,df1, |
call optim_store_m1qn3(ndz,iz,dz,niter,nsim,epsg,df1, |
226 |
I optimcycle, |
I optimcycle, |
227 |
I .true.) |
I .true.) |
228 |
c write model control vector |
c write model control vector |
229 |
print *,' OPTIMS_SUB: writing ', nn,' sized control to file ', |
print *,' OPTIM_SUB: writing ', nn,' sized control to file ', |
230 |
& ctrlname |
& ctrlname |
231 |
c give the cost function a funny value to make sure that nobody |
c give the cost function a funny value to make sure that nobody |
232 |
c mistakes it for the real one |
c mistakes it for the real one |
233 |
call optim_writedata( nn, ctrlname, .false., -9999., xx ) |
call optim_writedata( nn, ctrlname, .false., -9999., xx ) |
234 |
|
|
235 |
c clean up |
c clean up |
236 |
|
#ifdef DYNAMIC |
237 |
|
deallocate(xx, adxx) |
238 |
|
#endif /* DYNAMIC */ |
239 |
deallocate(dz) |
deallocate(dz) |
240 |
|
|
241 |
c stopping criterion |
c stopping criterion |