Example #1
0
def with_rk4(dxdt,autonom=False,order=4):
  """Wrap dxdt in rk4"""
  integrator       = functools.partial(rk4,order=order)
  if autonom: step = lambda x0,t0,dt: integrator(lambda t,x: dxdt(x),x0,np.nan,dt)
  else:       step = lambda x0,t0,dt: integrator(            dxdt   ,x0,t0    ,dt)
  name = "rk"+str(order)+" integration of "+pretty_repr(dxdt)
  step = NamedFunc(step,name)
  return step
Example #2
0
def with_rk4(dxdt, autonom=False, stages=4, s=0):
    """Wrap `dxdt` in `rk4`."""
    def tendency(x, t):
        if autonom:
            return dxdt(x)
        else:
            return dxdt(x, t)

    def step(x0, t0, dt):
        return rk4(tendency, x0, t0, dt, stages=stages)

    name = "rk" + str(stages) + " integration of " + pretty_repr(dxdt)
    step = NamedFunc(step, name)
    return step
Example #3
0
  def __repr__(self):
    with printoptions(threshold=self.threshold,precision=self.precision):
      # new line chars
      NL = '\n' + self.ch + ' '*(self.indent-1)

      # Infinite recursion prevention
      is_top_level = False
      if NestedPrint._stack == []:
        is_top_level = True
      if self in NestedPrint._stack:
        return "**Recursion**"
      NestedPrint._stack += [self]

      # Use included or filter-out excluded
      keys = self.included or filter_out(vars(self), *self.excluded)

      # Process attribute repr's
      txts = {}
      for key in keys:
        t = pretty_repr(getattr(self,key)) # sub-repr
        if '\n' in t:
          # Activate multi-line printing
          t = t.replace('\n',NL+' '*self.indent)      # other lines
          t = NL+' '*self.indent + t                  # first line
        t = NL + self.aliases.get(key,key) + ': ' + t # key-name
        txts[key] = t

      def sortr(x):
        if x in self.ordering:
          key = -1000 + self.ordering.index(x)
        else:
          if self.ordr_by_linenum:
            key = self.ordr_by_linenum*txts[x].count('\n')
          else:
            key = x.lower()
            # Convert str to int (assuming ASCII) for comparison with above cases
            key = sum( ord(x)*128**i for i,x in enumerate(x[::-1]) )
        return key

      # Assemble string
      s = repr_type_and_name(self)
      for key in sorted(txts, key=sortr):
        s += txts[key]

      # Empty _stack when top-level printing finished
      if is_top_level:
        NestedPrint._stack = []

      return s