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

Public Member Functions

def __new__
 
def __init__
 
def copy
 
def get_consistent_set
 
def equals
 
def extract
 
def set_expression
 
def get_expression
 
def simplify
 
def dimensionally_equivalent
 
def description
 

Private Member Functions

def _add_source
 
def _get_sources
 

Private Attributes

 _expression
 
 _sources
 

Detailed Description

A set of cellml_units objects.

This class behaves like a normal set, but also has additional
methods for operations specific to sets of <units> objects:
  simplify - allow for multiplication of sets of units
  dimensionally_equivalent - compare 2 sets of units for dimensional equivalence
  description - describe the units in this set

All units in the set (normally) must be dimensionally equivalent.  The exception is when dealing with
Functional Curation protocols which can defined units conversion rules for non-scaling cases.  We can
then have sets of alternative units in different dimensions, and the get_consistent_set method helps
with selecting from these.

Definition at line 2349 of file pycml.py.

Constructor & Destructor Documentation

def CellMLToNektar.pycml.UnitsSet.__init__ (   self,
  iterable = [],
  expression = None 
)

Definition at line 2372 of file pycml.py.

2373  def __init__(self, iterable=[], expression=None):
2374  super(UnitsSet, self).__init__(iterable)
2375  self._expression = expression
2376  self._sources = {}
2377  return

Member Function Documentation

def CellMLToNektar.pycml.UnitsSet.__new__ (   cls,
  iterable = [],
  expression = None 
)
Work around annoyance in set implementation of python 2.4.2c1 and on.
setobject.c checks for keyword arguments in its __new__ instead of its
__init__, so we get an error
'TypeError: set() does not take keyword arguments' if we don't do this.

Definition at line 2363 of file pycml.py.

2364  def __new__(cls, iterable=[], expression=None):
2365  """
2366  Work around annoyance in set implementation of python 2.4.2c1 and on.
2367  setobject.c checks for keyword arguments in its __new__ instead of its
2368  __init__, so we get an error
2369  'TypeError: set() does not take keyword arguments' if we don't do this.
2370  """
2371  return super(UnitsSet, cls).__new__(cls, iterable)
def CellMLToNektar.pycml.UnitsSet._add_source (   self,
  units,
  src_units_set,
  src_units 
)
private
Add source units for the given units.

This stores references to how units were arrived at when doing a
simplify.  It manages lists of (src_units_set, src_units) pairs for
each units definition in this set.

If src_units_set has no associated expression, then it is
considered to be a temporary object, created for example whilst
doing an n-ary times operation.  In this case, we add its list of
sources for src_units to our sources list instead.

Definition at line 2446 of file pycml.py.

References CellMLToNektar.pycml.UnitsSet._sources, Nektar::LibUtilities::CmdLineArg.description, and CellMLToNektar.pycml.UnitsSet.description().

2447  def _add_source(self, units, src_units_set, src_units):
2448  """Add source units for the given units.
2449 
2450  This stores references to how units were arrived at when doing a
2451  simplify. It manages lists of (src_units_set, src_units) pairs for
2452  each units definition in this set.
2453 
2454  If src_units_set has no associated expression, then it is
2455  considered to be a temporary object, created for example whilst
2456  doing an n-ary times operation. In this case, we add its list of
2457  sources for src_units to our sources list instead.
2458  """
2459  if not units in self._sources:
2460  self._sources[units] = []
2461  if not hasattr(src_units_set, '_expression'):
2462  print self.description(), units.description()
2463  print src_units_set.description(), src_units.description()
2464  if src_units_set._expression:
2465  self._sources[units].append((src_units_set, src_units))
2466  else:
2467  try:
2468  self._sources[units].extend(src_units_set._sources[src_units])
2469  except KeyError:
2470  # No sources list found. This can happen if we do
2471  # dimensionless.simplify(...), for example, to evaluate powers.
2472  pass
2473  return
def CellMLToNektar.pycml.UnitsSet._get_sources (   self,
  units 
)
private
Return the sources list for the given units.

Definition at line 2474 of file pycml.py.

2475  def _get_sources(self, units):
2476  """Return the sources list for the given units."""
2477  return self._sources.get(units, [])
def CellMLToNektar.pycml.UnitsSet.copy (   self)
Do a shallow copy of this UnitsSet.

Definition at line 2378 of file pycml.py.

References CellMLToNektar.pycml.UnitsSet._expression.

2379  def copy(self):
2380  """Do a shallow copy of this UnitsSet."""
2381  new_set = super(UnitsSet, self).copy()
2382  new_set._expression = self._expression
2383  new_set._sources = {}
2384  for units, src_list in self._sources.iteritems():
2385  new_set._sources[units] = copy.copy(src_list)
2386  return new_set
def CellMLToNektar.pycml.UnitsSet.description (   self)
Describe these units.

