Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
CellMLToNektar.pycml.mathml_ci Class Reference
Inheritance diagram for CellMLToNektar.pycml.mathml_ci:
Inheritance graph
[legend]
Collaboration diagram for CellMLToNektar.pycml.mathml_ci:
Collaboration graph
[legend]

Public Member Functions

def __init__
 
def variable
 
def get_units
 
def evaluate
 
def classify_variables
 
- Public Member Functions inherited from CellMLToNektar.pycml.mathml
def __init__
 
def __repr__
 
def __deepcopy__
 
def clone_self
 
def get_original_of_clone
 
def get_component
 
def model
 
def eval
 
- Public Member Functions inherited from CellMLToNektar.pycml.element_base
def __init__
 
def __delattr__
 
def __setattr__
 
def rootNode
 
def cmeta_id
 
def xml_remove_child_at
 
def xml_doc
 
def xml_properties
 

Static Public Member Functions

def create_new
 
- Static Public Member Functions inherited from CellMLToNektar.pycml.mathml
def clone
 

Private Member Functions

def _unset_cached_links
 
def _set_variable_obj
 
def _get_binding_time
 
def _rename
 
def _reduce
 

Private Attributes

 _cml_variable
 
 _cml_units
 

Additional Inherited Members

- Public Attributes inherited from CellMLToNektar.pycml.element_base
 xml_attributes
 
- Properties inherited from CellMLToNektar.pycml.mathml
 component = property(get_component)
 

Detailed Description

Definition at line 4219 of file pycml.py.

Constructor & Destructor Documentation

def CellMLToNektar.pycml.mathml_ci.__init__ (   self)

Definition at line 4220 of file pycml.py.

4221  def __init__(self):
4222  super(mathml_ci, self).__init__()
4223  self._cml_variable = None
4224  self._cml_units = None
4225  return

Member Function Documentation

def CellMLToNektar.pycml.mathml_ci._get_binding_time (   self)
private
Return the binding time of this expression.

The binding time of a <ci> element is that of the variable it
represents.

Definition at line 4266 of file pycml.py.

Referenced by CellMLToNektar.pycml.mathml_ci._reduce(), CellMLToNektar.pycml.mathml_apply._reduce(), and CellMLToNektar.pycml.mathml_piecewise._reduce().

4267  def _get_binding_time(self):
4268  """Return the binding time of this expression.
4269 
4270  The binding time of a <ci> element is that of the variable it
4271  represents.
4272  """
4273  return self.variable._get_binding_time()
def CellMLToNektar.pycml.mathml_ci._reduce (   self)
private
Reduce this expression by evaluating its static parts.

If this is a static variable, replace by its value (as a <cn> element).

Otherwise the behaviour depends on the number of uses of this
variable.  If there is only one, instantiate the definition of
this variable here in place of the <ci> element, otherwise
leave the element unchanged to avoid code duplication.

Definition at line 4282 of file pycml.py.

References CellMLToNektar.pycml.cellml_variable._get_binding_time(), CellMLToNektar.pycml.mathml_cn._get_binding_time(), CellMLToNektar.pycml.mathml_ci._get_binding_time(), CellMLToNektar.pycml.mathml_ci.classify_variables(), CellMLToNektar.utilities.DEBUG(), CellMLToNektar.pycml.mathml_cn.evaluate(), and CellMLToNektar.pycml.mathml_ci.evaluate().

