Nektar++
Public Member Functions | Private Member Functions | List of all members
CellMLToNektar.pycml.mathml_constructor Class Reference
Inheritance diagram for CellMLToNektar.pycml.mathml_constructor:
[legend]

Public Member Functions

def __init__ (self)
 
- Public Member Functions inherited from CellMLToNektar.pycml.mathml
def __init__ (self)
 
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 __init__ (self)
 
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)
 

Private Member Functions

def _tree_complexity (self, elt, **kw)
 
def _get_element_binding_time (self, elt)
 
def _get_element_units (self, elt, return_set=True)
 
def _reduce_elt (self, elt)
 
def _eval_self (self)
 
def _update_usage_counts (self, expr, remove=False)
 

Additional Inherited Members

- Static Public Member Functions inherited from CellMLToNektar.pycml.mathml
def clone (expr)
 
- Public Attributes inherited from CellMLToNektar.pycml.element_base
 xml_attributes
 
- Properties inherited from CellMLToNektar.pycml.mathml
 component = property(get_component)
 

Detailed Description

Base class for MathML constructor elements, e.g. apply and piecewise.

Definition at line 4000 of file pycml.py.

Constructor & Destructor Documentation

◆ __init__()

def CellMLToNektar.pycml.mathml_constructor.__init__ (   self)

Reimplemented from CellMLToNektar.pycml.mathml.

Reimplemented in CellMLToNektar.pycml.mathml_apply, and CellMLToNektar.pycml.mathml_piecewise.

Definition at line 4004 of file pycml.py.

4004 def __init__(self):
4005 super(mathml_constructor, self).__init__()
4006 return
4007

References CellMLToNektar.pycml.mathml_constructor.__init__().

Referenced by CellMLToNektar.pycml.mathml_constructor.__init__().

Member Function Documentation

◆ _eval_self()

def CellMLToNektar.pycml.mathml_constructor._eval_self (   self)
private
Evaluate self and return <cn>, <true> or <false>, as appropriate.

Definition at line 4108 of file pycml.py.

4108 def _eval_self(self):
4109 """Evaluate self and return <cn>, <true> or <false>, as appropriate."""
4110 value = self.evaluate()
4111 if value is True:
4112 new_elt = self.xml_create_element(u'true', NSS[u'm'])
4113 elif value is False:
4114 new_elt = self.xml_create_element(u'false', NSS[u'm'])
4115 else:
4116 # Add a new <units> element to the document if needed
4117 attrs = self._ensure_units_exist()
4118 new_elt = self.xml_create_element(u'cn', NSS[u'm'],
4119 content=unicode("%.17g" % value),
4120 attributes=attrs)
4121 return new_elt
4122

References CellMLToNektar.pycml.mathml._ensure_units_exist(), 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(), and CellMLToNektar.pycml.mathml_arctan.evaluate().

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

◆ _get_element_binding_time()

def CellMLToNektar.pycml.mathml_constructor._get_element_binding_time (   self,
  elt 
)
private
Helper method to get the binding time of a MathML element.

Definition at line 4057 of file pycml.py.

4057 def _get_element_binding_time(self, elt):
4058 """Helper method to get the binding time of a MathML element."""
4059 if hasattr(elt, '_get_binding_time'):
4060 # Call the element's method
4061 return elt._get_binding_time()
4062 elif elt.localName in [u'true', u'false', u'exponentiale', u'pi']:
4063 return BINDING_TIMES.static
4064 else:
4065 raise EvaluationError("Don't know how to compute binding time"
4066 " of element " + elt.localName)
4067

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

◆ _get_element_units()

def CellMLToNektar.pycml.mathml_constructor._get_element_units (   self,
  elt,
  return_set = True 
)
private
Helper method to get the units of a MathML element.

Definition at line 4068 of file pycml.py.

4068 def _get_element_units(self, elt, return_set=True):
4069 """Helper method to get the units of a MathML element."""
4070 if hasattr(elt, 'get_units'):
4071 # Element has a method to get its units, so call it
4072 u = elt.get_units()
4073 elif hasattr(elt, '_cml_units') and elt._cml_units:
4074 # Units have been cached
4075 u = elt._cml_units
4076 else:
4077 # Let's figure it out ourselves...
4078 if elt.localName in [u'false', u'true']:
4079 u = UnitsSet(
4080 [self.component.get_units_by_name('cellml:boolean')],
4081 expression=elt)
4082 elif elt.localName in [u'notanumber', u'pi', u'infinity',
4083 u'exponentiale']:
4084 u = UnitsSet(
4085 [self.component.get_units_by_name('dimensionless')],
4086 expression=elt)
4087 else:
4088 # Unknown or unexpected element
4089 raise UnitsError(self, u''.join([
4090 u'Unsupported element "', elt.localName, '".']))
4091 if not return_set:
4092 u = u.extract()
4093 return u
4094
def get_units_by_name(self, uname)
Definition: pycml.py:2725

References CellMLToNektar.pycml.cellml_variable.component, CellMLToNektar.pycml.mathml.component, and CellMLToNektar.pycml.get_units_by_name().

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

◆ _reduce_elt()

def CellMLToNektar.pycml.mathml_constructor._reduce_elt (   self,
  elt 
)
private
Try to reduce the given element.

