cosapp.core.numerics.boundary

Classes

AbstractBoundaryImpl()

Abstract Boundary class to manage methods specific to Boundary type.

AbstractTimeUnknown(**kwargs)

AttrRef(context, key)

Attribute Reference for scalar object.

Boundary(context, name[, mask, default, ...])

Numerical solver boundary.

GenericBoundaryImpl()

Class handling undefined Boundary.

MaskedAttrRef(obj, key, mask)

Masked Attribute Reference for MutableSequence-like object.

MaskedVarInfo(basename, selector, mask)

MutableSeqBoundaryImpl()

Specific methods for MutableSequence-like Boundary.

NumpyArrayAttrRef(obj, key)

Attribute Reference for unmasked numpy arrays.

NumpyBoundaryImpl()

Specific methods for numpy.ndarray Boundary.

NumpyMaskedAttrRef(obj, key, mask)

Masked Attribute Reference for numpy arrays.

ScalarBoundaryImpl()

Specific methods for Number Boundary.

TimeDerivative(context, name, source[, ...])

Explicit time derivative.

TimeUnknown(context, name, der[, ...])

Time-dependent solver unknown.

UndefinedBoundaryImpl()

Class handling undefined Boundary.

Unknown(context, name[, max_abs_step, ...])

Numerical solver unknown.

class cosapp.core.numerics.boundary.AbstractBoundaryImpl[source]

Bases: ABC

Abstract Boundary class to manage methods specific to Boundary type.

abstract check_new_value(new: T) None[source]
abstract size() int[source]
abstract update_value(new: T, checks=True) bool[source]
class cosapp.core.numerics.boundary.AbstractTimeUnknown(**kwargs)[source]

Bases: ABC

property constrained: bool

is unknown constrained by a limiting time step?

Type:

bool

property d_dt: Any

Value of time derivative

abstract property der: EvalString

Expression of the time derivative, given as an EvalString

extrapolated_time_step(step: float) float[source]

Time step necessary to attain a variation of step at a rate given by current value of the time derivative.

property max_abs_step: float

Maximum absolute step in one iteration

Type:

float

abstract property max_abs_step_expr: EvalString

Expression of the maximum absolute step in one iteration, given as an EvalString.

property max_time_step: float

Maximum admissible time step in one iteration

Type:

float

abstract property max_time_step_expr: EvalString

Expression of the maximum admissible time step, given as an EvalString.

abstract reset() None[source]

Reset transient unknown to a reference value

abstract touch() None[source]

Set owner port as ‘dirty’.

class cosapp.core.numerics.boundary.AttrRef(context: System, key: str)[source]

Bases: object

Attribute Reference for scalar object.

In addition to System and its derivatives, manage also complex object which could be included in the evaluation context.

Parameters:
  • obj (cosapp.systems.System) – System in which the boundary name is defined.

  • key (str) – Name of the boundary

update_shape() None[source]
property value: Number
class cosapp.core.numerics.boundary.Boundary(context: System, name: str, mask: numpy.ndarray | None = None, default: Number | numpy.ndarray | None = None, inputs_only=True, **kwargs)[source]

Bases: object

Numerical solver boundary.

Parameters:
  • context (cosapp.systems.System) – System in which the boundary is defined.

  • name (str) – Name of the boundary

  • mask (numpy.ndarray or None) – Mask of the values in the vector boundary.

  • default (Number, numpy.ndarray or None) – Default value to set the boundary with.

  • inputs_only (bool, optional) – If True (default), output variables are regarded as invalid.

property basename: str

Base name of the boundary.

Type:

str

property context: System

System in which the boundary is defined.

Type:

cosapp.systems.System

contextual_name(context: System | None = None) str[source]

str : Contextual name of the boundary, relative to context. If context is None (default), uses current variable context.

copy() Boundary[source]
classmethod create_attr_ref(context: System, basename: str, value: Number | Collection | None, mask: numpy.ndarray | None = None) tuple[AttrRef | NumpyMaskedAttrRef | MaskedAttrRef, AbstractBoundaryImpl, bool][source]