4283  def _reduce(self):
4284  """Reduce this expression by evaluating its static parts.
4285 
4286  If this is a static variable, replace by its value (as a <cn> element).
4287 
4288  Otherwise the behaviour depends on the number of uses of this
4289  variable. If there is only one, instantiate the definition of
4290  this variable here in place of the <ci> element, otherwise
4291  leave the element unchanged to avoid code duplication.
4292  """
4293  bt = self._get_binding_time()
4294  DEBUG('partial-evaluator', "Reducing", self.variable.fullname(),
4295  "which is", bt)
4296  if bt is BINDING_TIMES.static:
4297  value = self.evaluate()
4298  attrs = {(u'cml:units', NSS[u'cml']): self.variable.units}
4299  cn = self.xml_create_element(u'cn', NSS[u'm'],
4300  content=unicode("%.17g" % value),
4301  attributes=attrs)
4302  DEBUG('partial-evaluator', " value =", unicode(cn))
4303  self._xfer_complexity(cn)
4304  self.xml_parent.xml_insert_after(self, cn)
4305  self.xml_parent.xml_remove_child(self)
4306  self.variable._decrement_usage_count()
4307  else:
4308  defns = self.variable.get_dependencies()
4309  if defns:
4310  defn = defns[0]
4311  else:
4312  # Just need to update the name to be canonical - done in later pass
4313  defn = None
4314  if isinstance(defn, cellml_variable):
4315  if self.variable.pe_keep:
4316  # Don't remove this var, just reduce its source
4317  DEBUG('partial-evaluator', "Keeping",
4318  self.variable.fullname())
4319  self.variable._reduce(update_usage=True)
4320  else:
4321  # Create a new <ci> element
4322  ci = self.xml_create_element(
4323  u'ci', NSS[u'm'], content=defn.fullname(cellml=True))
4324  self._xfer_complexity(ci)
4325  ci._set_variable_obj(defn)
4326  DEBUG('partial-evaluator', " to", defn.fullname())
4327  self.xml_parent.xml_insert_after(self, ci)
4328  self.xml_parent.xml_remove_child(self)
4329  # Decrement the usage count of just us, not source vars
4330  self.variable._decrement_usage_count(follow_maps=False)
4331  # May need to recurse down maps
4332  ci._reduce()
4333  elif isinstance(defn, mathml_apply):
4334  if (not self.variable.pe_keep and
4335  (self.variable.get_usage_count() == 1 or
4336  self.rootNode.partial_evaluator.is_instantiable(defn))):
4337  # defn is defining expression, so will be a MathML element already,
4338  # and should be reduced already as well due to topological sort.
4339  # Clone the RHS and instantiate it here.
4340  rhs = mathml.clone(list(defn.operands())[1])
4341  DEBUG('partial-evaluator', " to", rhs)
4342  parent = self.xml_parent
4343  parent.xml_insert_after(self, rhs)
4344  parent.xml_remove_child(self)
4345  parent._adjust_complexity(self, rhs)
4346  # Flag the defining expression for removal
4347  defn._pe_process = u'remove'
4348  self.variable._decrement_usage_count()
4349  elif defn is not None:
4350  raise ValueError("Unexpected variable definition: " + defn.xml())
4351  return
def CellMLToNektar.pycml.mathml_ci._rename (   self,
  new_name = None 
)
private
Update the variable reference to use a canonical name.

Definition at line 4274 of file pycml.py.

Referenced by CellMLToNektar.pycml.mathml_ci.variable().

4275  def _rename(self, new_name=None):
4276  """Update the variable reference to use a canonical name."""
4277  self.xml_remove_child(unicode(self))
4278  if new_name is None:
4279  new_name = self.variable.fullname(cellml=True)
4280  self.xml_append(unicode(new_name))
4281  return
def CellMLToNektar.pycml.mathml_ci._set_variable_obj (   self,
  var 
)
private
Set the variable object referred to by this element.

Definition at line 4244 of file pycml.py.

References CellMLToNektar.pycml.mathml_ci._cml_variable.

4245  def _set_variable_obj(self, var):
4246  """Set the variable object referred to by this element."""
4247  self._cml_variable = var
def CellMLToNektar.pycml.mathml_ci._unset_cached_links (   self,
  elt = None 
)
private
Forget cached component and variable references in this MathML tree.

Used by partial evaluator when moving maths to a new component, and by
simulation protocols.

Definition at line 4226 of file pycml.py.

References CellMLToNektar.pycml.mathml_ci._cml_variable.

4227  def _unset_cached_links(self, elt=None):
4228  """Forget cached component and variable references in this MathML tree.
4229 
4230  Used by partial evaluator when moving maths to a new component, and by
4231  simulation protocols.
4232  """
4233  self._cml_variable = None
4234  super(mathml_ci, self)._unset_cached_links()
def CellMLToNektar.pycml.mathml_ci.classify_variables (   self,
  dependencies_only = False,
  needs_special_treatment = lambda n: None 
)
Classify variables in this expression according to how they are used.

