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

Public Member Functions

def __new__ (cls, iterable=[], expression=None)
 
def __init__ (self, iterable=[], expression=None)
 
def copy (self)
 
def get_consistent_set (self, desired_units)
 
def equals (self, other)
 
def extract (self, check_equality=False)
 
def set_expression (self, expr)
 
def get_expression (self)
 
def simplify (self, other_units=None, other_exponent=1)
 
def dimensionally_equivalent (self, other_units)
 
def description (self)
 

Private Member Functions

def _add_source (self, units, src_units_set, src_units)
 
def _get_sources (self, units)
 

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

◆ __init__()

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

Definition at line 2372 of file pycml.py.

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

References CellMLToNektar.pycml.UnitsSet.__init__().

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

Member Function Documentation

◆ __new__()

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.

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

References CellMLToNektar.pycml.UnitsSet.__new__().

Referenced by CellMLToNektar.pycml.UnitsSet.__new__().

◆ _add_source()

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.

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

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

◆ _get_sources()

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.

2474 def _get_sources(self, units):
2475 """Return the sources list for the given units."""
2476 return self._sources.get(units, [])
2477

References CellMLToNektar.pycml.UnitsSet._sources.

◆ copy()

def CellMLToNektar.pycml.UnitsSet.copy (   self)
Do a shallow copy of this UnitsSet.

Definition at line 2378 of file pycml.py.

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

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

Referenced by CellMLToNektar.pycml.UnitsSet.copy().

◆ description()

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.

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

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

◆ dimensionally_equivalent()

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.

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

References CellMLToNektar.pycml.UnitsSet.extract().

◆ equals()

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.

2413 def equals(self, other):
2414 """Test whether the units in the set are equal to those in another set."""
2415 try:
2416 equal = self.extract(check_equality=True).equals(other.extract(check_equality=True))
2417 except ValueError:
2418 equal = False
2419 return equal
2420
def equals(self, other)
Definition: pycml.py:2634

References CellMLToNektar.pycml.UnitsSet.equals(), and CellMLToNektar.pycml.UnitsSet.extract().

Referenced by CellMLToNektar.pycml.UnitsSet.equals().

◆ extract()

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.

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

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

◆ get_consistent_set()

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.

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

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

◆ get_expression()

def CellMLToNektar.pycml.UnitsSet.get_expression (   self)
Return an expression that has these units.

Definition at line 2478 of file pycml.py.

2478 def get_expression(self):
2479 """Return an expression that has these units."""
2480 return self._expression
2481

References CellMLToNektar.pycml.UnitsSet._expression.

◆ set_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.

2441 def set_expression(self, expr):
2442 """Store a reference to the expression that has these units."""
2443 self._expression = expr
2444 return
2445

References CellMLToNektar.pycml.UnitsSet._expression.

◆ simplify()

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.

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

Member Data Documentation

◆ _expression

CellMLToNektar.pycml.UnitsSet._expression
private

◆ _sources

CellMLToNektar.pycml.UnitsSet._sources
private