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

Public Member Functions

def __init__
 
def add_all_info
 
def add_dt_reference
 
def add_transmembrane_potential_name
 
def add_linearised_odes
 
def add_jacobian_matrix
 
def use_canonical_variable_names
 
def add_membrane_ionic_current
 
def add_linear_ode_update_equations
 
def add_variable_links
 
def do_binding_time_analysis
 
def has_modifiable_mathematics
 
def get_modifiable_mathematics
 
def get_linearised_odes
 
def create_dt
 
def get_dt
 

Private Member Functions

def _fix_jac_var_name
 
def _check_state_var_units_conversions
 
def _process_mathematics
 
def _add_variable_links
 
def _get_variable
 
def _get_special_variable
 
def _get_special_component
 

Private Attributes

 _model
 
 _solver_info
 
 _component
 
 _dt
 

Static Private Attributes

tuple _jac_temp_re = re.compile(r't[0-9]+')
 

Detailed Description

Add information for specialised translator classes into a model.

Definition at line 1359 of file translators.py.

Constructor & Destructor Documentation

def CellMLToNektar.translators.SolverInfo.__init__ (   self,
  model,
  force = False 
)
Add information for the solvers as XML.

The Jacobian and linearity analyses store their results in
Python data structures as attributes of this object.
Transcribe these into XML in a child <solver_info> element.

If any of these elements exist in the model they will be left
unaltered, unless force is set to True.

This constructor just sets up the container element; call one
of the add_* methods to actually add the information to it.

Definition at line 1361 of file translators.py.

1362  def __init__(self, model, force=False):
1363  """Add information for the solvers as XML.
1364 
1365  The Jacobian and linearity analyses store their results in
1366  Python data structures as attributes of this object.
1367  Transcribe these into XML in a child <solver_info> element.
1368 
1369  If any of these elements exist in the model they will be left
1370  unaltered, unless force is set to True.
1371 
1372  This constructor just sets up the container element; call one
1373  of the add_* methods to actually add the information to it.
1374  """
1375  self._model = model
1376  if force and hasattr(model, u'solver_info'):
1377  model.xml_remove_child(model.solver_info)
1378  if hasattr(model, u'solver_info'):
1379  solver_info = model.solver_info
1380  else:
1381  solver_info = model.xml_create_element(u'solver_info', NSS[u'solver'])
1382  model.xml_append(solver_info)
1383  self._solver_info = solver_info
1384  self._component = None
1385  self._dt = None

Member Function Documentation

def CellMLToNektar.translators.SolverInfo._add_variable_links (   self,
  elt 
)
private
Recursively link ci elements in the given XML tree to cellml_variable objects.

Also sets component links: for ci elements, to the component containing the linked
variable, and for cn elements, to the first component in the model.

Definition at line 1743 of file translators.py.

References CellMLToNektar.translators.SolverInfo._add_variable_links(), and CellMLToNektar.translators.SolverInfo._get_variable().

Referenced by CellMLToNektar.translators.SolverInfo._add_variable_links(), CellMLToNektar.translators.SolverInfo.add_linear_ode_update_equations(), and CellMLToNektar.translators.SolverInfo.add_variable_links().

1744  def _add_variable_links(self, elt):
1745  """Recursively link ci elements in the given XML tree to cellml_variable objects.
1746 
1747  Also sets component links: for ci elements, to the component containing the linked
1748  variable, and for cn elements, to the first component in the model.
1749  """
1750  if isinstance(elt, mathml_ci):
1751  var = self._get_variable(unicode(elt))
1752  elt._cml_variable = var
1753  elt._cml_component = var.component
1754  elif isinstance(elt, mathml_cn):
1755  # Fake a component, since it doesn't really have one
1756  elt._cml_component = elt.model.component
1757  elif hasattr(elt, 'xml_children'):
1758  for child in elt.xml_children:
1759  self._add_variable_links(child)
def CellMLToNektar.translators.SolverInfo._check_state_var_units_conversions (   self)
private
Check if any Jacobian entries need to be altered because the units of state variables have changed.

If any variable considered a state variable by the Jacobian is now of type Computed then it has been
converted.  We figure out the conversion factor, update the Jacobian to reference the new state variable,
and units-convert the derivative.

Definition at line 1604 of file translators.py.

