def solve(index, parameter, lower, upper, tolerance, ref, model): modelirrs = [] diffs = [] sol = None err = 0 def f(x): spectrum = model({parameter: x}) model_irrad = trapz(spectrum[1], spectrum[0]) modelirrs.append(model_irrad) diff = model_irrad - ref diffs.append({x: diff}) return diff try: sol = zeroin(lower, upper, f, tolerance) #sol = rtmcache(model, zeroin, (lower, upper, f, tolerance)) print '%d: %s in %d' % (index, str('%s %g' % (model['time'], sol)).ljust(28), len(modelirrs)) except BadBoundsError as e: print 'Bad Bounds at %s: %s; %s' % (model['time'], diffs, e.message) err = 1 except NoConvergeError as e: print 'NO CONVERGE' err = 2 #print 'No convergence at %s: %s; %s\ngiven %s' % (model['time'], diffs, e.message, model) except _rtm.RTMError as e: print 'No output at %s: %s; %s' % (model['time'], diffs, e.message) err = 3 return [model['time'], ref, modelirrs, sol, index, err]
def optimize(self, model, target_irradiance): self.meta = { 'model': dict(model), 'parameter': self.parameter, 'target_irradiance': target_irradiance, 'iterations': {}, } def f(x): model.update({self.parameter: x}) diff = model.irradiance[self.irradiance] - target_irradiance self.meta['iterations'].update({x: diff}) return diff result = zeroin(self.bounds[0], self.bounds[1], f, self.tolerance) self.meta['model'].update({self.parameter: result}) return result