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 3458 2018-10-30 14:51:23Z suehring $ |
---|
42 | # forkel 30. Oktober 2018: Integrating contents of kp4_compress into chem_gasphase_mod.f90 |
---|
43 | # forkel 25. September 2018: Added cat for $MECH to pass mechanism name to kpp4palm |
---|
44 | # ketelsen 18. September 2018: Added cat for '#INLINE F90_GLOBAL' |
---|
45 | # (moved here from mechanisms/def_MECH/chem_gasphase.kpp |
---|
46 | # |
---|
47 | # forkel: 14. September 2018: WCOPY removed |
---|
48 | # ketelsen: July 2018: Adaptations for vektor mode |
---|
49 | # forkel June 2018: re-established original case of subroutine names |
---|
50 | # forkel May 2018: additional copying of chem_gasphase_mod.f90 into $DEFDIR |
---|
51 | # forkel 20.04.2018: removed wlamch and wlamch_add from $KPP_SUBROUTINE_LIST |
---|
52 | # (epsilon(one) is used now) |
---|
53 | # forkel March 2017 |
---|
54 | # Re-introduced relative path for KPP_HOME |
---|
55 | # Subroutine list adapted to lowercase subroutine names |
---|
56 | # Added arr2, removed update_sun and k_3rd from subroutine list |
---|
57 | # Renamed output file to chem_gasphase_mod |
---|
58 | # Renamed this file from kp4/ksh to kpp4kpp.ksh |
---|
59 | # changed location of def_mechanism directories to gasphase_preproc/mechanisms |
---|
60 | # |
---|
61 | # |
---|
62 | # 2718 2018-01-02 08:49:38Z maronga |
---|
63 | # Initial revision |
---|
64 | # |
---|
65 | # |
---|
66 | ########################################################################## |
---|
67 | # |
---|
68 | # |
---|
69 | # Other notes: |
---|
70 | # ------------# |
---|
71 | # Re-introduced relative path for KPP_HOME |
---|
72 | # Subroutine list adapted to lowercase subroutine names |
---|
73 | # Added arr2, removed update_sun and k_3rd from subroutine list |
---|
74 | # Renamed output file to chem_gasphase_mod |
---|
75 | # Renamed this fikle from kp4/ksh to kpp4kpp.ksh |
---|
76 | # changed location of def_mechanism directories to GASPHASE_PREPROC/mechanisms |
---|
77 | # |
---|
78 | # Nov. 2016: Initial Version of KPP chemistry convertor by Klaus Ketelsen |
---|
79 | # |
---|
80 | # |
---|
81 | |
---|
82 | set -eu |
---|
83 | |
---|
84 | |
---|
85 | ########################### User SetUp #################################### |
---|
86 | |
---|
87 | export KPP_HOME=`pwd`/kpp |
---|
88 | export KPP=$KPP_HOME/bin/kpp |
---|
89 | |
---|
90 | BASE=`pwd`/kpp4palm |
---|
91 | |
---|
92 | ########################## End User Setup ################################ |
---|
93 | |
---|
94 | WORK=tmp_kpp4palm |
---|
95 | |
---|
96 | # Default |
---|
97 | |
---|
98 | MECH=smog |
---|
99 | OUTDIR=`pwd`/../../../SOURCE |
---|
100 | OUTFILE=chem_gasphase_mod |
---|
101 | DEFDIR=`pwd`/mechanisms/def_smog |
---|
102 | PREFIX=chem_gasphase_mod |
---|
103 | MODE="scalar" |
---|
104 | VLEN=1 |
---|
105 | KEEP="NO" |
---|
106 | UPDT="NO" |
---|
107 | DE_INDEX=0 |
---|
108 | DE_INDEX_FAST="YES" |
---|
109 | |
---|
110 | export KPP_SOLVER=Rosenbrock |
---|
111 | |
---|
112 | # get Command line option |
---|
113 | |
---|
114 | while getopts :m:i:fkup:o:s:vl:w: c # get options |
---|
115 | do case $c in |
---|
116 | m) MECH=$OPTARG;; # mechanism |
---|
117 | |
---|
118 | i) DE_INDEX=$OPTARG;; # if set, deindexing |
---|
119 | |
---|
120 | f) DE_INDEX_FAST="YES";; # if set, fast deindexing |
---|
121 | |
---|
122 | k) KEEP="YES";; # keep Working directory |
---|
123 | |
---|
124 | o) OUTDIR=$OPTARG;; # Output directory of Generated Code = |
---|
125 | # SOURCE directory (do not change) |
---|
126 | |
---|
127 | p) PREFIX=$OPTARG;; # Name Prefix (chem_gasphase_mod, do not change) |
---|
128 | |
---|
129 | s) KPP_SOLVER=$OPTARG;; # Chosen solver (only Rosebrock solvers work for vector mode) |
---|
130 | |
---|
131 | u) UPDT="YES";; # update mechanisms/def_$MECH/chem_gasphase_mod.f90 |
---|
132 | |
---|
133 | v) MODE="vector";; # Set to vector Mode |
---|
134 | |
---|
135 | l) VLEN=$OPTARG;; # Set vector length |
---|
136 | |
---|
137 | w) WORK=$OPTARG;; # Working directory |
---|
138 | |
---|
139 | \?) print ${0##*/} "unknown option:" $OPTARG |
---|
140 | print "USAGE: ${0##*/} [ -m dir -e -k -u -o dir -p name -s solver -v -l length -w dir ] " |
---|
141 | exit 1;; |
---|
142 | esac |
---|
143 | done |
---|
144 | shift OPTIND-1 |
---|
145 | |
---|
146 | echo MECHANISM = $MECH |
---|
147 | echo DE_INDEX = $DE_INDEX |
---|
148 | echo KEEP = $KEEP |
---|
149 | echo UPDT = $UPDT |
---|
150 | echo MODE = $MODE |
---|
151 | echo VLEN = $VLEN |
---|
152 | |
---|
153 | DEF_PREFIX=${PREFIX}.kpp |
---|
154 | DEFDIR=`pwd`/mechanisms/def_$MECH |
---|
155 | echo DEFDIR = $DEFDIR |
---|
156 | |
---|
157 | # Create or clean working directory |
---|
158 | |
---|
159 | MY_PWD=`pwd` |
---|
160 | mkdir -p $WORK |
---|
161 | rm -rf $WORK/* |
---|
162 | cd $WORK |
---|
163 | |
---|
164 | # kpp dependend, may be changed |
---|
165 | |
---|
166 | KPP_FILE_LIST="Initialize Integrator LinearAlgebra Jacobian Function Rates Util" |
---|
167 | if [[ $MODE = "vector" ]] |
---|
168 | then |
---|
169 | KPP_FILE_LIST="$KPP_FILE_LIST kp4_compress_subroutines" |
---|
170 | fi |
---|
171 | |
---|
172 | KPP_SUBROUTINE_LIST="Initialize" |
---|
173 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST INTEGRATE Fun" |
---|
174 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST KppSolve KppDecomp" |
---|
175 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Jac_SP k_arr " |
---|
176 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Update_RCONST ARR2" |
---|
177 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST initialize_kpp_ctrl error_output" |
---|
178 | |
---|
179 | # if [[ $MODE = "vector" && $KPP_SOLVER = "ROS2" ]] |
---|
180 | # then |
---|
181 | # cp $BASE/templates/${KPP_SOLVER}_vec.f90 ${KPP_SOLVER}.f90 # get vector Solver |
---|
182 | # else |
---|
183 | # # KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST FunTemplate JacTemplate Update_SUN " |
---|
184 | # KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
185 | # if [[ $MODE = "vector" ]] |
---|
186 | # then |
---|
187 | # cp $BASE/templates/${KPP_SOLVER}_vec.f90 ${KPP_SOLVER}.f90 # get vector Solver |
---|
188 | # else |
---|
189 | # KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Rosenbrock FunTemplate JacTemplate Update_SUN" |
---|
190 | # fi |
---|
191 | # fi |
---|
192 | if [[ $MODE = "vector" ]] |
---|
193 | then |
---|
194 | # get vector Solver |
---|
195 | cp $BASE/templates/${KPP_SOLVER}_vec.f90 ${KPP_SOLVER}.f90 |
---|
196 | cp $BASE/templates/kp4_compress_header ${PREFIX}_kp4_compress_header.f90 |
---|
197 | cp $BASE/templates/kp4_compress_subroutines ${PREFIX}_kp4_compress_subroutines.f90 |
---|
198 | fi |
---|
199 | |
---|
200 | # Interface ignore list |
---|
201 | KPP_INTERFACE_IGNORE=" " |
---|
202 | |
---|
203 | echo " " |
---|
204 | echo KPP_SOLVER $KPP_SOLVER |
---|
205 | echo " " |
---|
206 | |
---|
207 | case $KPP_SOLVER in |
---|
208 | ROS2) ;; |
---|
209 | |
---|
210 | Rosenbrock) |
---|
211 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WSCAL WAXPY" |
---|
212 | if [[ $MODE != "vector" ]] |
---|
213 | then |
---|
214 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Rosenbrock FunTemplate JacTemplate" |
---|
215 | KPP_INTERFACE_IGNORE="WAXPY" |
---|
216 | |
---|
217 | else |
---|
218 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST FunTemplate JacTemplate" |
---|
219 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST kco_initialize kco_compress kco_finalize" |
---|
220 | fi;; |
---|
221 | |
---|
222 | rosenbrock_mz) |
---|
223 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
224 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Rosenbrock FunTemplate JacTemplate Update_SUN";; |
---|
225 | |
---|
226 | rosenbrock) |
---|
227 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
228 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Rosenbrock FunTemplate JacTemplate";; |
---|
229 | |
---|
230 | kpp_lsode) |
---|
231 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
232 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST KppLsode DLSODE JAC_CHEM FUN_CHEM" |
---|
233 | KPP_INTERFACE_IGNORE="$KPP_INTERFACE_IGNORE JAC_CHEM KppDecomp KppSolve";; |
---|
234 | |
---|
235 | kpp_radau5) |
---|
236 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY FUN_CHEM JAC_CHEM SET2ZERO" |
---|
237 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST RADAU5 Update_SUN" |
---|
238 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST KppSolveCmplx KppDecompCmplx";; |
---|
239 | |
---|
240 | kpp_sdirk) |
---|
241 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
242 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST SDIRK JAC_CHEM SET2ZERO FUN_CHEM" |
---|
243 | KPP_INTERFACE_IGNORE="$KPP_INTERFACE_IGNORE Set2zero SET2ZERO FUN_CHEM";; |
---|
244 | |
---|
245 | kpp_seulex) |
---|
246 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY" |
---|
247 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST ATMSEULEX" |
---|
248 | KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST SEULEX_ErrorMsg SEULEX_Integrator FUN_CHEM JAC_CHEM SEUL" |
---|
249 | KPP_INTERFACE_IGNORE="$KPP_INTERFACE_IGNORE SEULEX_Integrator SDIRK FUN_CHEM SEUL";; |
---|
250 | |
---|
251 | \?) print "SORRY ONLY ROSENBROCK METHODS WORK AT THE MOMENT:" $KPP_SOLVER |
---|
252 | exit 1;; |
---|
253 | esac |
---|
254 | #mz-ak-20070509+ |
---|
255 | |
---|
256 | KPP_INCLUDE_LIST="Parameters Global JacobianSP Monitor" |
---|
257 | if [[ $MODE = "vector" ]] |
---|
258 | then |
---|
259 | KPP_INCLUDE_LIST="$KPP_INCLUDE_LIST kp4_compress_header" |
---|
260 | fi |
---|
261 | |
---|
262 | #Get definition Files |
---|
263 | |
---|
264 | cp $DEFDIR/*.eqn . |
---|
265 | cp $DEFDIR/*.spc . |
---|
266 | cp $DEFDIR/${PREFIX}.kpp . |
---|
267 | |
---|
268 | # Global variable are defined here |
---|
269 | # This has the advantage that it is not necessary to include these variables in all .kpp definition files |
---|
270 | |
---|
271 | cat >> ${PREFIX}.kpp << EOF |
---|
272 | #INLINE F90_GLOBAL |
---|
273 | ! QVAP - Water vapor |
---|
274 | REAL(kind=dp) :: QVAP |
---|
275 | ! FAKT - Conversion factor |
---|
276 | REAL(kind=dp) :: FAKT |
---|
277 | |
---|
278 | #ENDINLINE |
---|
279 | EOF |
---|
280 | |
---|
281 | # Store mechanism name in file mech_list |
---|
282 | cat >> mech_list << EOF |
---|
283 | ! Mechanism: $MECH |
---|
284 | ! |
---|
285 | EOF |
---|
286 | |
---|
287 | # Run kpp |
---|
288 | |
---|
289 | $KPP $DEF_PREFIX |
---|
290 | |
---|
291 | # Get templates for C++ program |
---|
292 | |
---|
293 | cp $BASE/templates/module_header* . # Use fixed Module_header |
---|
294 | cp $BASE/templates/initialize_kpp_ctrl_template.f90 . # CTRL kpp time stepping |
---|
295 | |
---|
296 | # file with subroutine list for c++ program create_kpp_module |
---|
297 | |
---|
298 | for i in $KPP_FILE_LIST |
---|
299 | do |
---|
300 | echo ${PREFIX}_${i} >> file_list |
---|
301 | done |
---|
302 | echo initialize_kpp_ctrl_template >> file_list |
---|
303 | |
---|
304 | # file with subroutine list for c++ program create_kpp_module |
---|
305 | |
---|
306 | for i in $KPP_SUBROUTINE_LIST |
---|
307 | do |
---|
308 | echo $i >> subroutine_list |
---|
309 | done |
---|
310 | |
---|
311 | # file with include list for c++ program create_kpp_module |
---|
312 | |
---|
313 | for i in $KPP_INCLUDE_LIST |
---|
314 | do |
---|
315 | echo ${PREFIX}_${i} >> include_list |
---|
316 | done |
---|
317 | |
---|
318 | touch interface_ignore_list |
---|
319 | for i in $KPP_INTERFACE_IGNORE |
---|
320 | do |
---|
321 | echo $i >> interface_ignore_list |
---|
322 | done |
---|
323 | |
---|
324 | echo start kpp4palm.exe with arguments |
---|
325 | echo $PREFIX $MODE $VLEN $DE_INDEX $DE_INDEX_FAST |
---|
326 | |
---|
327 | $BASE/bin/kpp4palm.exe $PREFIX $MODE $VLEN $DE_INDEX $DE_INDEX_FAST |
---|
328 | |
---|
329 | #Prelimanary, substitution has to be moved into kpp4palm.exe |
---|
330 | if [[ $MODE = "vector" ]] |
---|
331 | then |
---|
332 | sed -i -e 's/phot(nphot/phot(vl_dim,nphot/g' kk_kpp.f90 |
---|
333 | fi |
---|
334 | |
---|
335 | if [[ -e $OUTDIR/${OUTFILE}.f90 ]] |
---|
336 | then |
---|
337 | mv $OUTDIR/${OUTFILE}.f90 $OUTDIR/${OUTFILE}.f90.sav |
---|
338 | fi |
---|
339 | cp -p kk_kpp.f90 $OUTDIR/${OUTFILE}.f90 |
---|
340 | echo " " |
---|
341 | echo "Write kpp module -- > " $OUTDIR/${OUTFILE}.f90 |
---|
342 | |
---|
343 | if [[ $UPDT = "YES" ]] |
---|
344 | then |
---|
345 | cp -p kk_kpp.f90 $DEFDIR/${OUTFILE}.f90 |
---|
346 | echo " " |
---|
347 | echo "Write kpp module -- > " $DEFDIR/${OUTFILE}.f90 |
---|
348 | fi |
---|
349 | |
---|
350 | if [[ $KEEP = "NO" ]] |
---|
351 | then |
---|
352 | cd $MY_PWD |
---|
353 | rm -rf $WORK |
---|
354 | fi |
---|
355 | exit |
---|
356 | |
---|