References CellMLToNektar.translators.SolverInfo._get_variable(), and CellMLToNektar.translators.SolverInfo._solver_info.

Referenced by CellMLToNektar.translators.SolverInfo.add_variable_links().

1606  """Check if any Jacobian entries need to be altered because the units of state variables have changed.
1607 
1608  If any variable considered a state variable by the Jacobian is now of type Computed then it has been
1609  converted. We figure out the conversion factor, update the Jacobian to reference the new state variable,
1610  and units-convert the derivative.
1611  """
1612  if not hasattr(self._solver_info, u'jacobian'):
1613  return
1614  # Helper methods
1615  def set_var_values(elt, vars=None):
1616  """Fake all variables appearing in the given expression being set to 1.0, and return them."""
1617  if vars is None:
1618  vars = []
1619  if isinstance(elt, mathml_ci):
1620  elt.variable.set_value(1.0)
1621  vars.append(elt.variable)
1622  else:
1623  for child in getattr(elt, 'xml_children', []):
1624  set_var_values(child, vars)
1625  return vars
1626  # Find any converted state variables
1627  converted_state_vars = set()
1628  for entry in getattr(self._solver_info.jacobian, u'entry', []):
1629  var = self._get_variable(entry.var_i)
1630  if var.get_type() == VarTypes.Computed:
1631  converted_state_vars.add(var)
1632  if not converted_state_vars:
1633  return
1634  # Figure out the conversion factor in each case
1635  state_var_map = {}
1636  for var in converted_state_vars:
1637  defn = var.get_dependencies()[0]
1638  defn_vars = set_var_values(defn.eq.rhs)
1639  assert len(defn_vars) == 1, "Unexpected form of units conversion expression found"
1640  factor = defn.eq.rhs.evaluate()
1641  state_var_map[var] = (defn_vars[0], factor)
1642  defn_vars[0].unset_values()
1643  # Apply the conversion to relevant Jacobian entries
1644  for entry in getattr(self._solver_info.jacobian, u'entry', []):
1645  factor = 1
1646  var_i = self._get_variable(entry.var_i)
1647  if var_i in converted_state_vars:
1648  var_i, factor_i = state_var_map[var_i]
1649  var_i = var_i.get_source_variable(recurse=True)
1650  entry.var_i = unicode(var_i.fullname())
1651  factor /= factor_i
1652  var_j = self._get_variable(entry.var_j)
1653  if var_j in converted_state_vars:
1654  var_j, factor_j = state_var_map[var_j]
1655  var_j = var_j.get_source_variable(recurse=True)
1656  entry.var_j = unicode(var_j.fullname())
1657  factor *= factor_j
1658  if factor != 1:
1659  # Replace rhs with rhs * factor
1660  rhs = list(entry.math.xml_element_children())[0]
1661  entry.math.safe_remove_child(rhs)
1662  new_rhs = mathml_apply.create_new(entry, 'times', [(factor, 'dimensionless'), rhs])
1663  entry.math.xml_append(new_rhs)
def CellMLToNektar.translators.SolverInfo._fix_jac_var_name (   self,
  vname 
)
private
If PE will be performed on a model with a single component, then we'll need full names in
the variable attributes.

Definition at line 1458 of file translators.py.

Referenced by CellMLToNektar.translators.SolverInfo.add_jacobian_matrix().

1459  def _fix_jac_var_name(self, vname):
1460  """
1461  If PE will be performed on a model with a single component, then we'll need full names in
1462  the variable attributes.
1463  """
1464  if vname[:4] == 'var_' and len(self._model.component) == 1 and not self._model.component.ignore_component_name:
1465  name = unicode('var_' + self._model.component.name + '__' + vname[4:])
1466  else:
1467  name = unicode(vname)
1468  return name
def CellMLToNektar.translators.SolverInfo._get_special_component (   self)
private
Get or create a special component for containing special variables.

Definition at line 1798 of file translators.py.

References CellMLToNektar.translators.SolverInfo._component, and CellMLToNektar.translators.SolverInfo._model.

Referenced by CellMLToNektar.translators.SolverInfo._get_special_variable(), CellMLToNektar.translators.SolverInfo.add_jacobian_matrix(), and CellMLToNektar.translators.SolverInfo.add_linearised_odes().

