trnsystor.statement.Equation

class trnsystor.statement.Equation(name=None, equals_to=None, doc=None, model=None)[source]

EQUATION Statement.

The EQUATIONS statement allows variables to be defined as algebraic functions of constants, previously defined variables, and outputs from TRNSYS components. These variables can then be used in place of numbers in the TRNSYS input file to represent inputs to components; numerical values of parameters; and initial values of inputs and time-dependent variables. The capabilities of the EQUATIONS statement overlap but greatly exceed those of the CONSTANTS statement described in the previous section.

Hint

In trnsystor, the Equation class works hand in hand with the EquationCollection class. This class behaves a little bit like the equation component in the TRNSYS Studio, meaning that you can list equation in a block, give it a name, etc. See the EquationCollection class for more details.

Initialize object.

Parameters
  • name (str) – The left hand side of the equation.

  • equals_to (str, TypeVariable) – The right hand side of the equation.

  • doc (str, optional) – A small description optionally printed in the deck file.

  • model (Component) – The TrnsysModel this Equation belongs to.

classmethod from_expression(expression, doc=None)[source]

Create an equation from a string expression.

Anything before the equal sign (“=”) will become a Constant and anything after will become the equality statement.

Example

Create a simple expression like so:

>>> equa1 = Equation.from_expression("TdbAmb = [011,001]")
Parameters
  • expression (str) – A user-defined expression to parse.

  • doc (str, optional) – A small description optionally printed in the deck file.

classmethod from_symbolic_expression(name, exp, *args, doc=None)[source]

Create an equation from symbolic expression.

Crate an equation with a combination of a generic expression (with placeholder variables) and a list of arguments. The underlying engine will use Sympy and symbolic variables. You can use a mixture of TypeVariable and Equation, Constant as well as the python default str.

Important

If a str is passed in place of an expression argument ( args), make sure to declare that string as an Equation or a Constant later in the routine.

Examples

In this example, we define a variable (var_a) and we want it to be equal to the ‘Outlet Air Humidity Ratio’ divided by 12 + log( Temperature to heat source). In a TRNSYS deck file one would have to manually determine the unit numbers and output numbers and write something like : ‘[1, 2]/12 + log([1, 1])’. With the from_symbolic_expression(), we can do this very simply:

  1. first, define the name of the variable:

>>> name = "var_a"

2. then, define the expression as a string. Here, the variables a and b are symbols that represent the two type outputs. Note that their name has bee chosen arbitrarily.

>>> exp = "log(a) + b / 12"
>>> # would be also equivalent to
>>> exp = "log(x) + y / 12"

3. here, we define the actual variables (the type outputs) after loading our model from its proforma:

>>> from trnsystor import TrnsysModel
>>> fan = TrnsysModel.from_xml("fan_type.xml")
>>> vars = (fan.outputs[0], fan.outputs[1])

Important

The order of the symbolic variable encountered in the string expression (step 2), from left to right, must be the same for the tuple of variables. For instance, a is followed by b, therefore fan.outputs[0] is followed by fan.outputs[1].

4. finally, we create the Equation. Note that vars is passed with the ‘*’ declaration to unpack the tuple.

>>> from trnsystor.statement import Equation
>>> eq = Equation.from_symbolic_expression(name, exp, *vars)
>>> print(eq)
[1, 1]/12 + log([1, 2])
Parameters
  • name (str) – The name of the variable (left-hand side), of the equation.

  • exp (str) – The expression to evaluate. Use any variable name and mathematical expression.

  • *args (tuple) – A tuple of TypeVariable that will replace the any variable name specified in the above expression.

  • doc (str, optional) – A small description optionally printed in the deck file.

Returns

The Equation Statement object.

Return type

Equation

property eq_number

Return the equation number (unique).

property idx

Return the 0-based index of the Equation.

property unit_number

Return the unit number of the EquationCollection self belongs to.

connect_to(other, link_style_kwargs=None)

Connect a single TypeVariable to TypeVariable other.

Important

Keep in mind that since python traditionally uses 0-based indexing, the same logic is used in this package even though TRNSYS uses traditionally 1-based indexing. The package will internally handle the 1-based index in the output .dck file.

Examples

Connect two TypeVariable objects together

>>> pipe_1.outputs['Outlet_Air_Temperature'].connect_to(
>>>     other=pipe2.intputs['Inlet_Air_Temperature']
>>> )
Parameters

other (TypeVariable) – The other object.

Raises

TypeError – When trying to connect to anything other than a TrnsysModel.

copy()

TypeVariable: Make a copy of self.

classmethod from_tag(tag, model=None)

Class method to create a TypeVariable from an XML tag.

Parameters
  • tag (Tag) – The XML tag with its attributes and contents.

  • model (TrnsysModel) – The model.

property is_connected

Whether or not this TypeVariable is connected to another TypeVariable.

Checks if self is in any keys

property one_based_idx

Get the 1-based variable index of self such as it appears in Trnsys.

property predecessor

Other TypeVariable from which this Input TypeVariable is connected.

Predecessors