Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Private Member Functions | List of all members
CellMLToNektar.pycml.mathml_units_mixin Class Reference

MathML elements #. More...

Inheritance diagram for CellMLToNektar.pycml.mathml_units_mixin:
Inheritance graph
[legend]
Collaboration diagram for CellMLToNektar.pycml.mathml_units_mixin:
Collaboration graph
[legend]

Private Member Functions

def _add_units_conversion
 
def _set_element_in_units
 

Detailed Description

MathML elements #.

Base class for units mixin classes.

Definition at line 3469 of file pycml.py.

Member Function Documentation

def CellMLToNektar.pycml.mathml_units_mixin._add_units_conversion (   self,
  expr,
  defn_units,
  to_units,
  no_act = False 
)
private
Add mathematics for an explicit units conversion.

Wraps expr in the expression
m[to_units/defn_units]*(expr-o1[defn_units]) + o2[to_units].

Definition at line 3471 of file pycml.py.

References CellMLToNektar.pycml.extract(), and CellMLToNektar.pycml.simplify().

Referenced by CellMLToNektar.pycml.mathml_units_mixin._set_element_in_units(), and CellMLToNektar.pycml.mathml_units_mixin_tokens._set_in_units().

3472  def _add_units_conversion(self, expr, defn_units, to_units, no_act=False):
3473  """Add mathematics for an explicit units conversion.
3474 
3475  Wraps expr in the expression
3476  m[to_units/defn_units]*(expr-o1[defn_units]) + o2[to_units].
3477  """
3478 # print '_add_units_conv for', element_xpath(expr), 'from', defn_units.description(), 'to', to_units.description()
3479  if hasattr(expr.model, '_cml_special_units_converter') and not defn_units.dimensionally_equivalent(to_units):
3480  # This may be a special conversion case defined by a functional curation protocol
3481  if no_act:
3482  model._cml_conversions_needed = True
3483  return
3484  else:
3485  expr = expr.model._cml_special_units_converter(expr, defn_units, to_units)
3486 # print 'post special, expr units=', expr.get_units().description(), 'm=', expr.get_units().extract().expand().simplify().get_multiplicative_factor()
3487 # print 'orig defn_units=', defn_units.description(), 'm=', defn_units.expand().simplify().get_multiplicative_factor()
3488 # print 'orig to_units=', to_units.description(), 'm=', to_units.expand().simplify().get_multiplicative_factor()
3489  try:
3490  defn_units = expr.get_units().extract(check_equality=True)
3491  except:
3492  print 'ouch', expr.xml()
3493  for u in expr.get_units():
3494  print u.description(), u.get_multiplier(), expr.get_units()._get_sources(u)
3495  raise
3496  defn_units_exp = defn_units.expand().simplify()
3497  to_units_exp = to_units.expand().simplify()
3498  # Conversion factor
3499  m = (defn_units_exp.get_multiplicative_factor() / to_units_exp.get_multiplicative_factor())
3500  # Replace expr by m[to_units/defn_units]*(expr-o1[defn_units]) + o2[to_units]
3501  orig_expr, parent = expr, expr.xml_parent
3502  dummy = expr.xml_create_element(u'dummy', NSS[u'm'])
3503  model = expr.model # So we still have a reference after the next line
3504  parent.replace_child(expr, dummy) # Mark where to put the new elt
3505  if defn_units_exp.get_offset() != 0:
3506  # Create expr-o1 expression
3507  uattr = orig_expr._ensure_units_exist(defn_units, no_act=no_act)
3508  new_expr = mathml_apply.create_new(expr, u'minus',
3509  [expr, (unicode(defn_units_exp.get_offset()), uattr)])
3510  new_expr._cml_units = defn_units
3511  expr = new_expr
3512  if m != 1:
3513  quotient_units = to_units.quotient(defn_units)
3514  # Add units element to model if needed
3515  uattr = orig_expr._ensure_units_exist(quotient_units, no_act=no_act)
3516  # Create m*expr expression
3517  new_expr = mathml_apply.create_new(expr, u'times', [(unicode(m), uattr), expr])
3518  new_expr._cml_units = to_units
3519  expr = new_expr
3520  if to_units_exp.get_offset() != 0:
3521  # Create expr+o2 expression
3522  uattr = orig_expr._ensure_units_exist(to_units, no_act=no_act)
3523  new_expr = mathml_apply.create_new(expr, u'plus',
3524  [expr, (unicode(to_units_exp.get_offset()), uattr)])
3525  new_expr._cml_units = to_units
3526  expr = new_expr
3527  # Note that the model needed conversions
3528  if expr is not orig_expr:
3529  model._cml_conversions_needed = True
3530  if no_act:
3531  expr = orig_expr
3532  parent.replace_child(dummy, expr)
3533  return
def CellMLToNektar.pycml.mathml_units_mixin._set_element_in_units (   self,
  elt,
  units,
  no_act = False 
)
private
Try to set the units of the given element.

Generates a debug message if this isn't possible.

Definition at line 3534 of file pycml.py.

References CellMLToNektar.pycml.mathml_units_mixin._add_units_conversion(), and CellMLToNektar.utilities.DEBUG().

Referenced by CellMLToNektar.pycml.mathml_units_mixin_set_operands._set_in_units(), CellMLToNektar.pycml.mathml_units_mixin_equalise_operands._set_in_units(), CellMLToNektar.pycml.mathml_units_mixin_choose_nearest._set_in_units(), CellMLToNektar.pycml.mathml_units_mixin_container._set_in_units(), CellMLToNektar.pycml.mathml_apply._set_in_units(), CellMLToNektar.pycml.mathml_piecewise._set_in_units(), CellMLToNektar.pycml.mathml_power._set_in_units(), CellMLToNektar.pycml.mathml_root._set_in_units(), and CellMLToNektar.pycml.mathml_eq._set_in_units().

3535  def _set_element_in_units(self, elt, units, no_act=False):
3536  """Try to set the units of the given element.
3537 
3538  Generates a debug message if this isn't possible.
3539  """
3540  if hasattr(elt, '_set_in_units') and callable(elt._set_in_units):
3541  elt._set_in_units(units, no_act)
3542  elif elt.localName in [u'false', u'true']:
3543  boolean = self.component.get_units_by_name('cellml:boolean')
3544  if boolean is not units:
3545  # TODO: *blink* this should never happen
3546  self._add_units_conversion(elt, boolean, units, no_act)
3547  elif elt.localName in [u'notanumber', u'pi', u'infinity', u'exponentiale']:
3548  dimensionless = self.component.get_units_by_name('dimensionless')
3549  if dimensionless is not units:
3550  # TODO: *blink* this should never happen
3551  self._add_units_conversion(elt, dimensionless, units, no_act)
3552  else:
3553  DEBUG('validator',
3554  'Cannot set units (to', units.description(), ') for element', elt.localName)
3555  return