1799  def _get_special_component(self):
1800  """Get or create a special component for containing special variables."""
1801  if not self._component:
1802  self._component = cellml_component.create_new(self._model, u'')
1803  self._model._add_component(self._component, special=True)
1804  return self._component
1805 
1806 
def CellMLToNektar.translators.SolverInfo._get_special_variable (   self,
  varname,
  ptype = VarTypes.Unknown 
)
private
Get or create a special variable object that doesn't really exist in the model.

Definition at line 1787 of file translators.py.

References CellMLToNektar.translators.SolverInfo._get_special_component(), and CellMLToNektar.translators.SolverInfo._model.

Referenced by CellMLToNektar.translators.SolverInfo._get_variable(), and CellMLToNektar.translators.SolverInfo.get_dt().

1788  def _get_special_variable(self, varname, ptype=VarTypes.Unknown):
1789  """Get or create a special variable object that doesn't really exist in the model."""
1790  comp = self._get_special_component()
1791  try:
1792  var = comp.get_variable_by_name(varname)
1793  except KeyError:
1794  var = cellml_variable.create_new(self._model, varname, u'dimensionless')
1795  comp._add_variable(var)
1796  var._set_type(ptype)
1797  return var
def CellMLToNektar.translators.SolverInfo._get_variable (   self,
  varname 
)
private
Return the variable in the model with name varname.

Definition at line 1761 of file translators.py.

References CellMLToNektar.translators.SolverInfo._get_special_variable(), CellMLToNektar.translators.SolverInfo._model, and CellMLToNektar.translators.SolverInfo.get_dt().

Referenced by CellMLToNektar.translators.SolverInfo._add_variable_links(), CellMLToNektar.translators.SolverInfo._check_state_var_units_conversions(), and CellMLToNektar.translators.SolverInfo.use_canonical_variable_names().

1762  def _get_variable(self, varname):
1763  """Return the variable in the model with name varname."""
1764  try:
1765  if varname == 'delta_t':
1766  # Special case for the timestep in ComputeJacobian and elsewhere
1767  var = self.get_dt()
1768  elif self._jac_temp_re.match(varname):
1769  var = self._get_special_variable(varname, VarTypes.Unknown)
1770  else:
1771  var = cellml_variable.get_variable_object(self._model, varname)
1772  except KeyError:
1773  raise ValueError("Cannot find variable '%s' referenced in SolverInfo" % varname)
1774  return var
def CellMLToNektar.translators.SolverInfo._process_mathematics (   self,
  func 
)
private
Apply func to each top-level mathematical construct in the solver info blocks.

func must be able to accept mathml_piecewise, mathml_apply, mathml_ci and mathml_cn elements.

Definition at line 1671 of file translators.py.

References CellMLToNektar.translators.SolverInfo._solver_info.

Referenced by CellMLToNektar.translators.SolverInfo.add_variable_links(), and CellMLToNektar.translators.SolverInfo.do_binding_time_analysis().

1672  def _process_mathematics(self, func):
1673  """Apply func to each top-level mathematical construct in the solver info blocks.
1674 
1675  func must be able to accept mathml_piecewise, mathml_apply, mathml_ci and mathml_cn elements.
1676  """
1677  solver_info = self._solver_info
1678  # Jacobian
1679  if hasattr(solver_info, u'jacobian'):
1680  if hasattr(solver_info.jacobian, u'math'):
1681  for elt in solver_info.jacobian.math.apply:
1682  func(elt)
1683  for entry in solver_info.jacobian.entry:
1684  for elt in entry.math.xml_element_children():
1685  func(elt)
1686  # Linearised ODEs
1687  if hasattr(solver_info, u'linear_odes'):
1688  for math in solver_info.linear_odes.math:
1689  for elt in math.xml_element_children():
1690  func(elt)
def CellMLToNektar.translators.SolverInfo.add_all_info (   self)
Actually add the info.

Definition at line 1386 of file translators.py.

References CellMLToNektar.translators.SolverInfo.add_dt_reference(), CellMLToNektar.translators.SolverInfo.add_jacobian_matrix(), CellMLToNektar.translators.SolverInfo.add_linearised_odes(), CellMLToNektar.translators.SolverInfo.add_membrane_ionic_current(), and CellMLToNektar.translators.SolverInfo.add_transmembrane_potential_name().

