Nektar++
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:
[legend]

Public Member Functions

def __init__ (self)
 
def variable (self)
 
def get_units (self, return_set=True)
 
def evaluate (self)
 
def classify_variables (self, dependencies_only=False, needs_special_treatment=lambda n:None)
 
- Public Member Functions inherited from CellMLToNektar.pycml.mathml
def __repr__ (self)
 
def __deepcopy__ (self, memo)
 
def clone_self (self, register=False)
 
def get_original_of_clone (self)
 
def get_component (self)
 
def model (self)
 
def eval (self, elt)
 
- Public Member Functions inherited from CellMLToNektar.pycml.element_base
def __delattr__ (self, key)
 
def __setattr__ (self, key, value)
 
def rootNode (self)
 
def cmeta_id (self)
 
def xml_remove_child_at (self, index=-1)
 
def xml_doc (self)
 
def xml_properties (self)
 

Static Public Member Functions

def create_new (elt, variable_name)
 
- Static Public Member Functions inherited from CellMLToNektar.pycml.mathml
def clone (expr)
 

Private Member Functions

def _unset_cached_links (self, elt=None)
 
def _set_variable_obj (self, var)
 
def _get_binding_time (self)
 
def _rename (self, new_name=None)
 
def _reduce (self)
 

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

◆ __init__()

def CellMLToNektar.pycml.mathml_ci.__init__ (   self)

Reimplemented from CellMLToNektar.pycml.mathml.

Definition at line 4220 of file pycml.py.

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

Member Function Documentation

◆ _get_binding_time()

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.

4266  def _get_binding_time(self):
4267  """Return the binding time of this expression.
4268 
4269  The binding time of a <ci> element is that of the variable it
4270  represents.
4271  """
4272  return self.variable._get_binding_time()
4273 

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

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

◆ _reduce()

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.

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

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_piecewise._get_binding_time(), CellMLToNektar.pycml.mathml_diff._get_binding_time(), CellMLToNektar.pycml.mathml_and._get_binding_time(), CellMLToNektar.pycml.mathml_or._get_binding_time(), CellMLToNektar.pycml.mathml_eq._get_binding_time(), CellMLToNektar.pycml.mathml_apply._get_binding_time(), CellMLToNektar.utilities.DEBUG(), CellMLToNektar.pycml.mathml_cn.evaluate(), CellMLToNektar.pycml.mathml_ci.evaluate(), CellMLToNektar.pycml.mathml_apply.evaluate(), CellMLToNektar.pycml.mathml_piecewise.evaluate(), CellMLToNektar.pycml.mathml_plus.evaluate(), CellMLToNektar.pycml.mathml_minus.evaluate(), CellMLToNektar.pycml.mathml_times.evaluate(), CellMLToNektar.pycml.mathml_divide.evaluate(), CellMLToNektar.pycml.mathml_exp.evaluate(), CellMLToNektar.pycml.mathml_ln.evaluate(), CellMLToNektar.pycml.mathml_log.evaluate(), CellMLToNektar.pycml.mathml_abs.evaluate(), CellMLToNektar.pycml.mathml_power.evaluate(), CellMLToNektar.pycml.mathml_root.evaluate(), CellMLToNektar.pycml.mathml_and.evaluate(), CellMLToNektar.pycml.mathml_or.evaluate(), CellMLToNektar.pycml.mathml_leq.evaluate(), CellMLToNektar.pycml.mathml_lt.evaluate(), CellMLToNektar.pycml.mathml_geq.evaluate(), CellMLToNektar.pycml.mathml_gt.evaluate(), CellMLToNektar.pycml.mathml_neq.evaluate(), CellMLToNektar.pycml.mathml_eq.evaluate(), CellMLToNektar.pycml.mathml_rem.evaluate(), CellMLToNektar.pycml.mathml_logbase.evaluate(), CellMLToNektar.pycml.mathml_degree.evaluate(), CellMLToNektar.pycml.mathml_sin.evaluate(), CellMLToNektar.pycml.mathml_cos.evaluate(), CellMLToNektar.pycml.mathml_tan.evaluate(), CellMLToNektar.pycml.mathml_arcsin.evaluate(), CellMLToNektar.pycml.mathml_arccos.evaluate(), CellMLToNektar.pycml.mathml_arctan.evaluate(), CellMLToNektar.pycml.element_base.rootNode(), Nektar::LibUtilities::Interpreter::ExpressionEvaluator::AnalyticExpression::definition< ScannerT >.variable, and CellMLToNektar.pycml.mathml_ci.variable().

◆ _rename()

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.

