/[MITgcm]/MITgcm_contrib/mlosch/optim_m1qn3/run_optim.sh
ViewVC logotype

Annotation of /MITgcm_contrib/mlosch/optim_m1qn3/run_optim.sh

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


Revision 1.1 - (hide annotations) (download) (as text)
Wed May 9 09:46:25 2018 UTC (5 years, 11 months ago) by mlosch
Branch: MAIN
File MIME type: application/x-sh
- add some basic instructions to compile optim_m1qn3/optim.x and
  run an optimization based on tutorial_global_oce_optim
- add a python script that plots the cost function values as a function
  of m1qn3 iteration (not simulation!)

1 mlosch 1.1 #!/bin/bash
2     # these are some basic instructions to an optimization of
3     # MITgcm/verification/tutorial_global_oce_optim with
4     # MITgcm_offline/mlosch/optim_m1qn3
5     # Some tweaking is definitely possible and not described here.
6     #
7     #
8     cd MITgcm/verification
9     # this is just to compile and run the model for testing.
10     # I use TAF (just because I have it and OpenAD is a pain to compile on a Mac),
11     # but that should not make any difference
12     ./testreport -t tutorial_global_oce_optim -adm -j 4 -ncad
13     # this is the result:
14     # G D M C A F
15     # e p a R o d D
16     # n n k u s G G
17     # 2 d e n t r r
18    
19     # Y Y Y Y 15>16< 7 pass tutorial_global_oce_optim (e=0, w=2)
20    
21     # here is the cost function value that I get
22     # (PID.TID 0000.0001) local fc = 0.620023228182336D+01
23     # (PID.TID 0000.0001) global fc = 0.620023228182336D+01
24     # now I download and compile optim_m1qn3
25     cd ../../
26     cvs co MITgcm_contrib/mlosch/optim_m1qn3
27     cd MITgcm_contrib/mlosch/optim_m1qn3
28     # edit Makefile to adjust to your platform. For me this involves choosing
29     # the correct CPP command and setting SUFF=for (because MacOS is
30     # case-insensitive in my case)
31     make depend
32     make
33     # then I get optim.x
34     cd ../../../MITgcm/verification/tutorial_global_oce_optim/run
35     cp ../../../../MITgcm_contrib/mlosch/optim_m1qn3/optim.x .
36     # - turn off the gradient check (in data.pkg: useGrdchk = .FALSE.)
37     # - tweak the namelist files data.ctrl and data.optim to the compiler needs
38     # (I need a "/" to terminate a namelist)
39     # - I would replace fmin with dfminFrac = 0.1 (expected reduction of 10%)
40     # in data.optim&OPTIM to be independent of the absolute value of the
41     # cost function. When you run ./mitgcmuv_ad with this you need to comment
42     # out dfminFrac, because mitgcmuv_ad does not know about this namelist
43     # parameter (something to be fixed)
44     # - set numiter=100, nfunc=10, or some other large value. nfunc*numiter is
45     # the number of simulations that are allowed in total. This number should be
46     # much larger than numiter, because you may need more than one function call
47     # (= run of mitgcmuv_ad) per iteration, see m1qn3 docs for details
48     # - add an empty namelist &M1QN3 to data.optim
49     # &M1QN3
50     # /
51     ./optim.x > opt0.txt
52     # I get a lot of output in opt0.txt, which is easier to read with
53     # less -S opt0.out (to truncate long lines)
54     # essentially I have the same value for the cost function
55    
56     # ==================================================
57     # Large Scale Optimization with off-line capability.
58     # ==================================================
59    
60     # OPTIM_READPARMS: Control options have been read.
61     # OPTIM_READPARMS: Minimization options have been read.
62    
63     # OPTIM_READDATA: Reading cost function and gradient of cost function
64     # for optimization cycle: 0
65    
66     # OPTIM_READDATA: opened file ecco_cost_MIT_CE_000.opt0000
67     # OPTIM_READDATA: nvartype 1
68     # OPTIM_READDATA: nvarlength 2315
69     # OPTIM_READDATA: yctrlid MIT_CE_000
70     # OPTIM_READDATA: filenopt 0
71     # OPTIM_READDATA: fileff 6.2002322818233591
72     # OPTIM_READDATA: fileiG 1
73     # OPTIM_READDATA: filejG 1
74     # OPTIM_READDATA: filensx 2
75     # OPTIM_READDATA: filensy 2
76     # [...]
77     # OPTIM_READDATA: end of optim_readdata
78    
79    
80     # OPTIM_READPARMS: Iteration number = 0
81     # number of control variables = 2315
82     # cost function value in ecco_ctrl = 6.2002322818233591
83     # expected cost function minimum = 5.5802090536410232
84     # expected cost function decrease = 0.62002322818233591
85     # Data will be read from the following file: ecco_ctrl_MIT_CE_000.opt0000
86    
87     # OPTIM_SUB: Calling m1qn3_optim for iteration: 0
88     # OPTIM_SUB: with nn, REAL_BYTE = 2315 4
89     # OPTIM_SUB: read model state
90    
91     # OPTIM_READDATA: Reading control vector
92     # for optimization cycle: 0
93    
94     # OPTIM_READDATA: opened file ecco_ctrl_MIT_CE_000.opt0000
95     # OPTIM_READDATA: nvartype 1
96     # OPTIM_READDATA: nvarlength 2315
97     # OPTIM_READDATA: yctrlid MIT_CE_000
98     # OPTIM_READDATA: filenopt 0
99     # OPTIM_READDATA: fileff 6.2002322818233591
100     # OPTIM_READDATA: fileiG 1
101     # OPTIM_READDATA: filejG 1
102     # OPTIM_READDATA: filensx 2
103     # OPTIM_READDATA: filensy 2
104     # OPTIM_READDATA: end of optim_readdata
105    
106    
107     # OPTIM_READDATA: Reading cost function and gradient of cost function
108     # for optimization cycle: 0
109    
110     # OPTIM_READDATA: opened file ecco_cost_MIT_CE_000.opt0000
111     # OPTIM_READDATA: nvartype 1
112     # OPTIM_READDATA: nvarlength 2315
113     # OPTIM_READDATA: yctrlid MIT_CE_000
114     # OPTIM_READDATA: filenopt 0
115     # OPTIM_READDATA: fileff 6.2002322818233591
116     # OPTIM_READDATA: fileiG 1
117     # OPTIM_READDATA: filejG 1
118     # OPTIM_READDATA: filensx 2
119     # OPTIM_READDATA: filensy 2
120     # OPTIM_READDATA: end of optim_readdata
121    
122     # OPTIM_SUB after reading ecco_ctrl and ecco_cost:
123     # OPTIM_SUB nn = 2315
124     # OPTIM_SUB objf = 6.2002322818233591
125     # OPTIM_SUB xx(1) = 0.0000000000000000
126     # OPTIM_SUB adxx(1) = -6.7879882408306003E-005
127     # OPTIM_SUB: cold start, optimcycle = 0
128     # OPTIM_SUB: call m1qn3_offline ........
129     # OPTIM_SUB: ...........................
130     # OPTIM_SUB: returned from m1qn3_offline
131     # OPTIM_SUB: nn = 2315
132     # OPTIM_SUB: xx(1) = 0.51934864251896229 0.73000729032300782
133     # OPTIM_SUB: adxx(1) = -6.7879882408306003E-005 -9.5413379312958568E-005
134     # OPTIM_SUB: omode = -1
135     # OPTIM_SUB: niter = 1
136     # OPTIM_SUB: nsim = 10000
137     # OPTIM_SUB: reverse = 1
138    
139     # OPTIM_SUB: mean(xx) = 0.16365068483545642
140     # OPTIM_SUB: max(xx) = 4.6525355815045790
141     # OPTIM_SUB: min(xx) = -9.3326211896764324
142     # OPTIM_SUB: std(xx) = 15.613450260548481
143    
144    
145     # OPTIM_STORE_M1QN3: saving the state of m1qn3 in OPWARM.opt0001
146    
147     # OPTIM_SUB: writing 2315 sized control to file ecco_ctrl
148    
149     # OPTIM_WRITEDATA: Writing new control vector to file(s)
150     # for optimization cycle: 1
151    
152     # OPTIM_WRITEDATA: nvartype 1
153     # OPTIM_WRITEDATA: nvarlength 2315
154     # OPTIM_WRITEDATA: yctrlid MIT_CE_000
155     # OPTIM_WRITEDATA: nopt 1
156     # OPTIM_WRITEDATA: ff -9999.0000000000000
157     # OPTIM_WRITEDATA: iG 1
158     # OPTIM_WRITEDATA: jG 1
159     # OPTIM_WRITEDATA: nsx 2
160     # OPTIM_WRITEDATA: nsy 2
161     # OPTIM_WRITEDATA: end of optim_writedata, icvoffset 2315
162    
163    
164     # ======================================
165     # Large Scale Optimization run finished.
166     # ======================================
167    
168     # ff = -9999 is an intentional dummy value.
169    
170     # now you can organize the rest in a loop. In bash, it could look like this:
171     # first comment out dfminFrac, because mitgcmuv_ad does not know about this,
172     # dfminFrac is really only needed in the zeroth iteration
173     #
174     # tabula rasa
175     rm ecco_c* OPWARM.* m1qn3_output.txt
176     myiter=0
177     cat > data.optim <<EOF #
178     # ********************************
179     # Off-line optimization parameters
180     # ********************************
181     &OPTIM
182     optimcycle=${myiter},
183     numiter=100,
184     nfunc=100,
185     #dfminFrac = 0.1,
186     iprint=10,
187     nupdate=8,
188     /
189    
190     &M1QN3
191     /
192     EOF
193    
194     while (( $myiter < 20 ))
195     do
196     # formatter iteration count
197     it=`echo $myiter | awk '{printf "%03i",$1}'`
198     echo "iteration ${myiter}"
199     # comment out dfminFrac from data.optim
200     sed -i '' 's/.*dfminFrac.*/#dfminFrac = 0.1,/' data.optim
201     # increment counter in data.optim
202     sed -i .${it} "s/.*optimcycle.*/ optimcycle=${myiter},/" data.optim
203     ./mitgcmuv_ad > output${it}.txt
204     # add dfminFrac = 0.1,
205     sed -i '' 's/#dfminFrac.*/ dfminFrac = 0.1,/' data.optim
206     ./optim.x > opt${it}.txt
207     # increase counter for next iteration
208     ((myiter++))
209     done
210    
211     # grep "fc " output001.txt
212     # (PID.TID 0000.0001) early fc = 0.000000000000000D+00
213     # (PID.TID 0000.0001) local fc = 0.132325873958879D+02
214     # (PID.TID 0000.0001) global fc = 0.132325873958879D+02
215    
216     # grep "global fc " output???.txt
217     # output000.txt:(PID.TID 0000.0001) global fc = 0.620023228182336D+01
218     # output001.txt:(PID.TID 0000.0001) global fc = 0.132325873958879D+02
219     # output002.txt:(PID.TID 0000.0001) global fc = 0.615567811433600D+01
220     # output003.txt:(PID.TID 0000.0001) global fc = 0.615556878869932D+01
221     # output004.txt:(PID.TID 0000.0001) global fc = 0.615547009131471D+01
222    
223     # as you can see, the improvement is not very good after the initial steps.
224     # and the optimization will not be successful in the end, i.e. satisfy
225     # the tolerance set by epsg.
226     # This is likely because the number of timesteps in data is small (20)
227     # for this test. Try longer intergations (e.g. 1 year) and wait longer
228    
229     # check out m1qn3_output.txt, which records the output of m1qn3
230     # the python script plotfc.py greps the cost function values out of
231     # m1qn3_ouput.txt and plots them and the number of simulations per iteration
232     # which typically increase with decreasing cost function
233    
234    

  ViewVC Help
Powered by ViewVC 1.1.22