1387  def add_all_info(self):
1388  """Actually add the info."""
1391  self.add_linearised_odes()
1392  self.add_jacobian_matrix()
1393  self.add_dt_reference()
def CellMLToNektar.translators.SolverInfo.add_dt_reference (   self)
Add a reference to the variable representing dt.

Definition at line 1394 of file translators.py.

References CellMLToNektar.translators.SolverInfo._model, CellMLToNektar.translators.SolverInfo._solver_info, and CellMLToNektar.translators.SolverInfo.get_dt().

Referenced by CellMLToNektar.translators.SolverInfo.add_all_info().

1395  def add_dt_reference(self):
1396  """Add a reference to the variable representing dt."""
1397  solver_info = self._solver_info
1398  model = self._model
1399  if not hasattr(solver_info, u'dt'):
1400  dt = self.get_dt()
1401  elt = model.xml_create_element(u'dt', NSS[u'solver'], content=dt.fullname(cellml=True))
1402  solver_info.xml_append(elt)
1403  self._model._add_sorted_assignment(dt)
def CellMLToNektar.translators.SolverInfo.add_jacobian_matrix (   self)
Jacobian matrix elements.

Structure looks like:
<jacobian>
    [<math> assignments of common sub-terms </math>]
    <entry var_i='varname' var_j='varname'>
<math> apply|cn|ci ...</math>
    </entry>
</jacobian>

Definition at line 1469 of file translators.py.

References CellMLToNektar.translators.SolverInfo._fix_jac_var_name(), CellMLToNektar.translators.SolverInfo._get_special_component(), CellMLToNektar.translators.SolverInfo._model, CellMLToNektar.translators.SolverInfo._solver_info, and CellMLToNektar.pycml.amara_parse_cellml().

Referenced by CellMLToNektar.translators.SolverInfo.add_all_info().

1470  def add_jacobian_matrix(self):
1471  """Jacobian matrix elements.
1472 
1473  Structure looks like:
1474  <jacobian>
1475  [<math> assignments of common sub-terms </math>]
1476  <entry var_i='varname' var_j='varname'>
1477  <math> apply|cn|ci ...</math>
1478  </entry>
1479  </jacobian>
1480  """
1481  solver_info = self._solver_info
1482  model = self._model
1483  if model._cml_jacobian and model._cml_jacobian_full:
1484  jac = model._cml_jacobian[1]
1485  else:
1486  # Old-style partial jacobian, or no jacobian
1487  jac = model._cml_jacobian
1488  if not hasattr(solver_info, u'jacobian') and jac:
1489  jac_elt = model.xml_create_element(u'jacobian', NSS[u'solver'])
1490  solver_info.xml_append(jac_elt)
1491 
1492  if model._cml_jacobian_full:
1493  # There may be temporaries
1494  temporaries = model._cml_jacobian[0]
1495  if temporaries:
1496  jac_elt.xml_append(amara_parse_cellml(temporaries).math)
1497 
1498  jac_vars = jac.keys()
1499  jac_vars.sort() # Will sort by variable name
1500  for v_i, v_j in jac_vars:
1501  # Add (i,j)-th entry
1502  attrs = {u'var_i': self._fix_jac_var_name(v_i),
1503  u'var_j': self._fix_jac_var_name(v_j)}
1504  entry = model.xml_create_element(u'entry', NSS[u'solver'], attributes=attrs)
1505  jac_elt.xml_append(entry)
1506  entry_doc = amara_parse_cellml(jac[(v_i, v_j)].xml())
1507  entry.xml_append(entry_doc.math)
1508  # Ensure that the model has a special component
1509  self._get_special_component()
1510  return
def amara_parse_cellml
Definition: pycml.py:191
def CellMLToNektar.translators.SolverInfo.add_linear_ode_update_equations (   self)
Add the update equations for the linear ODEs.

A linear ODE has the form du/dt = g+h.u where g & h are not functions of u.  The
update expression then looks like u = (u + g.dt)/(1 - h.dt).

This replaces the linear_odes block with the structure:
<linear_odes>
    <math>
<ci>u</ci>
<ci>t</ci>
<apply> <!-- (u + g.dt)/(1 - h.dt) --> </apply>
    </math>
    .
    .
    .