Call the _reduce method on elt, if it has one.
If not, do nothing (we assume elt cannot be reduced).

Definition at line 4095 of file pycml.py.

4095 def _reduce_elt(self, elt):
4096 """Try to reduce the given element.
4097
4098 Call the _reduce method on elt, if it has one.
4099 If not, do nothing (we assume elt cannot be reduced).
4100 """
4101 if hasattr(elt, '_reduce') and callable(elt._reduce):
4102 elt._reduce()
4103 else:
4104 DEBUG('partial-evaluator', "Don't know how to reduce",
4105 elt.localName)
4106 return
4107
def DEBUG(facility, *args)
Definition: utilities.py:95

References CellMLToNektar.utilities.DEBUG().

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

◆ _tree_complexity()

def CellMLToNektar.pycml.mathml_constructor._tree_complexity (   self,
  elt,
**  kw 
)
private
Calculate a rough estimate of the computation time for
evaluating the given element.

If lookup_tables is True, then assume we're using lookup tables
where possible.
If store_result is True, the complexity is saved to the
_cml_complexity attribute.
If algebraic is True, the complexity is calculated as a dictionary,
mapping node types to the number of occurences of that type.

Definition at line 4008 of file pycml.py.

4008 def _tree_complexity(self, elt, **kw):
4009 """
4010 Calculate a rough estimate of the computation time for
4011 evaluating the given element.
4012
4013 If lookup_tables is True, then assume we're using lookup tables
4014 where possible.
4015 If store_result is True, the complexity is saved to the
4016 _cml_complexity attribute.
4017 If algebraic is True, the complexity is calculated as a dictionary,
4018 mapping node types to the number of occurences of that type.
4019 """
4020 kw['lookup_tables'] = kw.get('lookup_tables', False)
4021 kw['store_result'] = kw.get('store_result', False)
4022 kw['algebraic'] = kw.get('algebraic', False)
4023 if kw['algebraic']:
4024 ac = {}
4025 if kw['lookup_tables'] and \
4026 elt.getAttributeNS(NSS['lut'], u'possible', u'no') == u'yes':
4027 # This elt will be replaced by a lookup table
4028 if hasattr(self.rootNode, 'num_lookup_tables'):
4029 # Count the number of used lookup tables
4030 self.rootNode.num_lookup_tables += 1
4031 # Cost per table: 2 lookups, 2 +, -, *, 3 ci
4032 if kw['algebraic']:
4033 ac['lookup'] = 2
4034 ac['op'] = 3
4035 ac['times'] = 1
4036 ac['variable'] = 3
4037 else:
4038 ac = 2*1 + 2*1 + 1 + 1 + 3*0.7
4039 elif hasattr(elt, 'tree_complexity') \
4040 and callable(elt.tree_complexity):
4041 ac = elt.tree_complexity(**kw)
4042 elif elt.localName in ['true', 'false', 'cn', 'exponentiale', 'pi']:
4043 if kw['algebraic']: ac['constant'] = 1
4044 else: ac = 0.5
4045 elif elt.localName == 'ci':
4046 if kw['algebraic']: ac['variable'] = 1
4047 else: ac = 0.7
4048 elif elt.localName in ['degree', 'logbase']:
4049 ac = self._tree_complexity(child_i(elt, 1), **kw)
4050 else:
4051 raise EvaluationError("Don't know complexity of element " +
4052 elt.localName)
4053 if kw['store_result'] and isinstance(elt, mathml):
4054 elt._cml_complexity = ac
4055 return ac
4056
def child_i(elt, i)
Definition: pycml.py:3449

References CellMLToNektar.pycml.mathml_constructor._tree_complexity(), CellMLToNektar.pycml.child_i(), and CellMLToNektar.pycml.element_base.rootNode().

Referenced by CellMLToNektar.pycml.mathml_constructor._tree_complexity(), CellMLToNektar.pycml.mathml_apply.tree_complexity(), and CellMLToNektar.pycml.mathml_piecewise.tree_complexity().

◆ _update_usage_counts()

def CellMLToNektar.pycml.mathml_constructor._update_usage_counts (   self,
  expr,
  remove = False 
)
private
Update usage counts of variables used in the given expression.

By default, increment the usage count of any variable occuring
in a <ci> element within expr.  If remove is set to True,
then decrement the usage counts instead.

Definition at line 4123 of file pycml.py.

4123 def _update_usage_counts(self, expr, remove=False):
4124 """Update usage counts of variables used in the given expression.
4125
4126 By default, increment the usage count of any variable occuring
4127 in a <ci> element within expr. If remove is set to True,
4128 then decrement the usage counts instead.
4129 """
4130 if isinstance(expr, mathml_ci):
4131 if remove:
4132 expr.variable._decrement_usage_count()
4133 else:
4134 raise NotImplementedError("_update_usage_counts currently only reliable for remove=True")
4135 expr.variable._used()
4136 elif isinstance(expr, mathml_apply) and isinstance(expr.operator(),
4137 mathml_diff):
4138 # TODO: Check if this is a suitable handling of ODEs on a RHS
4139 # It matches the current behaviour in apply.classify_variables.
4140 pass
4141 else:
4142 for e in self.xml_element_children(expr):
4143 self._update_usage_counts(e, remove=remove)
4144

References CellMLToNektar.pycml.mathml_constructor._update_usage_counts().

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