Skip to content

Instantly share code, notes, and snippets.

@fcostin
Created March 1, 2010 13:45
Show Gist options
  • Save fcostin/318376 to your computer and use it in GitHub Desktop.
Save fcostin/318376 to your computer and use it in GitHub Desktop.
import cmepy.solver
def do_something_fancy(y):
"""
transforms the solution y in some interesting and useful manner
"""
return y # TODO IMPLEMENT ME
class FancySolver(object):
"""
FancySolver looks like a solver but does something fancy after each step
"""
def __init__(self, **args):
self._solver_args = args
self._solver = None
self._rebuild_solver()
def _rebuild_solver(self, p_0=None, t_0=None):
"""
private method used to rebuild solver instance
"""
args = dict(self._solver_args)
if p_0 is not None:
args['p_0'] = p_0
if t_0 is not None:
args['t_0'] = t_0
self._solver = cmepy.solver.create(**args)
def step(self, t):
"""
Advance the solver to time t (and secretly do something fancy to the solution)
"""
self._solver.step(t)
modified_y = do_something_fancy(self._solver.y)
self._rebuild_solver(modified_y, self._solver.t)
@property
def y(self):
"""
Read only property returning the current solution y.
"""
return self._solver.y
@property
def t(self):
"""
Read only property returning current solution time t.
"""
return self._solver.t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment