- Timestamp:
- 02/03/12 01:35:40 (4 months ago)
- File:
-
- 1 edited
-
simo/trunk/src/simo/simulation/sim.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
simo/trunk/src/simo/simulation/sim.py
r2680 r2706 1202 1202 # check if any of the active units has forced operations during the 1203 1203 # current time step 1204 tind, torem = self.data.get_tind(self.main_level, 0)1205 ids = self.data.get_id(self.main_level, tind)1206 1204 keys = set(self._forced_operations.keys()) 1207 # get the current and initial dates for all objects 1208 dates = self.data.get_date(tind) 1209 # go through each simulation unit and evaluate forced operations for 1210 # a single unit at a time 1211 # NB: evaluating forced operation chains for a single 1212 # (iteration,branch,unit) may not be very efficient 1213 tslev, tsind = self._ctrl.built_ins[names.TIME_STEP] 1214 timesteps, err = self.data.get_value(tind, tsind) 1215 if self.timespan.unit == names.YEAR: 1216 years, months, days = timesteps, 0, 0 1217 elif self.timespan.unit == names.MONTH: 1218 years, months, days = 0, timesteps, 0 1219 indset = set(range(tind.shape[0])) 1220 for i in range(tind.shape[0]): 1221 key = (self._start_iter + tind[i,0], tind[i,1], ids[i][0]) 1222 if key not in keys: 1223 continue 1224 self._blocked_forced.append(tind[i]) 1225 # define current simulation time step end date 1205 for fkey in keys: 1206 tind, torem = self.data.get_tind(self.main_level, 0) 1207 ids = self.data.get_id(self.main_level, tind) 1208 # get the current and initial dates for all objects 1209 dates = self.data.get_date(tind) 1210 # go through each simulation unit and evaluate forced 1211 # operations for a single unit at a time 1212 # NB: evaluating forced operation chains for a single 1213 # (iteration,branch,unit) may not be very efficient 1214 tslev, tsind = self._ctrl.built_ins[names.TIME_STEP] 1215 timesteps, err = self.data.get_value(tind, tsind) 1226 1216 if self.timespan.unit == names.YEAR: 1227 end_date = date(dates[i].year + int(years[i]), 1228 dates[i].month + months, 1229 dates[i].day + days) 1230 end_date += relativedelta(days=-1) 1217 years, months, days = timesteps, 0, 0 1231 1218 elif self.timespan.unit == names.MONTH: 1232 yr = int(months[i] / 12.) 1233 mo = int(months[i] % 12.) 1234 end_date = date(dates[i].year + yr, 1235 dates[i].month + mo, 1236 dates[i].day + days) 1237 end_date += relativedelta(days=-1) 1238 # make sure the end_date is also smaller than the ending date. 1239 if self.timespan.ending.type == 'end_date': 1240 # if time step is 0, set the run end as end_date, otherwise 1241 # this is for cases when the current time step is the last one 1242 # and the time step has been set to zero to skip growing 1243 run_end = self.timespan.ending.target 1244 if timesteps[i] == 0: 1245 end_date = run_end 1246 else: 1247 end_date = min(end_date, run_end) 1248 # go through the current simulation unit's forced operations 1249 ops, rem = [], [] 1250 for j, op in enumerate(self._forced_operations[key]): 1251 # if forced operation should be evaluated during the current 1252 # time step, store the unit id to operation evaluation list 1253 if op.date >= dates[i] and op.date <= end_date: 1254 ops.append(op) 1255 rem.insert(0, j) 1256 for j in rem: 1257 self._forced_operations[key].pop(j) 1258 if len(self._forced_operations[key]) == 0: 1259 del self._forced_operations[key] 1260 for op in ops: 1261 self._curr_forced_op_id = op.op_id 1262 # block all other objects 1263 block = list(indset.difference((i,))) 1264 self.data.block(self.main_level, 0, tind[block,:], True) 1265 ind = [] 1266 for focind in self._forced_chain_index: 1267 mccind = [focind[c] for c in op.chain_names if c in focind] 1268 ind.append(mccind) 1269 orig_values = self._update_data(tind[(i,),:], op.data_values) 1270 # evaluate the forced operation model chains 1271 self._curr_op_type = 'forced' 1272 self._evaluate_chains('forced_operation', ind) 1273 self._curr_op_type = 'simulated' 1274 op.data_values = self._update_data(tind[(i,),:], orig_values) 1275 # release the blocked objects 1276 self.data.release(self.main_level, 0, tind[block,:], True, 1277 False) 1278 self._curr_forced_op_id = None 1219 years, months, days = 0, timesteps, 0 1220 indset = set(range(tind.shape[0])) 1221 for i in range(tind.shape[0]): 1222 key = (self._start_iter + tind[i,0], tind[i,1], ids[i][0]) 1223 if key != fkey: 1224 continue 1225 self._blocked_forced.append(tind[i]) 1226 # define current simulation time step end date 1227 if self.timespan.unit == names.YEAR: 1228 end_date = date(dates[i].year + int(years[i]), 1229 dates[i].month + months, 1230 dates[i].day + days) 1231 end_date += relativedelta(days=-1) 1232 elif self.timespan.unit == names.MONTH: 1233 yr = int(months[i] / 12.) 1234 mo = int(months[i] % 12.) 1235 end_date = date(dates[i].year + yr, 1236 dates[i].month + mo, 1237 dates[i].day + days) 1238 end_date += relativedelta(days=-1) 1239 # make sure the end_date is also smaller than the ending date. 1240 if self.timespan.ending.type == 'end_date': 1241 # if time step is 0, set the run end as end_date, otherwise 1242 # this is for cases when the current time step is 1243 # the last one and the time step has been set to zero 1244 # to skip growing 1245 run_end = self.timespan.ending.target 1246 if timesteps[i] == 0: 1247 end_date = run_end 1248 else: 1249 end_date = min(end_date, run_end) 1250 # go through the current simulation unit's forced operations 1251 ops, rem = [], [] 1252 for j, op in enumerate(self._forced_operations[key]): 1253 # if forced operation should be evaluated during 1254 # the current time step, store the unit id to 1255 # operation evaluation list 1256 if op.date >= dates[i] and op.date <= end_date: 1257 ops.append(op) 1258 rem.insert(0, j) 1259 for j in rem: 1260 self._forced_operations[key].pop(j) 1261 if len(self._forced_operations[key]) == 0: 1262 del self._forced_operations[key] 1263 for op in ops: 1264 self._curr_forced_op_id = op.op_id 1265 block = list(indset.difference((i,))) 1266 self.data.block(self.main_level, 0, tind[block,:], True) 1267 ind = [] 1268 for focind in self._forced_chain_index: 1269 mccind = [focind[c] for c in op.chain_names \ 1270 if c in focind] 1271 ind.append(mccind) 1272 orig_values = self._update_data(tind[(i,),:], 1273 op.data_values) 1274 # evaluate the forced operation model chains 1275 self._curr_op_type = 'forced' 1276 self._evaluate_chains('forced_operation', ind) 1277 self._curr_op_type = 'simulated' 1278 op.data_values = self._update_data(tind[(i,),:], 1279 orig_values) 1280 # release the blocked objects 1281 self.data.release(self.main_level, 0, tind[block,:], 1282 True, False) 1283 self._curr_forced_op_id = None 1284 break 1279 1285 if len(self._blocked_forced) > 0: 1280 1286 self._blocked_forced = numpy.array(self._blocked_forced)
Note: See TracChangeset
for help on using the changeset viewer.