Shows the descriptions of each member, as though this were a set of
unicode strings.  If multiple members have the same description,
only one instance is shown.  If only one description is being shown,
then the curly brackets are not added.

Definition at line 2525 of file pycml.py.

Referenced by CellMLToNektar.pycml.UnitsSet._add_source(), and CellMLToNektar.pycml.cellml_units.uniquify_tuple().

2526  def description(self):
2527  """Describe these units.
2528 
2529  Shows the descriptions of each member, as though this were a set of
2530  unicode strings. If multiple members have the same description,
2531  only one instance is shown. If only one description is being shown,
2532  then the curly brackets are not added.
2533  """
2534  desc = list(set(u.description() for u in self))
2535  desc.sort()
2536  if len(desc) > 1:
2537  desc = u'{' + u','.join(desc) + u'}'
2538  else:
2539  desc = desc[0]
2540  return desc
def CellMLToNektar.pycml.UnitsSet.dimensionally_equivalent (   self,
  other_units 
)
Check for dimensional equivalence between sets of units.

Since all units in each set should be equivalent, we just compare
an arbitrary member from each set.

other_units may be a single cellml_units instance, in which case we
compare an arbitrary member of self to it.

Definition at line 2512 of file pycml.py.

References CellMLToNektar.pycml.UnitsSet.extract().

2513  def dimensionally_equivalent(self, other_units):
2514  """Check for dimensional equivalence between sets of units.
2515 
2516  Since all units in each set should be equivalent, we just compare
2517  an arbitrary member from each set.
2518 
2519  other_units may be a single cellml_units instance, in which case we
2520  compare an arbitrary member of self to it.
2521  """
2522  u1 = self.extract()
2523  u2 = other_units.extract()
2524  return u1.dimensionally_equivalent(u2)
def CellMLToNektar.pycml.UnitsSet.equals (   self,
  other 
)
Test whether the units in the set are equal to those in another set.

Definition at line 2413 of file pycml.py.

References CellMLToNektar.pycml.UnitsSet.extract().

2414  def equals(self, other):
2415  """Test whether the units in the set are equal to those in another set."""
2416  try:
2417  equal = self.extract(check_equality=True).equals(other.extract(check_equality=True))
2418  except ValueError:
2419  equal = False
2420  return equal
def CellMLToNektar.pycml.UnitsSet.extract (   self,
  check_equality = False 
)
Extract a representative element from this set.

This is intended to be used to get the cellml_units object from a singleton set.

If check_equality is True, check that all members of this set have the same multiplicative factor.

Definition at line 2421 of file pycml.py.

References CellMLToNektar.pycml.get_multiplicative_factor(), and CellMLToNektar.pycml.get_offset().

Referenced by CellMLToNektar.pycml.UnitsSet.dimensionally_equivalent(), CellMLToNektar.pycml.UnitsSet.equals(), and CellMLToNektar.pycml.UnitsSet.get_consistent_set().

2422  def extract(self, check_equality=False):
2423  """Extract a representative element from this set.
2424 
2425  This is intended to be used to get the cellml_units object from a singleton set.
2426 
2427  If check_equality is True, check that all members of this set have the same multiplicative factor.
2428  """
2429  representative = iter(self).next()
2430  if check_equality:
2431  for u in self:
2432  if not u._rel_error_ok(u.expand().get_multiplicative_factor(),
2433  representative.expand().get_multiplicative_factor(),
2434  1e-6):
2435  raise ValueError("UnitsSet equality check failed")
2436  if u.is_simple() and not u._rel_error_ok(u.expand().get_offset(),
2437  representative.expand().get_offset(),
2438  1e-6):
2439  raise ValueError("UnitsSet equality check failed")
2440  return representative
def get_multiplicative_factor
Definition: pycml.py:2876
def CellMLToNektar.pycml.UnitsSet.get_consistent_set (   self,
  desired_units 
)
Extract a subset of the units in this set that are dimensionally equivalent.

When dealing with potential non-scaling conversions, an expression may have potential units that are
not within the same dimension.  However, when deciding on the units for the expression most operations
need to handle a set of options that *are* within the same dimension, and this operation supports that.

Given a cellml_units object for the desired units of the expression, this method first tries to create
a UnitsSet containing just our members in the same dimension.  If we have no members in the desired
dimension, then we check that all members of this set are in the same dimension, and return the set
itself - there should never be more than 2 different dimensions to choose from (I hope!).