</linear_odes>

Definition at line 1547 of file translators.py.

References CellMLToNektar.translators.SolverInfo._add_variable_links(), CellMLToNektar.translators.SolverInfo._solver_info, and CellMLToNektar.translators.SolverInfo.get_linearised_odes().

1549  """Add the update equations for the linear ODEs.
1550 
1551  A linear ODE has the form du/dt = g+h.u where g & h are not functions of u. The
1552  update expression then looks like u = (u + g.dt)/(1 - h.dt).
1553 
1554  This replaces the linear_odes block with the structure:
1555  <linear_odes>
1556  <math>
1557  <ci>u</ci>
1558  <ci>t</ci>
1559  <apply> <!-- (u + g.dt)/(1 - h.dt) --> </apply>
1560  </math>
1561  .
1562  .
1563  .
1564  </linear_odes>
1565  """
1566  block = getattr(self._solver_info, u'linear_odes', None)
1567  dt = self._model.get_config().dt_variable.fullname() # was dt = u'delta_t'
1568  # Add the new equations
1569  for u, t, gh in self.get_linearised_odes():
1570  g, h = gh
1571  g.safe_remove_child(g, g.xml_parent)
1572  g_dt = mathml_apply.create_new(block, u'times', [g, dt])
1573  numer = mathml_apply.create_new(block, u'plus', [u.fullname(), g_dt])
1574  h.safe_remove_child(h, h.xml_parent)
1575  h_dt = mathml_apply.create_new(block, u'times', [h, dt])
1576  denom = mathml_apply.create_new(block, u'minus', [(u'1', u'dimensionless'), h_dt])
1577  eqn = mathml_apply.create_new(block, u'divide', [numer, denom])
1578  math = block.xml_create_element(u'math', NSS[u'm'])
1579  math.xml_append(mathml_ci.create_new(block, u.fullname()))
1580  math.xml_append(mathml_ci.create_new(block, t.fullname()))
1581  math.xml_append(eqn)
1582  block.xml_append(math)
1583  self._add_variable_links(math)
1584  # Remove the old equations (first math element)
1585  block.xml_remove_child(block.math)
def CellMLToNektar.translators.SolverInfo.add_linearised_odes (   self)
Linearised ODEs - where du/dt = g + hu (and g, h are not functions of u).

Structure looks like:
<linear_odes>
    <math>
<apply><eq/>
    <apply><diff/>
        <bvar><ci>t</ci></bvar>
        <ci>u</ci>
    </apply>
    <apply><plus/>
        g
        <apply><times/>
            h
            <ci>u</ci>
        </apply>
    </apply>
</apply>
.
.
.
    </math>
</linear_odes>

Definition at line 1414 of file translators.py.

References CellMLToNektar.translators.SolverInfo._get_special_component(), CellMLToNektar.translators.SolverInfo._model, and CellMLToNektar.translators.SolverInfo._solver_info.

Referenced by CellMLToNektar.translators.SolverInfo.add_all_info().

1415  def add_linearised_odes(self):
1416  """Linearised ODEs - where du/dt = g + hu (and g, h are not functions of u).
1417 
1418  Structure looks like:
1419  <linear_odes>
1420  <math>
1421  <apply><eq/>
1422  <apply><diff/>
1423  <bvar><ci>t</ci></bvar>
1424  <ci>u</ci>
1425  </apply>
1426  <apply><plus/>
1427  g
1428  <apply><times/>
1429  h
1430  <ci>u</ci>
1431  </apply>
1432  </apply>
1433  </apply>
1434  .
1435  .
1436  .
1437  </math>
1438  </linear_odes>
1439  """
1440  solver_info = self._solver_info
1441  model = self._model
1442  if not hasattr(solver_info, u'linear_odes') and model._cml_linear_update_exprs:
1443  odes_elt = model.xml_create_element(u'linear_odes', NSS[u'solver'])
1444  solver_info.xml_append(odes_elt)
1445  odes_math = model.xml_create_element(u'math', NSS[u'm'])
1446  odes_elt.xml_append(odes_math)
1447  linear_vars = model._cml_linear_update_exprs.keys()
1448  linear_vars.sort(key=lambda v: v.fullname())
1449  free_var = model._cml_free_var
1450  for var in linear_vars:
1451  g, h = model._cml_linear_update_exprs[var]
1452  hu = mathml_apply.create_new(model, u'times', [h, var.fullname()])
1453  rhs = mathml_apply.create_new(model, u'plus', [g, hu])
1454  odes_math.xml_append(mathml_diff.create_new(
1455  model, free_var.fullname(), var.fullname(), rhs))
1456  # Ensure that the model has a special component
1457  self._get_special_component()
def CellMLToNektar.translators.SolverInfo.add_membrane_ionic_current (   self)
Add ionic current information as XML for solvers to use.

