1 | #!/usr/bin/ksh |
---|
2 | |
---|
3 | # kpp4palm - script for creating gasphase module |
---|
4 | |
---|
5 | #------------------------------------------------------------------------------# |
---|
6 | # This file is part of the PALM model system. |
---|
7 | # |
---|
8 | # PALM is free software: you can redistribute it and/or modify it under the terms |
---|
9 | # of the GNU General Public License as published by the Free Software Foundation, |
---|
10 | # either version 3 of the License, or (at your option) any later version. |
---|
11 | # |
---|
12 | # PALM is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
13 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
---|
14 | # A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License along with |
---|
17 | # PALM. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # |
---|
19 | # Copyright 2017-2018 Klaus Ketelsen and MPI-CH (April 2007) |
---|
20 | # Copyright 2017-2018 Karlsruhe Institute of Technology |
---|
21 | # Copyright 2017-2018 Leibniz Universitaet Hannover |
---|
22 | #------------------------------------------------------------------------------# |
---|
23 | # Nov. 2016: Initial Version of KPP chemistry convertor adapted for PALM |
---|
24 | # by Klaus Ketelsen |
---|
25 | # |
---|
26 | # This code is a modified version of KP4 (Jöckel, P., Kerkweg, A., Pozzer, A., |
---|
27 | # Sander, R., Tost, H., Riede, H., Baumgaertner, A., Gromov, S., and Kern, B., |
---|
28 | # 2010: Development cycle 2 of the Modular Earth Submodel System (MESSy2), |
---|
29 | # Geosci. Model Dev., 3, 717-752, https://doi.org/10.5194/gmd-3-717-2010). |
---|
30 | # KP4 is part of the Modular Earth Submodel System (MESSy), which is is |
---|
31 | # available under the GNU General Public License (GPL). |
---|
32 | # |
---|
33 | #------------------------------------------------------------------------------# |
---|
34 | # |
---|
35 | # Current revisions: |
---|
36 | # ------------------ |
---|
37 | # |
---|
38 | # |
---|
39 | # Former revisions: |
---|
40 | # ----------------- |
---|
41 | # $Id: kpp4palm.ksh 3780 2019-03-05 11:19:45Z eckhard $ |
---|
42 | # forkel 05. March 2019: cs_mech and set_cm |
---|
43 | # forkel 30. Oktober 2018: Integrating contents of kp4_compress into chem_gasphase_mod.f90 |
---|
44 | # forkel 25. September 2018: Added cat for $MECH to pass mechanism name to kpp4palm |
---|
45 | # ketelsen 18. September 2018: Added cat for '#INLINE F90_GLOBAL' |
---|
46 | # (moved here from mechanisms/def_MECH/chem_gasphase.kpp |
---|
47 | # |
---|
48 | # forkel: 14. September 2018: WCOPY removed |
---|
49 | # ketelsen: July 2018: Adaptations for vektor mode |
---|
50 | # forkel June 2018: re-established original case of subroutine names |
---|
51 | # forkel May 2018: additional copying of chem_gasphase_mod.f90 into $DEFDIR |
---|
52 | # forkel 20.04.2018: removed wlamch and wlamch_add from $KPP_SUBROUTINE_LIST |
---|
53 | # (epsilon(one) is used now) |
---|
54 | # forkel March 2017 |
---|
55 | # Re-introduced relative path for KPP_HOME |
---|
56 | # Subroutine list adapted to lowercase subroutine names |
---|
57 | # Added arr2, removed update_sun and k_3rd from subroutine list |
---|
58 | # Renamed output file to chem_gasphase_mod |
---|
59 | # Renamed this file from kp4/ksh to kpp4kpp.ksh |
---|
60 | # changed location of def_mechanism directories to gasphase_preproc/mechanisms |
---|
61 | # |
---|
62 | # |
---|
63 | # 2718 2018-01-02 08:49:38Z maronga |
---|
64 | # Initial revision |
---|
65 | # |
---|
66 | # |
---|
67 | ########################################################################## |
---|
68 | # |
---|
69 | # |
---|
70 | # Other notes: |
---|
71 | # ------------# |
---|
72 | # Re-introduced relative path for KPP_HOME |
---|
73 | # Subroutine list adapted to lowercase subroutine names |
---|
74 | # Added arr2, removed update_sun and k_3rd from subroutine list |
---|
75 | # Renamed output file to chem_gasphase_mod |
---|
76 | # Renamed this fikle from kp4/ksh to kpp4kpp.ksh |
---|
77 | # changed location of def_mechanism directories to GASPHASE_PREPROC/mechanisms |
---|
78 | # |
---|
79 | # Nov. 2016: Initial Version of KPP chemistry convertor by Klaus Ketelsen |
---|
80 | # |
---|
81 | # |
---|
82 | |
---|
83 | set -eu |
---|
84 | |
---|
85 | |
---|
86 | ########################### User SetUp #################################### |
---|
87 | |
---|
88 | export KPP_HOME=`pwd`/kpp |
---|
89 | export KPP=$KPP_HOME/bin/kpp |
---|
90 | |
---|
91 | BASE=`pwd`/kpp4palm |
---|
92 | |
---|
93 | ########################## End User Setup ################################ |
---|
94 | |
---|
95 | WORK=tmp_kpp4palm |
---|
96 | |
---|
97 | # Default |
---|
98 | |
---|
99 | MECH=smog |
---|
100 | OUTDIR=`pwd`/../../../SOURCE |
---|
101 | OUTFILE=chem_gasphase_mod |
---|
102 | DEFDIR=`pwd`/mechanisms/def_smog |
---|
103 | PREFIX=chem_gasphase_mod |
---|
104 | MODE="scalar" |
---|
105 | VLEN=1 |
---|
106 | KEEP="NO" |
---|
107 | UPDT="NO" |
---|
108 | DE_INDEX=0 |
---|
109 | DE_INDEX_FAST="YES" |
---|
110 | |
---|
111 | export KPP_SOLVER=Rosenbrock |
---|
112 | |
---|
113 | # get Command line option |
---|
114 | |
---|
115 | while getopts :m:i:fkup:o:s:vl:w: c # get options |
---|
116 | do case $c in |
---|
117 | m) MECH=$OPTARG;; # mechanism |
---|
118 | |
---|
119 | i) DE_INDEX=$OPTARG;; # if set, deindexing |
---|
120 | |
---|
121 | f) DE_INDEX_FAST="YES";; # if set, fast deindexing |
---|
122 | |
---|
123 | k) KEEP="YES";; # keep Working directory |
---|
124 | |
---|
125 | o) OUTDIR=$OPTARG;; # Output directory of Generated Code = |
---|
126 | # SOURCE directory (do not change) |
---|
127 | |
---|
128 | p) PREFIX=$OPTARG;; # Name Prefix (chem_gasphase_mod, do not change) |
---|
129 | |
---|
130 | s) KPP_SOLVER=$OPTARG;; # Chosen solver (only Rosebrock solvers work for vector mode) |
---|
131 | |
---|
132 | u) UPDT="YES";; # update mechanisms/def_$MECH/chem_gasphase_mod.f90 |
---|
133 | |
---|
134 | v) MODE="vector";; # Set to vector Mode |
---|
135 | |
---|
136 | l) VLEN=$OPTARG;; # Set vector length |
---|
137 | |
---|
138 | w) WORK=$OPTARG;; # Working directory |
---|
139 | |
---|
140 | \?) print ${0##*/} "unknown option:" $OPTARG |
---|
141 | print "USAGE: ${0##*/} [ -m dir -e -k -u -o dir -p name -s solver -v -l length -w dir ] " |
---|
142 | exit 1;; |
---|
143 | esac |
---|
144 | done |
---|
145 | shift OPTIND-1 |
---|
146 | |
---|
147 | echo MECHANISM = $MECH |
---|
148 | echo DE_INDEX = $DE_INDEX |
---|
149 | echo KEEP = $KEEP |
---|
150 | echo UPDT = $UPDT |
---|
151 | echo MODE = $MODE |
---|
152 | echo VLEN = $VLEN |
---|
153 | |
---|
154 | DEF_PREFIX=${PREFIX}.kpp |
---|
155 | DEFDIR=`pwd`/mechanisms/def_$MECH |
---|
156 | echo DEFDIR = $DEFDIR |
---|
157 | |
---|
158 | # Create or clean working directory |
---|
159 | |
---|
160 | MY_PWD=`pwd` |
---|
161 | mkdir -p $WORK |
---|
162 | rm -rf $WORK/* |
---|
163 | cd $WORK |
---|
164 | |
---|
165 | # kpp dependend, may be changed |
---|
166 | |
---|
167 | KPP_FILE_LIST="Initialize Integrator LinearAlgebra Jacobian Function Rates Util" |
---|
168 | if [[ $MODE = "vector" ]] |
---|
169 | then |
---|
170 | KPP_FILE_LIST="$KPP_FILE_LIST kp4_compress_subroutines" |
---|
171 | fi |
---|
172 | |
---|
173 | KPP_SUBROUTINE_LIST="Initialize" |
---|
174 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST INTEGRATE Fun" |
---|
175 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST KppSolve KppDecomp" |
---|
176 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Jac_SP k_arr " |
---|
177 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Update_RCONST ARR2" |
---|
178 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST initialize_kpp_ctrl error_output" |
---|
179 | |
---|
180 | # if [[ $MODE = "vector" && $KPP_SOLVER = "ROS2" ]] |
---|
181 | # then |
---|
182 | # cp $BASE/templates/${KPP_SOLVER}_vec.f90 ${KPP_SOLVER}.f90 # get vector Solver |
---|
183 | # else |
---|
184 | # # KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST FunTemplate JacTemplate Update_SUN " |
---|
185 | # KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
186 | # if [[ $MODE = "vector" ]] |
---|
187 | # then |
---|
188 | # cp $BASE/templates/${KPP_SOLVER}_vec.f90 ${KPP_SOLVER}.f90 # get vector Solver |
---|
189 | # else |
---|
190 | # KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Rosenbrock FunTemplate JacTemplate Update_SUN" |
---|
191 | # fi |
---|
192 | # fi |
---|
193 | if [[ $MODE = "vector" ]] |
---|
194 | then |
---|
195 | # get vector Solver |
---|
196 | cp $BASE/templates/${KPP_SOLVER}_vec.f90 ${KPP_SOLVER}.f90 |
---|
197 | cp $BASE/templates/kp4_compress_header ${PREFIX}_kp4_compress_header.f90 |
---|
198 | cp $BASE/templates/kp4_compress_subroutines ${PREFIX}_kp4_compress_subroutines.f90 |
---|
199 | fi |
---|
200 | |
---|
201 | # Interface ignore list |
---|
202 | KPP_INTERFACE_IGNORE=" " |
---|
203 | |
---|
204 | echo " " |
---|
205 | echo KPP_SOLVER $KPP_SOLVER |
---|
206 | echo " " |
---|
207 | |
---|
208 | case $KPP_SOLVER in |
---|
209 | ROS2) ;; |
---|
210 | |
---|
211 | Rosenbrock) |
---|
212 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WSCAL WAXPY" |
---|
213 | if [[ $MODE != "vector" ]] |
---|
214 | then |
---|
215 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Rosenbrock FunTemplate JacTemplate" |
---|
216 | KPP_INTERFACE_IGNORE="WAXPY" |
---|
217 | |
---|
218 | else |
---|
219 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST FunTemplate JacTemplate" |
---|
220 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST kco_initialize kco_compress kco_finalize" |
---|
221 | fi;; |
---|
222 | |
---|
223 | rosenbrock_mz) |
---|
224 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
225 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Rosenbrock FunTemplate JacTemplate Update_SUN";; |
---|
226 | |
---|
227 | rosenbrock) |
---|
228 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
229 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Rosenbrock FunTemplate JacTemplate";; |
---|
230 | |
---|
231 | kpp_lsode) |
---|
232 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
233 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST KppLsode DLSODE JAC_CHEM FUN_CHEM" |
---|
234 | KPP_INTERFACE_IGNORE="$KPP_INTERFACE_IGNORE JAC_CHEM KppDecomp KppSolve";; |
---|
235 | |
---|
236 | kpp_radau5) |
---|
237 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY FUN_CHEM JAC_CHEM SET2ZERO" |
---|
238 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST RADAU5 Update_SUN" |
---|
239 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST KppSolveCmplx KppDecompCmplx";; |
---|
240 | |
---|
241 | kpp_sdirk) |
---|
242 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
243 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST SDIRK JAC_CHEM SET2ZERO FUN_CHEM" |
---|
244 | KPP_INTERFACE_IGNORE="$KPP_INTERFACE_IGNORE Set2zero SET2ZERO FUN_CHEM";; |
---|
245 | |
---|
246 | kpp_seulex) |
---|
247 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
248 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST ATMSEULEX" |
---|
249 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST SEULEX_ErrorMsg SEULEX_Integrator FUN_CHEM JAC_CHEM SEUL" |
---|
250 | KPP_INTERFACE_IGNORE="$KPP_INTERFACE_IGNORE SEULEX_Integrator SDIRK FUN_CHEM SEUL";; |
---|
251 | |
---|
252 | \?) print "SORRY ONLY ROSENBROCK METHODS WORK AT THE MOMENT:" $KPP_SOLVER |
---|
253 | exit 1;; |
---|
254 | esac |
---|
255 | #mz-ak-20070509+ |
---|
256 | |
---|
257 | KPP_INCLUDE_LIST="Parameters Global JacobianSP Monitor" |
---|
258 | if [[ $MODE = "vector" ]] |
---|
259 | then |
---|
260 | KPP_INCLUDE_LIST="$KPP_INCLUDE_LIST kp4_compress_header" |
---|
261 | fi |
---|
262 | |
---|
263 | #Get definition Files |
---|
264 | |
---|
265 | cp $DEFDIR/*.eqn . |
---|
266 | cp $DEFDIR/*.spc . |
---|
267 | cp $DEFDIR/${PREFIX}.kpp . |
---|
268 | |
---|
269 | # Global variable are defined here |
---|
270 | # This has the advantage that it is not necessary to include these variables in all .kpp definition files |
---|
271 | |
---|
272 | cat >> ${PREFIX}.kpp << EOF |
---|
273 | #INLINE F90_GLOBAL |
---|
274 | ! QVAP - Water vapor |
---|
275 | REAL(kind=dp) :: QVAP |
---|
276 | ! FAKT - Conversion factor |
---|
277 | REAL(kind=dp) :: FAKT |
---|
278 | |
---|
279 | ! CS_MECH for check of mechanism name with namelist |
---|
280 | CHARACTER(LEN=30) :: CS_MECH |
---|
281 | #ENDINLINE |
---|
282 | EOF |
---|
283 | |
---|
284 | # Store mechanism name in file mech_list |
---|
285 | cat >> mech_list << EOF |
---|
286 | ! Mechanism: $MECH |
---|
287 | ! |
---|
288 | EOF |
---|
289 | |
---|
290 | # Store mechanism name for cs_mech |
---|
291 | cat >> set_cm << EOF |
---|
292 | |
---|
293 | ! Set cs_mech for check with mechanism name from namelist |
---|
294 | cs_mech = '$MECH' |
---|
295 | EOF |
---|
296 | |
---|
297 | # Run kpp |
---|
298 | |
---|
299 | $KPP $DEF_PREFIX |
---|
300 | |
---|
301 | # Get templates for C++ program |
---|
302 | |
---|
303 | cp $BASE/templates/module_header* . # Use fixed Module_header |
---|
304 | cp $BASE/templates/initialize_kpp_ctrl_template.f90 . # CTRL kpp time stepping |
---|
305 | |
---|
306 | # file with subroutine list for c++ program create_kpp_module |
---|
307 | |
---|
308 | for i in $KPP_FILE_LIST |
---|
309 | do |
---|
310 | echo ${PREFIX}_${i} >> file_list |
---|
311 | done |
---|
312 | echo initialize_kpp_ctrl_template >> file_list |
---|
313 | |
---|
314 | # file with subroutine list for c++ program create_kpp_module |
---|
315 | |
---|
316 | for i in $KPP_SUBROUTINE_LIST |
---|
317 | do |
---|
318 | echo $i >> subroutine_list |
---|
319 | done |
---|
320 | |
---|
321 | # file with include list for c++ program create_kpp_module |
---|
322 | |
---|
323 | for i in $KPP_INCLUDE_LIST |
---|
324 | do |
---|
325 | echo ${PREFIX}_${i} >> include_list |
---|
326 | done |
---|
327 | |
---|
328 | touch interface_ignore_list |
---|
329 | for i in $KPP_INTERFACE_IGNORE |
---|
330 | do |
---|
331 | echo $i >> interface_ignore_list |
---|
332 | done |
---|
333 | |
---|
334 | echo start kpp4palm.exe with arguments |
---|
335 | echo $PREFIX $MODE $VLEN $DE_INDEX $DE_INDEX_FAST |
---|
336 | |
---|
337 | $BASE/bin/kpp4palm.exe $PREFIX $MODE $VLEN $DE_INDEX $DE_INDEX_FAST |
---|
338 | |
---|
339 | #Prelimanary, substitution has to be moved into kpp4palm.exe |
---|
340 | if [[ $MODE = "vector" ]] |
---|
341 | then |
---|
342 | sed -i -e 's/phot(nphot/phot(vl_dim,nphot/g' kk_kpp.f90 |
---|
343 | fi |
---|
344 | |
---|
345 | if [[ -e $OUTDIR/${OUTFILE}.f90 ]] |
---|
346 | then |
---|
347 | mv $OUTDIR/${OUTFILE}.f90 $OUTDIR/${OUTFILE}.f90.sav |
---|
348 | fi |
---|
349 | cp -p kk_kpp.f90 $OUTDIR/${OUTFILE}.f90 |
---|
350 | echo " " |
---|
351 | echo "Write kpp module -- > " $OUTDIR/${OUTFILE}.f90 |
---|
352 | |
---|
353 | if [[ $UPDT = "YES" ]] |
---|
354 | then |
---|
355 | cp -p kk_kpp.f90 $DEFDIR/${OUTFILE}.f90 |
---|
356 | echo " " |
---|
357 | echo "Write kpp module -- > " $DEFDIR/${OUTFILE}.f90 |
---|
358 | fi |
---|
359 | |
---|
360 | if [[ $KEEP = "NO" ]] |
---|
361 | then |
---|
362 | cd $MY_PWD |
---|
363 | rm -rf $WORK |
---|
364 | fi |
---|
365 | exit |
---|
366 | |
---|