Definition at line 2387 of file pycml.py.

References CellMLToNektar.pycml.UnitsSet._expression, CellMLToNektar.pycml.UnitsSet._sources, and CellMLToNektar.pycml.UnitsSet.extract().

2388  def get_consistent_set(self, desired_units):
2389  """Extract a subset of the units in this set that are dimensionally equivalent.
2390 
2391  When dealing with potential non-scaling conversions, an expression may have potential units that are
2392  not within the same dimension. However, when deciding on the units for the expression most operations
2393  need to handle a set of options that *are* within the same dimension, and this operation supports that.
2394 
2395  Given a cellml_units object for the desired units of the expression, this method first tries to create
2396  a UnitsSet containing just our members in the same dimension. If we have no members in the desired
2397  dimension, then we check that all members of this set are in the same dimension, and return the set
2398  itself - there should never be more than 2 different dimensions to choose from (I hope!).
2399  """
2400  new_set = UnitsSet([], expression=self._expression)
2401  for units in self:
2402  if units.dimensionally_equivalent(desired_units):
2403  new_set.add(units)
2404  new_set._sources[units] = copy.copy(self._sources[units])
2405  if not new_set:
2406  rep_u = self.extract()
2407  for units in self:
2408  if not units.dimensionally_equivalent(rep_u):
2409  raise ValueError("Unexpected dimensional variation in UnitsSet; " + rep_u.description() + " and " + units.description()
2410  + " do not match.")
2411  new_set = self
2412  return new_set
def CellMLToNektar.pycml.UnitsSet.get_expression (   self)
Return an expression that has these units.

Definition at line 2478 of file pycml.py.

References CellMLToNektar.pycml.UnitsSet._expression.

2479  def get_expression(self):
2480  """Return an expression that has these units."""
2481  return self._expression
def CellMLToNektar.pycml.UnitsSet.set_expression (   self,
  expr 
)
Store a reference to the expression that has these units.

Definition at line 2441 of file pycml.py.

References CellMLToNektar.pycml.UnitsSet._expression.

2442  def set_expression(self, expr):
2443  """Store a reference to the expression that has these units."""
2444  self._expression = expr
2445  return
def CellMLToNektar.pycml.UnitsSet.simplify (   self,
  other_units = None,
  other_exponent = 1 
)
Simplify the units in this set.

Each cellml_units object in this set is simplified, and a new
UnitsSet returned with the results.

If other_units is not None, then products of units are
calculated.  The returned set is essentially the cartesian
product of the input sets under the simplify operator, i.e.
u1.simplify(other_units=u2, other_exponent=other_exponent)
will be called for each member u1 of self and each member u2
of other_units (if other_units is a UnitsSet; otherwise
u2=other_units).

Definition at line 2482 of file pycml.py.

2483  def simplify(self, other_units=None, other_exponent=1):
2484  """Simplify the units in this set.
2485 
2486  Each cellml_units object in this set is simplified, and a new
2487  UnitsSet returned with the results.
2488 
2489  If other_units is not None, then products of units are
2490  calculated. The returned set is essentially the cartesian
2491  product of the input sets under the simplify operator, i.e.
2492  u1.simplify(other_units=u2, other_exponent=other_exponent)
2493  will be called for each member u1 of self and each member u2
2494  of other_units (if other_units is a UnitsSet; otherwise
2495  u2=other_units).
2496  """
2497  result_set = UnitsSet()
2498  for units in self:
2499  if other_units is None:
2500  res_u = units.simplify()
2501  result_set.add(res_u)
2502  result_set._add_source(res_u, self, units)
2503  else:
2504  if isinstance(other_units, cellml_units):
2505  other_units = UnitsSet([other_units])
2506  for u in other_units:
2507  res_u = units.simplify(u, other_exponent)
2508  result_set.add(res_u)
2509  result_set._add_source(res_u, self, units)
2510  result_set._add_source(res_u, other_units, u)
2511  return result_set

Member Data Documentation

CellMLToNektar.pycml.UnitsSet._expression
private

Definition at line 2374 of file pycml.py.

Referenced by CellMLToNektar.pycml.UnitsSet.copy(), CellMLToNektar.pycml.UnitsSet.get_consistent_set(), CellMLToNektar.pycml.UnitsSet.get_expression(), and CellMLToNektar.pycml.UnitsSet.set_expression().

CellMLToNektar.pycml.UnitsSet._sources
private

Definition at line 2375 of file pycml.py.

Referenced by CellMLToNektar.pycml.UnitsSet._add_source(), and CellMLToNektar.pycml.UnitsSet.get_consistent_set().