Definition at line 1530 of file translators.py.

References CellMLToNektar.translators.SolverInfo._model, and CellMLToNektar.translators.SolverInfo._solver_info.

Referenced by CellMLToNektar.translators.SolverInfo.add_all_info().

1531  def add_membrane_ionic_current(self):
1532  """Add ionic current information as XML for solvers to use."""
1533  solver_info = self._solver_info
1534  model = self._model
1535  # The total ionic current. This relies on having a configuration store.
1536  if hasattr(model.xml_parent, '_cml_config') and not hasattr(solver_info, u'ionic_current'):
1537  conf = model.xml_parent._cml_config
1538  if conf.i_ionic_vars:
1539  ionic_elt = model.xml_create_element(u'ionic_current', NSS[u'solver'])
1540  # Adds each ionic var to the xml doc from the config store
1541  for var in conf.i_ionic_vars:
1542  varelt = model.xml_create_element(u'var', NSS[u'solver'],
1543  content=var.fullname())
1544  ionic_elt.xml_append(varelt)
1545  solver_info.xml_append(ionic_elt)
1546  return
def CellMLToNektar.translators.SolverInfo.add_transmembrane_potential_name (   self)
The name of the transmembrane potential.

Definition at line 1404 of file translators.py.

References CellMLToNektar.translators.SolverInfo._model, and CellMLToNektar.translators.SolverInfo._solver_info.

Referenced by CellMLToNektar.translators.SolverInfo.add_all_info().

1406  """The name of the transmembrane potential."""
1407  solver_info = self._solver_info
1408  model = self._model
1409  if not hasattr(solver_info, u'transmembrane_potential'):
1410  v_elt = model.xml_create_element(
1411  u'transmembrane_potential', NSS[u'solver'],
1412  content=model._cml_transmembrane_potential.fullname())
1413  solver_info.xml_append(v_elt)
def CellMLToNektar.translators.SolverInfo.add_variable_links (   self)
Link ci elements in the added XML to cellml_variable objects.

This analyses the names in the ci elements to determine which variable in
the model they refer to.

Definition at line 1586 of file translators.py.

References CellMLToNektar.translators.SolverInfo._add_variable_links(), CellMLToNektar.translators.SolverInfo._check_state_var_units_conversions(), CellMLToNektar.translators.SolverInfo._process_mathematics(), and CellMLToNektar.translators.SolverInfo._solver_info.

1587  def add_variable_links(self):
1588  """Link ci elements in the added XML to cellml_variable objects.
1589 
1590  This analyses the names in the ci elements to determine which variable in
1591  the model they refer to.
1592  """
1594  #1795 - classify temporary variables for the Jacobian matrix, and append
1595  # to the main list of assignments in the model
1596  solver_info = self._solver_info
1597  if hasattr(solver_info, u'jacobian') and hasattr(solver_info.jacobian, u'math'):
1598  for elt in solver_info.jacobian.math.apply:
1599  elt.classify_variables(root=True)
1600  for elt in solver_info.jacobian.math.apply:
1601  self._model.topological_sort(elt)
1602  #2418 - check if any state variables have been units-converted
def CellMLToNektar.translators.SolverInfo.create_dt (   self,
  modifier,
  comp,
  units 
)
Create the special 'dt' variable in the given component.

Definition at line 1775 of file translators.py.

References CellMLToNektar.translators.SolverInfo._dt.

1776  def create_dt(self, modifier, comp, units):
1777  """Create the special 'dt' variable in the given component."""
1778  self._dt = modifier.add_variable(comp, modifier._uniquify_var_name(u'dt', comp), units)
1779  self._dt._set_type(VarTypes.Free)
1780  return self._dt
def CellMLToNektar.translators.SolverInfo.do_binding_time_analysis (   self)
Do a binding time analysis on the additional mathematics.