For ci elements we just return a set containing the referenced variable
as the single dependency.  If dependencies_only is False, we also mark
the variable as used.

Definition at line 4353 of file pycml.py.

References Nektar::LibUtilities::AnalyticExpressionEvaluator::AnalyticExpression::definition< ScannerT >.variable, and CellMLToNektar.pycml.mathml_ci.variable().

Referenced by CellMLToNektar.pycml.mathml_ci._reduce().

4354  needs_special_treatment=lambda n: None):
4355  """Classify variables in this expression according to how they are used.
4356 
4357  For ci elements we just return a set containing the referenced variable
4358  as the single dependency. If dependencies_only is False, we also mark
4359  the variable as used.
4360  """
4361  var = self.variable
4362  if not dependencies_only:
4363  var._used()
4364  return set([var])
def CellMLToNektar.pycml.mathml_ci.create_new (   elt,
  variable_name 
)
static
Create a new <ci> element with the given variable name.

Definition at line 4366 of file pycml.py.

4367  def create_new(elt, variable_name):
4368  """Create a new <ci> element with the given variable name."""
4369  new_elt = elt.xml_create_element(u'ci', NSS[u'm'],
4370  content=unicode(variable_name))
4371  return new_elt
def CellMLToNektar.pycml.mathml_ci.evaluate (   self)
Evaluate this expression by returning the value of the
variable it represents.

Definition at line 4259 of file pycml.py.

Referenced by CellMLToNektar.pycml.mathml_constructor._eval_self(), and CellMLToNektar.pycml.mathml_ci._reduce().

4260  def evaluate(self):
4261  """
4262  Evaluate this expression by returning the value of the
4263  variable it represents.
4264  """
4265  return self.variable.get_value()
def CellMLToNektar.pycml.mathml_ci.get_units (   self,
  return_set = True 
)
Return the units of the variable represented by this element.

Definition at line 4248 of file pycml.py.

References CellMLToNektar.pycml.cellml_model._cml_units, CellMLToNektar.pycml.cellml_component._cml_units, and CellMLToNektar.pycml.mathml_units_mixin_tokens._cml_units.

Referenced by CellMLToNektar.pycml.mathml._ensure_units_exist(), CellMLToNektar.pycml.mathml_units_mixin_tokens._set_in_units(), and CellMLToNektar.pycml.mathml_apply._set_in_units().

4249  def get_units(self, return_set=True):
4250  """Return the units of the variable represented by this element."""
4251  if not self._cml_units:
4252  self._cml_units = UnitsSet([self.component.get_units_by_name(self.variable.units)],
4253  expression=self)
4254  if not return_set:
4255  u = self._cml_units.extract()
4256  else:
4257  u = self._cml_units
4258  return u
def CellMLToNektar.pycml.mathml_ci.variable (   self)
Cache & return the variable object refered to by this element.

Definition at line 4236 of file pycml.py.

References CellMLToNektar.pycml.mathml_ci._cml_variable, and CellMLToNektar.pycml.mathml_ci._rename().

Referenced by CellMLToNektar.pycml.mathml_ci.classify_variables().

4237  def variable(self):
4238  """Cache & return the variable object refered to by this element."""
4239  if self._cml_variable is None:
4240  vname = unicode(self).strip()
4241  self._rename(vname) # Remove excess whitespace from our text content
4242  self._cml_variable = self.component.get_variable_by_name(vname)
4243  return self._cml_variable

Member Data Documentation

CellMLToNektar.pycml.mathml_ci._cml_units
private

Definition at line 4223 of file pycml.py.

Referenced by CellMLToNektar.pycml.mathml_apply._set_in_units(), CellMLToNektar.pycml.mathml_piecewise._set_in_units(), CellMLToNektar.pycml.mathml_apply.get_units(), and CellMLToNektar.pycml.mathml_piecewise.get_units().

CellMLToNektar.pycml.mathml_ci._cml_variable
private

Definition at line 4222 of file pycml.py.

Referenced by CellMLToNektar.pycml.mathml_ci._set_variable_obj(), CellMLToNektar.pycml.mathml_ci._unset_cached_links(), and CellMLToNektar.pycml.mathml_ci.variable().