4274  def _rename(self, new_name=None):
4275  """Update the variable reference to use a canonical name."""
4276  self.xml_remove_child(unicode(self))
4277  if new_name is None:
4278  new_name = self.variable.fullname(cellml=True)
4279  self.xml_append(unicode(new_name))
4280  return
4281 

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

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

◆ _set_variable_obj()

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.

4244  def _set_variable_obj(self, var):
4245  """Set the variable object referred to by this element."""
4246  self._cml_variable = var
4247 

References CellMLToNektar.pycml.mathml_ci._cml_variable.

◆ _unset_cached_links()

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.

Reimplemented from CellMLToNektar.pycml.mathml.

Definition at line 4226 of file pycml.py.

4226  def _unset_cached_links(self, elt=None):
4227  """Forget cached component and variable references in this MathML tree.
4228 
4229  Used by partial evaluator when moving maths to a new component, and by
4230  simulation protocols.
4231  """
4232  self._cml_variable = None
4233  super(mathml_ci, self)._unset_cached_links()
4234 

References CellMLToNektar.pycml.mathml_ci._cml_variable.

Referenced by CellMLToNektar.pycml.mathml._unset_cached_links().

◆ classify_variables()

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 4352 of file pycml.py.

4353  needs_special_treatment=lambda n: None):
4354  """Classify variables in this expression according to how they are used.
4355 
4356  For ci elements we just return a set containing the referenced variable
4357  as the single dependency. If dependencies_only is False, we also mark
4358  the variable as used.
4359  """
4360  var = self.variable
4361  if not dependencies_only:
4362  var._used()
4363  return set([var])
4364 

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

◆ create_new()

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.

4366  def create_new(elt, variable_name):
4367  """Create a new <ci> element with the given variable name."""
4368  new_elt = elt.xml_create_element(u'ci', NSS[u'm'],
4369  content=unicode(variable_name))
4370  return new_elt
4371 
def create_new(parent, name, bases, add_to_parent=False, standard=False)
Definition: pycml.py:2912

◆ evaluate()

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.

4259  def evaluate(self):
4260  """
4261  Evaluate this expression by returning the value of the
4262  variable it represents.
4263  """
4264  return self.variable.get_value()
4265 

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

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

◆ get_units()

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.

4248  def get_units(self, return_set=True):
4249  """Return the units of the variable represented by this element."""
4250  if not self._cml_units:
4251  self._cml_units = UnitsSet([self.component.get_units_by_name(self.variable.units)],
4252  expression=self)
4253  if not return_set:
4254  u = self._cml_units.extract()
4255  else:
4256  u = self._cml_units
4257  return u
4258 
def get_units_by_name(self, uname)
Definition: pycml.py:2725
def extract(self, check_equality=False)
Definition: pycml.py:2657

References CellMLToNektar.pycml.cellml_model._cml_units, CellMLToNektar.pycml.cellml_component._cml_units, CellMLToNektar.pycml.mathml_units_mixin_tokens._cml_units, CellMLToNektar.pycml.mathml_units_mixin_container._cml_units, CellMLToNektar.pycml.mathml_cn._cml_units, CellMLToNektar.pycml.mathml_ci._cml_units, CellMLToNektar.pycml.mathml_apply._cml_units, CellMLToNektar.pycml.mathml_piecewise._cml_units, CellMLToNektar.pycml.cellml_variable.component, CellMLToNektar.pycml.mathml.component, CellMLToNektar.pycml.extract(), CellMLToNektar.pycml.get_units_by_name(), Nektar::LibUtilities::Interpreter::ExpressionEvaluator::AnalyticExpression::definition< ScannerT >.variable, and CellMLToNektar.pycml.mathml_ci.variable().

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().

◆ variable()

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.

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

References CellMLToNektar.pycml.mathml_ci._cml_variable, CellMLToNektar.pycml.mathml_ci._rename(), CellMLToNektar.pycml.cellml_variable.component, and CellMLToNektar.pycml.mathml.component.

Referenced by CellMLToNektar.pycml.mathml_ci._get_binding_time(), CellMLToNektar.pycml.mathml_ci._reduce(), CellMLToNektar.pycml.mathml_ci._rename(), CellMLToNektar.pycml.mathml_ci.classify_variables(), CellMLToNektar.pycml.mathml_ci.evaluate(), and CellMLToNektar.pycml.mathml_ci.get_units().

Member Data Documentation

◆ _cml_units

CellMLToNektar.pycml.mathml_ci._cml_units
private

◆ _cml_variable

CellMLToNektar.pycml.mathml_ci._cml_variable
private