This requires self.add_variable_links to have been called already.

Definition at line 1664 of file translators.py.

References CellMLToNektar.translators.SolverInfo._process_mathematics().

1665  def do_binding_time_analysis(self):
1666  """Do a binding time analysis on the additional mathematics.
1667 
1668  This requires self.add_variable_links to have been called already.
1669  """
1670  self._process_mathematics(lambda elt: elt._get_binding_time())
def CellMLToNektar.translators.SolverInfo.get_dt (   self)
Get or create a special 'dt' variable.

Definition at line 1781 of file translators.py.

References CellMLToNektar.translators.SolverInfo._dt, and CellMLToNektar.translators.SolverInfo._get_special_variable().

Referenced by CellMLToNektar.translators.SolverInfo._get_variable(), and CellMLToNektar.translators.SolverInfo.add_dt_reference().

1782  def get_dt(self):
1783  """Get or create a special 'dt' variable."""
1784  if not self._dt:
1785  self._dt = self._get_special_variable(u'dt', VarTypes.Free)
1786  return self._dt
def CellMLToNektar.translators.SolverInfo.get_linearised_odes (   self)
Return an iterable over the linearised ODEs, i.e. ODEs of the form
du/dt = g + hu (with g, h not functions of u).

Yields tuples (u, t, eqns) where the form of eqns depends on whether
add_linear_ode_update_equations has been called.  If so, it is a 1-tuple
containing the update equation; if not, it is (g,h).

Definition at line 1719 of file translators.py.

References CellMLToNektar.translators.SolverInfo._solver_info.

Referenced by CellMLToNektar.translators.SolverInfo.add_linear_ode_update_equations(), and CellMLToNektar.translators.SolverInfo.get_modifiable_mathematics().

1720  def get_linearised_odes(self):
1721  """Return an iterable over the linearised ODEs, i.e. ODEs of the form
1722  du/dt = g + hu (with g, h not functions of u).
1723 
1724  Yields tuples (u, t, eqns) where the form of eqns depends on whether
1725  add_linear_ode_update_equations has been called. If so, it is a 1-tuple
1726  containing the update equation; if not, it is (g,h).
1727  """
1728  if hasattr(self._solver_info, u'linear_odes'):
1729  if hasattr(self._solver_info.linear_odes.math, u'ci'):
1730  for math in self._solver_info.linear_odes.math:
1731  u, t, eqn = list(math.xml_element_children())
1732  u = u.variable
1733  t = t.variable
1734  yield (u, t, (eqn,))
1735  else:
1736  for ode in self._solver_info.linear_odes.math.apply:
1737  u = ode.apply.ci.variable
1738  t = ode.apply.bvar.ci.variable
1739  opers = ode.apply[1].operands()
1740  g = opers.next()
1741  h = opers.next().operands().next()
1742  yield (u, t, (g,h))
def CellMLToNektar.translators.SolverInfo.get_modifiable_mathematics (   self)
Get an iterable over mathematical constructs in the solver info blocks that can be changed.

Returned elements will be mathml_piecewise, mathml_apply, mathml_ci or mathml_cn instances.

Definition at line 1699 of file translators.py.

References CellMLToNektar.translators.SolverInfo._solver_info, and CellMLToNektar.translators.SolverInfo.get_linearised_odes().

Referenced by CellMLToNektar.translators.SolverInfo.has_modifiable_mathematics().

1700  def get_modifiable_mathematics(self):
1701  """Get an iterable over mathematical constructs in the solver info blocks that can be changed.
1702 
1703  Returned elements will be mathml_piecewise, mathml_apply, mathml_ci or mathml_cn instances.
1704  """
1705  solver_info = self._solver_info
1706  # Jacobian - entry definitions and temporaries can be changed
1707  if hasattr(solver_info, u'jacobian'):
1708  if hasattr(solver_info.jacobian, u'math'):
1709  for elt in solver_info.jacobian.math.apply:
1710  yield elt
1711  for entry in solver_info.jacobian.entry:
1712  for elt in entry.math.xml_element_children():
1713  yield elt
1714  # Linearised ODEs - only g & h can be changed
1715  if hasattr(solver_info, u'linear_odes'):
1716  for _, _, eqns in self.get_linearised_odes():
1717  for eqn in eqns:
1718  yield eqn
def CellMLToNektar.translators.SolverInfo.has_modifiable_mathematics (   self)
Check if the solver info blocks contain any modifiable mathematics.

