/[MITgcm]/MITgcm_contrib/mlosch/optim_m1qn3/optim_readdata.F
ViewVC logotype

Annotation of /MITgcm_contrib/mlosch/optim_m1qn3/optim_readdata.F

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (hide annotations) (download)
Thu Apr 26 11:10:06 2012 UTC (12 years ago) by mlosch
Branch: MAIN
First working version of a new optimization package that uses a slightly
modified version of m1qn3, v3.3
(https://who.rocq.inria.fr/Jean-Charles.Gilbert/modulopt/optimization-routines/m1qn3/m1qn3.html)
to work as an offline optimizer. The advantage of m1qn3_offline is, that
it is run in reverse communication control mode, so that it gives back
control to the call routine (here a script) to provide a new estimate of the
cost function and the gradient based on the control vector. This way we can
do complete line searches that are meaningful.

1 mlosch 1.1 C $Header: $
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_readdata(
9     I nn,
10     I dfile,
11     I lheaderonly,
12     O ff,
13     O vv
14     & )
15    
16     c ==================================================================
17     c SUBROUTINE optim_readdata
18     c ==================================================================
19     c
20     c o Read the data written by the MITgcmUV state estimation setup and
21     c join them to one vector that is subsequently used by the minimi-
22     c zation algorithm "lsopt". Depending on the specified file name
23     c either the control vector or the gradient vector can be read.
24     c
25     c *dfile* should be the radix of the file: ecco_ctrl or ecco_cost
26     c
27     c started: Christian Eckert eckert@mit.edu 12-Apr-2000
28     c
29     c changed: Patrick Heimbach heimbach@mit.edu 19-Jun-2000
30     c - finished, revised and debugged
31     c
32     c ==================================================================
33     c SUBROUTINE optim_readdata
34     c ==================================================================
35    
36     implicit none
37    
38     c == global variables ==
39    
40     #include "EEPARAMS.h"
41     #include "SIZE.h"
42     #include "ctrl.h"
43     #include "optim.h"
44    
45     c == routine arguments ==
46    
47     integer nn
48     _RL ff
49    
50     #if defined (DYNAMIC)
51     _RL vv(nn)
52     #elif defined (USE_POINTER) || (MAX_INDEPEND == 0)
53     _RL vv
54     pointer (pvv,vv(1))
55     #else
56     integer nmax
57     parameter( nmax = MAX_INDEPEND )
58     _RL vv(nmax)
59     #endif
60    
61     character*(9) dfile
62     logical lheaderonly
63    
64     c == local variables ==
65    
66     integer bi,bj
67     integer biG,bjG
68     integer i,j
69     integer ii,k
70     integer icvar
71     integer icvrec
72     integer icvcomp
73     integer icvoffset
74     integer nopt
75     integer funit
76    
77     integer cbuffindex
78     real*4 cbuff( sNx*nSx*nPx*sNy*nSy*nPy )
79    
80     character*(128) fname
81    
82     c integer filei
83     c integer filej
84     c integer filek
85     c integer fileiG
86     c integer filejG
87     c integer filensx
88     c integer filensy
89     integer filenopt
90     _RL fileff
91    
92     cgg(
93     _RL gg
94     integer igg
95     integer iobcs
96     cgg)
97    
98     c == end of interface ==
99    
100     print *, 'pathei-lsopt in optim_readdata'
101    
102     c-- The reference i/o unit.
103     funit = 20
104    
105     c-- Next optimization cycle.
106     nopt = optimcycle
107    
108     if ( dfile .eq. ctrlname ) then
109     print*
110     print*,' OPTIM_READDATA: Reading control vector'
111     print*,' for optimization cycle: ',nopt
112     print*
113     else if ( dfile .eq. costname ) then
114     print*
115     print*,' OPTIM_READDATA: Reading cost function and'
116     print*,' gradient of cost function'
117     print*,' for optimization cycle: ',nopt
118     print*
119     else
120     print*
121     print*,' OPTIM_READDATA: subroutine called by a false *dfile*'
122     print*,' argument. *dfile* = ',dfile
123     print*
124     stop ' ... stopped in OPTIM_READDATA.'
125     endif
126    
127     c-- Read the data.
128    
129     bjG = 1 + (myygloballo - 1)/sny
130     biG = 1 + (myxgloballo - 1)/snx
131    
132     c-- Generate file name and open the file.
133     write(fname(1:128),'(4a,i4.4)')
134     & dfile,'_',yctrlid(1:10),'.opt', nopt
135     open( funit, file = fname,
136     & status = 'old',
137     & form = 'unformatted',
138     & access = 'sequential' )
139     print*, 'opened file ', fname
140    
141     c-- Read the header.
142     read( funit ) nvartype
143     read( funit ) nvarlength
144     read( funit ) yctrlid
145     read( funit ) filenopt
146     read( funit ) fileff
147     read( funit ) fileiG
148     read( funit ) filejG
149     read( funit ) filensx
150     read( funit ) filensy
151    
152     read( funit ) (nWetcGlobal(k), k=1,nr)
153     read( funit ) (nWetsGlobal(k), k=1,nr)
154     read( funit ) (nWetwGlobal(k), k=1,nr)
155     #ifdef ALLOW_CTRL_WETV
156     read( funit ) (nWetvGlobal(k), k=1,nr)
157     #endif
158     #ifdef ALLOW_SHIFWFLX_CONTROL
159     read(funit) (nWetiGlobal(k), k=1,nr)
160     c read(funit) nWetiGlobal(1)
161     #endif
162    
163     cgg( Add OBCS Mask information into the header section for optimization.
164     #ifdef ALLOW_OBCSN_CONTROL
165     read( funit ) ((nWetobcsnGlo(k,iobcs), k=1,nr),iobcs= 1,nobcs)
166     #endif
167     #ifdef ALLOW_OBCSS_CONTROL
168     read( funit ) ((nWetobcssGlo(k,iobcs), k=1,nr),iobcs= 1,nobcs)
169     #endif
170     #ifdef ALLOW_OBCSW_CONTROL
171     read( funit ) ((nWetobcswGlo(k,iobcs), k=1,nr),iobcs= 1,nobcs)
172     #endif
173     #ifdef ALLOW_OBCSE_CONTROL
174     read( funit ) ((nWetobcseGlo(k,iobcs), k=1,nr),iobcs= 1,nobcs)
175     #endif
176     cgg)
177     read( funit ) (ncvarindex(i), i=1,maxcvars)
178     read( funit ) (ncvarrecs(i), i=1,maxcvars)
179     read( funit ) (ncvarxmax(i), i=1,maxcvars)
180     read( funit ) (ncvarymax(i), i=1,maxcvars)
181     read( funit ) (ncvarnrmax(i), i=1,maxcvars)
182     read( funit ) (ncvargrd(i), i=1,maxcvars)
183     read( funit )
184    
185     cph(
186     cph if (lheaderonly) then
187     print *, 'pathei: nvartype ', nvartype
188     print *, 'pathei: nvarlength ', nvarlength
189     print *, 'pathei: yctrlid ', yctrlid
190     print *, 'pathei: filenopt ', filenopt
191     print *, 'pathei: fileff ', fileff
192     print *, 'pathei: fileiG ', fileiG
193     print *, 'pathei: filejG ', filejG
194     print *, 'pathei: filensx ', filensx
195     print *, 'pathei: filensy ', filensy
196    
197     print *, 'pathei: nWetcGlobal ',
198     & (nWetcGlobal(k), k=1,nr)
199     print *, 'pathei: nWetsGlobal ',
200     & (nWetsGlobal(k), k=1,nr)
201     print *, 'pathei: nWetwGlobal ',
202     & (nWetwGlobal(k), k=1,nr)
203     print *, 'pathei: nWetvGlobal ',
204     & (nWetvGlobal(k), k=1,nr)
205     #ifdef ALLOW_SHIFWFLX_CONTROL
206     print *, 'pathei: nWetiGlobal ',
207     & (nWetiGlobal(k), k=1,nr)
208     #endif
209     #ifdef ALLOW_OBCSN_CONTROL
210     do iobcs=1,nobcs
211     print *, 'pathei: nWetobcsnGlo (iobcs=', iobcs,')',
212     & (nWetobcsnGlo(k,iobcs), k=1,nr)
213     enddo
214     #endif
215     #ifdef ALLOW_OBCSS_CONTROL
216     do iobcs=1,nobcs
217     print *, 'pathei: nWetobcssGlo (iobcs=', iobcs,')',
218     & (nWetobcssGlo(k,iobcs), k=1,nr)
219     enddo
220     #endif
221     #ifdef ALLOW_OBCSW_CONTROL
222     do iobcs=1,nobcs
223     print *, 'pathei: nWetobcswGlo (iobcs=', iobcs,')',
224     & (nWetobcswGlo(k,iobcs), k=1,nr)
225     enddo
226     #endif
227     #ifdef ALLOW_OBCSE_CONTROL
228     do iobcs=1,nobcs
229     print *, 'pathei: nWetobcseGlo (iobcs=', iobcs,')',
230     & (nWetobcseGlo(k,iobcs), k=1,nr)
231     enddo
232     #endif
233     print *, 'pathei: ncvarindex ',
234     & (ncvarindex(i), i=1,maxcvars)
235     print *, 'pathei: ncvarrecs ',
236     & (ncvarrecs(i), i=1,maxcvars)
237     print *, 'pathei: ncvarxmax ',
238     & (ncvarxmax(i), i=1,maxcvars)
239     print *, 'pathei: ncvarymax ',
240     & (ncvarymax(i), i=1,maxcvars)
241     print *, 'pathei: ncvarnrmax ',
242     & (ncvarnrmax(i), i=1,maxcvars)
243     print *, 'pathei: ncvargrd ',
244     & (ncvargrd(i), i=1,maxcvars)
245     cph end if
246     cph)
247     c-- Check the header information for consistency.
248    
249     cph if ( filenopt .ne. nopt ) then
250     cph print*
251     cph print*,' READ_HEADER: Input data belong to the wrong'
252     cph print*,' optimization cycle.'
253     cph print*,' optimization cycle = ',nopt
254     cph print*,' input optim cycle = ',filenopt
255     cph print*
256     cph stop ' ... stopped in READ_HEADER.'
257     cph endif
258    
259     if ( (fileiG .ne. biG) .or. (filejG .ne. bjG) ) then
260     print*
261     print*,' READ_HEADER: Tile indices of loop and data '
262     print*,' do not match.'
263     print*,' loop x/y component = ',
264     & biG,bjG
265     print*,' data x/y component = ',
266     & fileiG,filejG
267     print*
268     stop ' ... stopped in READ_HEADER.'
269     endif
270    
271     if ( (filensx .ne. nsx) .or. (filensy .ne. nsy) ) then
272     print*
273     print*,' READ_HEADER: Numbers of tiles do not match.'
274     print*,' parameter x/y no. of tiles = ',
275     & bi,bj
276     print*,' data x/y no. of tiles = ',
277     & filensx,filensy
278     print*
279     stop ' ... stopped in READ_HEADER.'
280     endif
281    
282     ce Add some more checks. ...
283    
284     if (.NOT. lheaderonly) then
285     c-- Read the data.
286     icvoffset = 0
287     do icvar = 1,maxcvars
288     if ( ncvarindex(icvar) .ne. -1 ) then
289     do icvrec = 1,ncvarrecs(icvar)
290     do bj = 1,nsy
291     do bi = 1,nsx
292     read( funit ) ncvarindex(icvar)
293     read( funit ) filej
294     read( funit ) filei
295     do k = 1,ncvarnrmax(icvar)
296     cbuffindex = 0
297     if (ncvargrd(icvar) .eq. 'c') then
298     cbuffindex = nWetcGlobal(k)
299     else if (ncvargrd(icvar) .eq. 's') then
300     cbuffindex = nWetsGlobal(k)
301     else if (ncvargrd(icvar) .eq. 'w') then
302     cbuffindex = nWetwGlobal(k)
303     else if (ncvargrd(icvar) .eq. 'v') then
304     cbuffindex = nWetvGlobal(k)
305     #ifdef ALLOW_SHIFWFLX_CONTROL
306     else if (ncvargrd(icvar) .eq. 'i') then
307     cbuffindex = nWetiGlobal(k)
308     #endif
309     cgg( O.B. points have the grid mask "m".
310     else if (ncvargrd(icvar) .eq. 'm') then
311     cgg From "icvrec", calculate what iobcs must be.
312     gg = (icvrec-1)/nobcs
313     igg = int(gg)
314     iobcs= icvrec - igg*nobcs
315     #ifdef ALLOW_OBCSN_CONTROL
316     if (icvar .eq. 11) then
317     cbuffindex = nWetobcsnGlo(k,iobcs)
318     endif
319     #endif
320     #ifdef ALLOW_OBCSS_CONTROL
321     if (icvar .eq. 12) then
322     cbuffindex = nWetobcssGlo(k,iobcs)
323     endif
324     #endif
325     #ifdef ALLOW_OBCSW_CONTROL
326     if (icvar .eq. 13) then
327     cbuffindex = nWetobcswGlo(k,iobcs)
328     endif
329     #endif
330     #ifdef ALLOW_OBCSE_CONTROL
331     if (icvar .eq. 14) then
332     cbuffindex = nWetobcseGlo(k,iobcs)
333     endif
334     #endif
335     cgg)
336     endif
337     if ( icvoffset + cbuffindex .gt. nvarlength ) then
338     print*
339     print *, ' ERROR:'
340     print *, ' There are at least ', icvoffset+cbuffindex,
341     & ' records in '//fname(1:28)//'.'
342     print *, ' This is more than expected from nvarlength =',
343     & nvarlength, '.'
344     print *, ' Something is wrong in the computation of '//
345     & 'the wet points or'
346     print *, ' in computing the number of records in '//
347     & 'some variable(s).'
348     print *, ' ... stopped in OPTIM_READDATA.'
349     stop ' ... stopped in OPTIM_READDATA.'
350     endif
351     if (cbuffindex .gt. 0) then
352     read( funit ) cbuffindex
353     read( funit ) filek
354     read( funit ) (cbuff(ii), ii=1,cbuffindex)
355     do icvcomp = 1,cbuffindex
356     vv(icvoffset+icvcomp) = cbuff(icvcomp)
357     c If you want to optimize with respect to just O.B. T and S
358     c uncomment the next two lines.
359     c if (iobcs .eq. 3) vv(icvoffset+icvcomp)=0.
360     c if (iobcs .eq. 4) vv(icvoffset+icvcomp)=0.
361     enddo
362     icvoffset = icvoffset + cbuffindex
363     endif
364     enddo
365     enddo
366     enddo
367     enddo
368     endif
369     enddo
370    
371     else
372    
373     c-- Assign the number of control variables.
374     nn = nvarlength
375    
376     endif
377    
378     close( funit )
379    
380     c-- Assign the cost function value in case we read the cost file.
381    
382     if ( dfile .eq. ctrlname ) then
383     ff = 0. d 0
384     else if ( dfile .eq. costname ) then
385     ff = fileff
386     endif
387     c-- Always return the cost function value if lheaderonly
388     if ( lheaderonly) ff = fileff
389    
390     return
391     end

  ViewVC Help
Powered by ViewVC 1.1.22