1
2
3 """
4 Interface for creating modeling environments.
5
6 Author: R. Lombaert
7
8 """
9
10 import os
11 from time import gmtime
12 import types
13
14 import cc.path
15 from cc.tools.io import DataIO
16
17
18
20
21 """
22 The basic modeling environment. Inherited by MCMax() and Gastronoom().
23
24 """
25
26 - def __init__(self,code,path,replace_db_entry=0,new_entries=[],\
27 single_session=0):
28
29 """
30 Initializing an instance of ModelingSession.
31
32 @param code: code for which the modelingsession is created
33 @type code: string
34 @param path: modeling output folder in the code's home folder
35 @type path: string
36
37 @keyword replace_db_entry: replace an entry in the database with a
38 newly calculated model with a new model id
39 (eg if some general data not included in
40 the inputfiles is changed)
41
42 (default: 0)
43 @type replace_db_entry: bool
44 @keyword new_entries: The new model_ids when replace_db_entry is 1
45 of other models in the grid. These are not
46 replaced!
47
48 (default: [])
49 @type new_entries: list[str]
50 @keyword single_session: If this is the only CC session. Speeds up db
51 check.
52
53 (default: 0)
54 @type single_session: bool
55
56 """
57
58 self.path = path
59 self.code = code
60 self.model_id = ''
61 self.replace_db_entry = replace_db_entry
62 self.new_entries = new_entries
63 self.single_session = single_session
64 if code == 'Chemistry':
65 self.mutable = []
66 else:
67 mutablefile = os.path.join(cc.path.aux,\
68 'Mutable_Parameters_%s.dat'%code)
69 self.mutable = [line[0]
70 for line in DataIO.readFile(mutablefile,delimiter=' ')
71 if ' '.join(line)]
72 self.mutable = [line for line in self.mutable if line[0] != '#']
73 fout = os.path.join(getattr(cc.path,self.code.lower()),self.path)
74 DataIO.testFolderExistence(os.path.join(fout,'models'))
75
76
77
79
80 '''
81 Make a new model_id based on the current UTC in seconds since 1970.
82
83 '''
84
85 return 'model_%.4i-%.2i-%.2ih%.2i-%.2i-%.2i' \
86 %(gmtime()[0],gmtime()[1],gmtime()[2],\
87 gmtime()[3],gmtime()[4],gmtime()[5])
88
89
90
91 - def setCommandKey(self,comm_key,star,key_type,star_key=None,\
92 alternative=None,make_int=0,exp_not=0):
93
94 '''
95 Try setting a key in the command_list from a star instance.
96
97 If the key is unknown, it is left open and will be filled in from the
98 standard gastronoom inputfile.
99
100 @param comm_key: the name of the keyword in the command list
101 @type comm_key: string
102 @param star: The parameter set
103 @type star: Star()
104 @param key_type: the type of the keyword, either 'DUST' or 'GAS'
105 @type key_type: string
106
107 @keyword star_key: the name of the keyword in the star instance
108 (minus '_%s'%key_type, which is added as well in a
109 second attempt if the first without the addition is
110 not found), if None, it is equal to comm_key
111
112 (default: None)
113 @type star_key: string
114 @keyword alternative: a default value passed from the standard
115 inputfile that is used if the keyword or the
116 keyword + '_%s'%key_type is not found in Star()
117
118 (default: None)
119 @type alternative: string
120 @keyword make_int: make an integer before converting to string for this
121 keyword.
122
123 (default: 0)
124 @type make_int: boolean
125 @keyword exp_not: Convert to exponential notation in a string
126
127 (default: 0)
128 @type exp_not: bool
129
130 @return: True if successful, otherwise False.
131 @rtype: bool
132
133 '''
134
135 if star_key is None: star_key = comm_key
136 try:
137 self.command_list[comm_key] = \
138 DataIO.inputToString(star[star_key],make_int,exp_not)
139 return True
140 except KeyError:
141 try:
142 self.command_list[comm_key] = \
143 DataIO.inputToString(star[star_key+ '_%s'%key_type],\
144 make_int,exp_not)
145 return True
146 except KeyError:
147 if not alternative is None:
148 self.command_list[comm_key] = \
149 DataIO.inputToString(alternative,make_int,exp_not)
150 return True
151 else:
152 return False
153
154
155
156 - def compareCommandLists(self,this_list,modellist,code,ignoreAbun=0,\
157 extra_dict=None,check_keys=[]):
158
159 """
160 Comparing a command_list with a database entry.
161
162 @param this_list: parameters in this modeling session
163 @type this_list: dict
164 @param modellist: parameters from database model
165 @type modellist: dict
166 @param code: The GASTRoNOoM subcode
167 @type code: string
168
169 @keyword ignoreAbun: only relevant for mline: ignore the 4 abundance
170 parameters (such as for co)
171
172 (default: 0)
173 @type ignoreAbun: bool
174 @keyword extra_dict: if not None this gives extra dictionary entries
175 to be used in the comparison on top of this_list.
176 The extra entries are assumed present in modellist
177 otherwise the comparison will return False.
178
179 (default: None)
180 @type extra_dict: dict
181 @keyword check_keys: Only check keys given in this list. If empty, the
182 standard keyword lists are used.
183
184 (default: [])
185 @type check_keys: list[str]
186
187 @return: Comparison between the two parameter sets
188 @rtype: bool
189
190 """
191
192 model_bool_list = []
193 if not extra_dict is None: this_list.update(extra_dict)
194 if check_keys:
195 keywords = check_keys
196 elif code == 'mcmax':
197 keywords = set(this_list.keys()+modellist.keys())
198 if 'dust_species' in keywords:
199 keywords.remove('dust_species')
200 if 'IN_PROGRESS' in keywords:
201 keywords.remove('IN_PROGRESS')
202
203
204
205
206
207 else:
208 keywords = getattr(self,code + '_keywords')
209 if code == 'mline' and ignoreAbun and not check_keys:
210 keywords = [key
211 for key in keywords
212 if key not in ['ABUN_MOLEC','ABUN_MOLEC_RINNER',\
213 'ABUN_MOLEC_RE','RMAX_MOLEC']]
214
215 for keyword in keywords:
216
217
218
219
220
221
222
223
224
225
226 try:
227 try:
228 try:
229 val = float(this_list[keyword])
230 except TypeError:
231 raise ValueError
232 delta = not val and 1e-10 or 0.001*val
233 if val < 0:
234 tb = val-delta > float(modellist[keyword]) > val+delta
235 else:
236 tb = val-delta < float(modellist[keyword]) < val+delta
237 except ValueError:
238 tb = this_list[keyword]==modellist[keyword]
239 except KeyError:
240 if keyword not in this_list.keys() \
241 and keyword not in modellist.keys():
242 tb = True
243 else:
244 tb = False
245 model_bool_list.append(tb)
246
247 if False not in model_bool_list:
248 return True
249 else:
250 return False
251
252
253
254 - def cCL(self,*args,**kwargs):
255
256 '''
257 Short-hand helper function for compareCommandLists.
258
259 '''
260
261 return self.compareCommandLists(*args,**kwargs)
262