Definition at line 1691 of file translators.py.

References CellMLToNektar.translators.SolverInfo.get_modifiable_mathematics().

1692  def has_modifiable_mathematics(self):
1693  """Check if the solver info blocks contain any modifiable mathematics."""
1694  try:
1695  self.get_modifiable_mathematics().next()
1696  return True
1697  except StopIteration:
1698  return False
def CellMLToNektar.translators.SolverInfo.use_canonical_variable_names (   self)
PE has just been performed, so we need to update variable names occurring outside
the modifiable mathematics sections.

Definition at line 1511 of file translators.py.

References CellMLToNektar.translators.SolverInfo._get_variable(), and CellMLToNektar.translators.SolverInfo._solver_info.

1512  def use_canonical_variable_names(self):
1513  """
1514  PE has just been performed, so we need to update variable names occurring outside
1515  the modifiable mathematics sections.
1516  """
1517  jac_elt = getattr(self._solver_info, u'jacobian', None)
1518  for entry in getattr(jac_elt, u'entry', []):
1519  for vlabel in ['var_i', 'var_j']:
1520  vname = getattr(entry, vlabel)
1521  var = self._get_variable(vname)
1522  new_name = var.get_source_variable(recurse=True).fullname()
1523  setattr(entry, vlabel, new_name)
1524  dt_elt = getattr(self._solver_info, u'dt', None)
1525  if dt_elt:
1526  var = self._get_variable(unicode(dt_elt))
1527  new_name = var.get_source_variable(recurse=True).fullname()
1528  dt_elt.xml_remove_child(unicode(dt_elt))
1529  dt_elt.xml_append(unicode(new_name))

Member Data Documentation

CellMLToNektar.translators.SolverInfo._component
private

Definition at line 1383 of file translators.py.

Referenced by CellMLToNektar.translators.SolverInfo._get_special_component().

CellMLToNektar.translators.SolverInfo._dt
private

Definition at line 1384 of file translators.py.

Referenced by CellMLToNektar.translators.SolverInfo.create_dt(), and CellMLToNektar.translators.SolverInfo.get_dt().

tuple CellMLToNektar.translators.SolverInfo._jac_temp_re = re.compile(r't[0-9]+')
staticprivate

Definition at line 1760 of file translators.py.

CellMLToNektar.translators.SolverInfo._model
private

Definition at line 1374 of file translators.py.

Referenced by CellMLToNektar.translators.SolverInfo._get_special_component(), CellMLToNektar.translators.SolverInfo._get_special_variable(), CellMLToNektar.translators.SolverInfo._get_variable(), CellMLToNektar.translators.SolverInfo.add_dt_reference(), CellMLToNektar.translators.SolverInfo.add_jacobian_matrix(), CellMLToNektar.translators.SolverInfo.add_linearised_odes(), CellMLToNektar.translators.SolverInfo.add_membrane_ionic_current(), and CellMLToNektar.translators.SolverInfo.add_transmembrane_potential_name().

CellMLToNektar.translators.SolverInfo._solver_info
private

Definition at line 1382 of file translators.py.

Referenced by CellMLToNektar.translators.SolverInfo._check_state_var_units_conversions(), CellMLToNektar.translators.SolverInfo._process_mathematics(), CellMLToNektar.translators.SolverInfo.add_dt_reference(), CellMLToNektar.translators.SolverInfo.add_jacobian_matrix(), CellMLToNektar.translators.SolverInfo.add_linear_ode_update_equations(), CellMLToNektar.translators.SolverInfo.add_linearised_odes(), CellMLToNektar.translators.SolverInfo.add_membrane_ionic_current(), CellMLToNektar.translators.SolverInfo.add_transmembrane_potential_name(), CellMLToNektar.translators.SolverInfo.add_variable_links(), CellMLToNektar.translators.SolverInfo.get_linearised_odes(), CellMLToNektar.translators.SolverInfo.get_modifiable_mathematics(), and CellMLToNektar.translators.SolverInfo.use_canonical_variable_names().