Returns an AttrRef, MaskedAttrRef, or ǸumpyMaskedAttrRef` object from a name and its evaluation context. The NumpyMaskedAttrRef derives from AttrRef if the context variable refers to a numpy.array and MaskedAttrRef for a variable referring to an object similar to a MutableSequence. In the two latter cases, a mask may be applied on value.

Parameters:
  • context (-) – System in which the boundary is defined.

  • basename (-) – Name of the boundary without its mask if any.

  • value (-) – Value of the context variable.

  • mask (-) – Mask to apply on the variable; default is None (i.e. no mask).

Returns:

  • Union[AttrRef, NumpyMaskedAttrRef, MaskedAttrRef] – (Masked) Attribute Reference object.

  • AbstractBoundaryImpl – Object containing methods specific according to the variable type.

  • bool – Specify if the boundary value is a scalar.

static create_mask(system: System, varname: str, selector: str, mask: numpy.ndarray | None = None) tuple[Number | Collection | None, numpy.ndarray | None][source]

Evaluate the basename expression within its context and generate a mask if a selector is specified in the fullname expression.

Parameters:
  • system (-) – System to which variable belongs.

  • basename (-) – Variable name without any optional array mask.

  • selector (-) – Expression corresponding to an array mask.

  • mask (-) – Imposed mask to apply on the variable; default is None (i.e. no mask).

Returns:

  • Optional[Union[Number, Collection]] – Value of the context variable.

  • Optional[numpy.ndarray] – Imposed or generated mask to apply on the variable.

property default_value: Number | ndarray
find_port(inputs_only=False) None[source]

Find port associated to its AttrRef. In the case of a complex object, the port containing it is retrieved and checks.

Parameters:
  • [bool (- inputs_only) – If True, output variables are regarded as invalid. Default is False.

  • optional] – If True, output variables are regarded as invalid. Default is False.

static is_mutable_sequence(value: Any) bool[source]

Determine if an object is MutableSequence-like.

property is_scalar: bool

Returns whether this boundary is scalar or not.

property mask: ndarray | None

Mask of the values in the vector boundary.

Type:

numpy.ndarray or None

mask_indices()[source]

Iterator over index tuples of the mask (if any).

property name: str

Contextual name of the boundary.

Type:

str

static parse_expression(expression: str) MaskedVarInfo[source]

Decompose a variable specification into its base name and selector.

Parameters:

expression (str) – Variable specification (variable name + optional array mask, if required)

Returns:

  • - str (variable name)

  • - str (array selector)

property port: BasePort

port containing the boundary.

Type:

BasePort

property portname: str

name of the port accessed by the boundary.

Type:

str

property ref: AttrRef | MaskedAttrRef | NumpyMaskedAttrRef

attribute reference accessed by the boundary.

Type:

AttrRef

set_to_default() None[source]
property size: int
touch() None[source]

Set owner port as ‘dirty’.

update_default_value(new: Number | MutableSequence | ndarray, checks: bool = True) None[source]
update_value(new: Number | MutableSequence | ndarray, checks=True) None[source]
property value: Number | ndarray
property variable: str

name of the variable accessed by the boundary.

Type:

str

property variable_reference: VariableReference

variable reference accessed by the boundary.

Type:

VariableReference

class cosapp.core.numerics.boundary.GenericBoundaryImpl[source]

Bases: AbstractBoundaryImpl

Class handling undefined Boundary.

static check_new_value(ref: T, new: T) None[source]

Check that reference and new values are compatible.

static size(value: T) int[source]
static update_value(ref_value: T, new: T, checks=True) bool[source]
class cosapp.core.numerics.boundary.MaskedAttrRef(obj: System, key: str, mask: numpy.ndarray)[source]

Bases: AttrRef

Masked Attribute Reference for MutableSequence-like object.

Include a mask applying to an evaluation context vector.

Parameters:
  • obj (cosapp.systems.System) – System in which the boundary name is defined.

  • key (str) – Name of the boundary

  • mask (numpy.ndarray) – Mask of the values in the vector boundary.

classmethod make_from_attr_ref(attr_ref: AttrRef, obj: BasePort | Any, name: str, mask: ndarray) MaskedAttrRef[source]
set_attributes(mask: ndarray) None[source]
set_mask(mask: ndarray) None[source]
property value: MutableSequence
class cosapp.core.numerics.boundary.MaskedVarInfo(basename, selector, mask)[source]

Bases: NamedTuple

basename: str

Alias for field number 0

property fullname: str
mask: ndarray | None

Alias for field number 2

selector: str

Alias for field number 1

class cosapp.core.numerics.boundary.MutableSeqBoundaryImpl[source]

Bases: AbstractBoundaryImpl

Specific methods for MutableSequence-like Boundary.

static check_new_value(value: MutableSequence, new: MutableSequence) None[source]

Check that reference and new values are compatible.

static size(value: MutableSequence) int[source]
static update_value(ref_value: MutableSequence, new: MutableSequence, checks=True) bool[source]
class cosapp.core.numerics.boundary.NumpyArrayAttrRef(obj: System, key: str)[source]

Bases: AttrRef

Attribute Reference for unmasked numpy arrays.

Parameters:
  • obj (cosapp.systems.System) – System in which the boundary name is defined.

  • key (str) – Name of the boundary

update_shape() None[source]
property value
class cosapp.core.numerics.boundary.NumpyBoundaryImpl[source]

Bases: AbstractBoundaryImpl

Specific methods for numpy.ndarray Boundary.

static check_new_value(ref: ndarray, new: ndarray) None[source]

Check that reference and new value shapes are compatible.

static size(value: ndarray) int[source]
static update_value(ref_value: ndarray, new: ndarray, checks=True) bool[source]
class cosapp.core.numerics.boundary.NumpyMaskedAttrRef(obj: System, key: str, mask: numpy.ndarray)[source]

Bases: AttrRef

Masked Attribute Reference for numpy arrays.

Include a mask applying to an evaluation context vector.

Parameters:
  • obj (cosapp.systems.System) – System in which the boundary name is defined.

  • key (str) – Name of the boundary

  • mask (numpy.ndarray) – Mask of the values in the vector boundary.

classmethod make_from_attr_ref(attr_ref: AttrRef, obj: BasePort | Any, name: str, mask: ndarray) NumpyMaskedAttrRef[source]
set_attributes(mask: ndarray) None[source]
set_mask(mask: ndarray) None[source]
update_shape() None[source]
property value: ndarray
class cosapp.core.numerics.boundary.ScalarBoundaryImpl[source]

Bases: AbstractBoundaryImpl

Specific methods for Number Boundary.

static check_new_value(ref: Number, new: Number) None[source]

Check that reference and new values are compatible.

static size(value: Number) int[source]
static update_value(ref_value: Number, new: Number, checks=True) bool[source]
class cosapp.core.numerics.boundary.TimeDerivative(context: System, name: str, source: Any, initial_value: Any = None)[source]

Bases: Boundary

Explicit time derivative.

Parameters:
  • context (cosapp.systems.System) – System in which the unknown is defined.

  • name (str) – Name of the variable

  • source (str) – Variable such that name = d(source)/dt

  • initial_value (Any) – Time derivative initial value

copy() TimeDerivative[source]
property initial_value: Number | ndarray

Initial value of time derivative

property initial_value_expr: EvalString

Initial value of time derivative, returned as an EvalString

reset(value: Any = None) None[source]
property source: Number | ndarray

Value of the variable whose rate is evaluated

property source_expr: EvalString

Variable whose rate is evaluated, returned as an EvalString

static source_type(expression: Any, context: System) tuple[source]

Static method to evaluate the type and default value of an expression used as rate source

to_dict() dict[str, Any][source]

Returns a JSONable representation of the time derivative.

Returns:

JSONable representation

Return type:

dict[str, Any]

update(dt: Number) Number[source]

Evaluate rate-of-change of source over time interval dt

property value: Number | ndarray
class cosapp.core.numerics.boundary.TimeUnknown(context: System, name: str, der: Any, max_time_step: Number | str = inf, max_abs_step: Number | str = inf, pulled_from: VariableReference | None = None)[source]

Bases: Boundary, AbstractTimeUnknown

Time-dependent solver unknown.

Parameters:
  • context (cosapp.systems.System) – System in which the unknown is defined.

  • name (str) – Name of the unknown

max_time_step

Max time step authorized in one iteration; default numpy.inf

Type:

float

copy() TimeUnknown[source]

Copy time-dependent unknown object.

Returns:

Duplicated unknown

Return type:

TimeUnknown

property d_dt: Any

Value of time derivative

property der: EvalString

Expression of time derivative, given as an EvalString

static der_type(expression: Any, context: System) tuple[EvalString, Any, type][source]

Static method to evaluate the type and default value of an expression used as time derivative

static is_number(value) bool[source]

Is value suitable for a derivative?

property max_abs_step: float

Maximum absolute step in one iteration

Type:

float

property max_abs_step_expr: EvalString

Maximum admissible step, given as an EvalString.

property max_time_step: float

Maximum admissible time step in one iteration

Type:

float

property max_time_step_expr: EvalString

Maximum admissible time step, given as an EvalString.

property pulled_from: VariableReference | None

Original time unknown before pulling; None otherwise.

Type:

VariableReference or None

reset() None[source]

Reset transient unknown to a reference value. Inactive for class TimeUnknown.

to_dict() dict[str, Any][source]

Returns a JSONable representation of the transient unknown.

Returns:

JSONable representation

Return type:

dict[str, Any]

update_shape() None[source]
property value: Number | ndarray
class cosapp.core.numerics.boundary.UndefinedBoundaryImpl[source]

Bases: AbstractBoundaryImpl

Class handling undefined Boundary.

static check_new_value(value: T, new: T) None[source]
static size(value: T) int[source]
static update_value(ref_value: T, new: T, checks=True) bool[source]
class cosapp.core.numerics.boundary.Unknown(context: System, name: str, max_abs_step: Number = inf, max_rel_step: Number = inf, lower_bound: Number = -inf, upper_bound: Number = inf, mask: numpy.ndarray | None = None)[source]

Bases: Boundary

Numerical solver unknown.

Parameters:
  • context (cosapp.systems.System) – System in which the unknown is defined.

  • name (str) – Name of the unknown

  • lower_bound (float) – Minimum value authorized; default -numpy.inf

  • upper_bound (float) – Maximum value authorized; default numpy.inf

  • max_abs_step (float) – Max absolute step authorized in one iteration; default numpy.inf

  • max_rel_step (float) – Max relative step authorized in one iteration; default numpy.inf

  • mask (numpy.ndarray or None) – Mask of unknown values in the vector variable.

lower_bound

Minimum value authorized; default -numpy.inf

Type:

float

upper_bound

Maximum value authorized; default numpy.inf

Type:

float

max_abs_step

Largest absolute step authorized in one iteration; default numpy.inf

Type:

float

max_rel_step

Largest relative step authorized in one iteration; default numpy.inf

Type:

float

Notes

The dimensionality of the variable should be taken into account in the bounding process.

check_numerical_type() None[source]

Check if the unknown is suitable for a solver.

Raises:

TypeError – If the unknown is neither a numerical variable nor a mutable sequence.

Warning

UserWarning

If the unknown is declared as an array of integers.

copy() Unknown[source]

Copy the unknown object.

Returns:

Duplicated unknown

Return type:

Unknown

to_dict() dict[str, Any][source]

Returns a JSONable representation of the unknown.

Returns:

JSONable representation

Return type:

dict[str, Any]

transfer(context: System, name: str) Unknown[source]

Transfer a copy of the unknown in a new context.

Returns:

Duplicated unknown, in new context

Return type:

Unknown

cosapp.core.numerics.boundary.object__getstate__(self, /)

Helper for pickle.