Internal API
Here, all internal API is documented. Note that this is, as the name says, internal. Thus it is only intended for internal use and might change unexpectedly.
Index
BioChemicalTreatment.ProcessSimulator.DEFAULT_PROCESSES
BioChemicalTreatment.ProcessSimulator.DEFAULT_PROCESSES_ARGS
BioChemicalTreatment.ProcessSimulator.DEFAULT_PROCESSES_KWARGS
BioChemicalTreatment.ProcessSimulator.DEFAULT_PROCESSES_NAMES
BioChemicalTreatment.ProcessSimulator.DEFAULT_STATE_MAPPING
BioChemicalTreatment.Reactions.DEFAULT_PARSING_CONTEXT
BioChemicalTreatment.ProcessSimulator.AbstractProcessElement
BioChemicalTreatment.ProcessSimulator.PortMetadata
Base.convert
BioChemicalTreatment.ProcessSimulator.InflowPort
BioChemicalTreatment.ProcessSimulator.OutflowPort
BioChemicalTreatment.ProcessSimulator.ReactionInputPort
BioChemicalTreatment.ProcessSimulator.ReactionOutputPort
BioChemicalTreatment.ProcessSimulator.StateInputPort
BioChemicalTreatment.ProcessSimulator.StateOutputPort
BioChemicalTreatment.ProcessSimulator.assemble_chains
BioChemicalTreatment.ProcessSimulator.assemble_port_metadata_fields
BioChemicalTreatment.ProcessSimulator.find_reference_chains
BioChemicalTreatment.ProcessSimulator.get_blocking
BioChemicalTreatment.ProcessSimulator.get_connected
BioChemicalTreatment.ProcessSimulator.get_default_or_guess
BioChemicalTreatment.ProcessSimulator.get_element_chains
BioChemicalTreatment.ProcessSimulator.get_element_positions
BioChemicalTreatment.ProcessSimulator.get_influent_source
BioChemicalTreatment.ProcessSimulator.get_label
BioChemicalTreatment.ProcessSimulator.get_name
BioChemicalTreatment.ProcessSimulator.get_namespaced_property
BioChemicalTreatment.ProcessSimulator.get_node_style
BioChemicalTreatment.ProcessSimulator.get_port_label
BioChemicalTreatment.ProcessSimulator.get_portmetadata
BioChemicalTreatment.ProcessSimulator.get_tikzpicture_body
BioChemicalTreatment.ProcessSimulator.get_tikzpicture_options
BioChemicalTreatment.ProcessSimulator.inchain
BioChemicalTreatment.ProcessSimulator.init_port_metadata
BioChemicalTreatment.ProcessSimulator.insert_column!
BioChemicalTreatment.ProcessSimulator.insert_elem!
BioChemicalTreatment.ProcessSimulator.isempty_column
BioChemicalTreatment.ProcessSimulator.lstrip_namespace
BioChemicalTreatment.ProcessSimulator.metadata_connections
BioChemicalTreatment.ProcessSimulator.metadatachain_to_elementchain
BioChemicalTreatment.ProcessSimulator.order_chains_horizontally
BioChemicalTreatment.ProcessSimulator.order_connections
BioChemicalTreatment.ProcessSimulator.realports_from_combinedport
BioChemicalTreatment.ProcessSimulator.remove_column!
BioChemicalTreatment.ProcessSimulator.remove_elem!
BioChemicalTreatment.ProcessSimulator.remove_empty_chains!
BioChemicalTreatment.ProcessSimulator.remove_empty_columns!
BioChemicalTreatment.ProcessSimulator.rstrip_namespace
BioChemicalTreatment.ProcessSimulator.set_port_metadata!
BioChemicalTreatment.ProcessSimulator.split_namespace
BioChemicalTreatment.ProcessSimulator.squeeze_vertically!
BioChemicalTreatment.ProcessSimulator.straightify_verticals!
BioChemicalTreatment.ProcessSimulator.strip_chain!
BioChemicalTreatment.ProcessSimulator.strip_chains!
BioChemicalTreatment.ProcessSimulator.strip_dependencies
BioChemicalTreatment.ProcessSimulator.strip_namespace
BioChemicalTreatment.ProcessSimulator.symbolic_to_namestring
BioChemicalTreatment.ProcessSimulator.to_ordered_array
BioChemicalTreatment.ProcessSimulator.typestr
BioChemicalTreatment.Reactions.parameter_expressions
BioChemicalTreatment.Reactions.parameter_value_or_expression
BioChemicalTreatment.Reactions.parameter_values
BioChemicalTreatment.Reactions.parse_compmat
BioChemicalTreatment.Reactions.parse_equation
BioChemicalTreatment.Reactions.parse_params
BioChemicalTreatment.Reactions.parse_processrates
BioChemicalTreatment.Reactions.parse_states
BioChemicalTreatment.Reactions.parse_stoichmat
BioChemicalTreatment.Reactions.parse_variable
BioChemicalTreatment.Reactions.symbolic_to_namestring
BioChemicalTreatment.ProcessSimulator.@reactor
ProcessSimulator Internal API
ProcessElement Interface
BioChemicalTreatment.ProcessSimulator.AbstractProcessElement
— TypeAbstractProcessElement
An abstract type for every flow element for implementing generic functionality.
This abstract type is used for implementing generic functionality needed for every flow element, e.g. Reactor
, FlowElement
or Process
. It defines the default interface and some general functions on them.
As this library is designed to be fully compatible with ModelingToolkit
all flow elements should be convertible to an ODESystem
. This abstract type then implements several functions from ModelingToolkit
by falling back to this conversion. Further it implements the most general interface consisting of:
Extended help
Implementation
If implementing a type inheriting from this, the Base.convert(::Type{<:ODESystem}, x::AbstractProcessElement)
should be implemented for the new type, and it should return a ModelingToolkit.ODESystem
.
Further, where applicable, the following as well:
BioChemicalTreatment.ProcessSimulator.typestr
— Functiontypestr(sys::AbstractProcessElement)
Get a string representing the type of the processelement. Added to the type to carry additional data.
Defaults to the typestr
field of sys
or the real type.
Base.convert
— Functionconvert(::Type{<:ODESystem}, x::AbstractProcessElement)
Convert the AbstractProcessElement x to an ModelingToolkit.ODESystem
.
Extended Help
Implementation
This function must be implemented for each type inheriting from AbstractProcessElement
. It takes one instance of the new type and return a ModelingToolkit.ODESystem
.
Reactor
BioChemicalTreatment.ProcessSimulator.@reactor
— Macro@reactor function NAME(ARGS; KWARGS)
# Reactor implementation
# Returns a Reactor, takes the `states` as arg
# and `name` as kwarg
end
Given the Base implementation of a reactor, needs to be based on the states
in the reactor, given as argument called states
, automatically create methods to call the same reactor with one or multiple processes, which are then automatically connected.
Conditions for the provided function:
- A vector of states is provided to the reactor in an argument called
states
. It can be of typeVector
, but cannot have a default value. - The function must take the name of the resulting reactor as kwarg called
name
. Can be of typeSymbol
, but cannot have a default value.
Results in defining a function with the given name and two methods:
- The function as given
- A method for providing a
Process
orVector{Process}
instead of thestates
. Makes all processes internal to the reactor and automatically connected, adds the default processes as well. See the connection implementation.
Documents the added function as well. For this, the docstring of the original function is reused with the following modifications:
Replace the function call with the appropriate arguments
Replace everything beginning with:
'The system defines the mass flow'
by
'This function constructs a REACTORNAME, directly with the supplied processes in it. If `usedefault_processes = true`, the default processes are added, which enables easy addition of processes to the default ones as well as calling this function without any process.'
Change of
states
parameter documentation byprocesses
Addition of parameter documentation for
state_mapping
anduse_default_processes
Defaults
BioChemicalTreatment.ProcessSimulator.DEFAULT_PROCESSES
— ConstantDEFAULT_PROCESSES
Collection of functions to be called for generating the default processes for new reactors.
BioChemicalTreatment.ProcessSimulator.DEFAULT_PROCESSES_NAMES
— ConstantDEFAULT_PROCESSES_NAMES
Collection of names for the default processes for new reactors.
BioChemicalTreatment.ProcessSimulator.DEFAULT_PROCESSES_KWARGS
— ConstantDEFAULT_PROCESSES_KWARGS
Collection of function keyword arguments to generate the default processes for new reactors.
BioChemicalTreatment.ProcessSimulator.DEFAULT_PROCESSES_ARGS
— ConstantDEFAULT_PROCESSES_ARGS
Collection of function arguments to generate the default processes for new reactors.
BioChemicalTreatment.ProcessSimulator.DEFAULT_STATE_MAPPING
— ConstantDEFAULT_STATE_MAPPING::AbstractDict{Symbol, <:AbstractSet}
The matting from STATE_NAME => Set of states which are mapped to it.
Ports
BioChemicalTreatment.ProcessSimulator.InflowPort
— FunctionInflowPort(components; initial_flow, initial_concentrations, set_guess, name)
A connector for connecting inflows carrying components.
Parameters:
components
: The components in the flow. Can be provided as:- A
Dict
with keys being Strings representing the names of the components in the flow and values being Boolean meaning if the component is particulate or not. Does not include the flow rate. - An array of symbols for the components. Name and if particulate or not are taken from these symbols. May or may not include the the flow rate
q
.
- A
initial_flow
: The initial value for the flow rateq
. Defaults to 0.initial_concentrations
: Initial concentrations of the components in the flow, supports array and variable map (keys can be string, symbol or symbolic variables). Defaults to 0 for each component or the default values of the symbols if given as symbols.set_guess
: Initial values set only guesses. This is to prevent contradicting initial conditions. Defaults to true.
States:
q
: The flow rate- A state for each component specified by the
components
parameter.
BioChemicalTreatment.ProcessSimulator.OutflowPort
— FunctionOutflowPort(components; initial_flow, initial_concentrations, set_guess, name)
A connector for connecting outflows carrying components.
Parameters:
components
: The components in the flow. Can be provided as:- A
Dict
with keys being Strings representing the names of the components in the flow and values being Boolean meaning if the component is particulate or not. Does not include the flow rate. - An array of symbols for the components. Name and if particulate or not are taken from these symbols. May or may not include the the flow rate
q
.
- A
initial_flow
: The initial value for the flow rateq
. Defaults to 0.initial_concentrations
: Initial concentrations of the components in the flow, supports array and variable map (keys can be string, symbol or symbolic variables). Defaults to 0 for each component or the default values of the symbols if given as symbols.set_guess
: Initial values set only guesses. This is to prevent contradicting initial conditions. Defaults to true.
States:
q
: The flow rate- A state for each component specified by the
components
parameter.
BioChemicalTreatment.ProcessSimulator.ReactionInputPort
— FunctionReactionInputPort(components; initial_rates, name)
A connector for connecting reaction rates as input.
Parameters:
components
: The components. Can be provided as:- An array of Strings for the component names.
- An array of symbols for the components, from which the names are taken.
initial_rates
: The initial reaction rates, supports array and variable map (keys can be string, symbol or symbolic variables). Defaults to 0 for all rates or the default value if given as symbolic.set_guess
: Initial values set only guesses. This is to prevent contradicting initial conditions. Defaults to true
States:
- A state for each reaction rate specified by the
components
parameter.
BioChemicalTreatment.ProcessSimulator.ReactionOutputPort
— FunctionReactionOutputPort(components; initial_rates, name)
A connector for connecting reaction rates as output.
Parameters:
components
: The components. Can be provided as:- An array of Strings for the component names.
- An array of symbols for the components, from which the names are taken.
initial_rates
: The initial reaction rates, supports array and variable map (keys can be string, symbol or symbolic variables). Defaults to 0 for all rates or the default value if given as symbolic.set_guess
: Initial values set only guesses. This is to prevent contradicting initial conditions. Defaults to true
States:
- A state for each reaction rate specified by the
components
parameter.
BioChemicalTreatment.ProcessSimulator.StateInputPort
— FunctionStateInputPort(states; initial_states, name)
A connector for connecting reactor states as input.
Parameters:
states
: The states. Can be provided as:- A
Dict
with keys being Strings representing the names of the states and values being a named tuple with fieldspar
Boolean or nothing meaning if the component is particulate or not (or not a component). - An array of symbols for the components. Name and if particulate or not are taken from these symbols.
- A
initial_states
: Initial states, supports array and variable map (keys can be string, symbol or symbolic variables). Defaults to 0 for each state or the default values of the symbols if given as symbols.set_guess
: Initial values set only guesses. This is to prevent contradicting initial conditions. Defaults to true
States:
- A state for each state specified by the
states
parameter
BioChemicalTreatment.ProcessSimulator.StateOutputPort
— FunctionStateOutputPort(states; initial_states, name)
A connector for connecting reactor states as output.
Parameters
states
: The states. Can be provided as:- A
Dict
with keys being Strings representing the names of the states and values being Boolean or nothing meaning if the component is particulate or not (or not a component). - An array of symbols for the components. Name and if particulate or not are taken from these symbols.
- A
initial_states
: Initial states, supports array and variable map (keys can be string, symbol or symbolic variables). Defaults to 0 for each state or the default values of the symbols if given as symbols.set_guess
: Initial values set only guesses. This is to prevent contradicting initial conditions. Defaults to true
States:
- A state for each state specified by the
states
parameter
PortMetadata
BioChemicalTreatment.ProcessSimulator.PortMetadata
— TypePortMetadata
Struct to be associated with a Port-type ODESystem
. Contains:
name
: Port Nametype
: Port typeisinput
: If the port is an inputindex
: The number of the portparent
: The parent systemother
: Any other metadata
BioChemicalTreatment.ProcessSimulator.init_port_metadata
— Functioninit_port_metadata(port)
Initialize the metadata of an ODESystem with a PortMetadata struct to save port metadata.
Does not mutate the object inplace and needs to be reassigned afterwards.
BioChemicalTreatment.ProcessSimulator.set_port_metadata!
— Functionset_port_metadata!(port, pe::AbstractProcessElement)
Set the metadata of port
which is an connector of the processelement pe
according to its position in the process element.
set_port_metadata!(pe::AbstractProcessElement, getter_function)
Set the metadata of all ports of pe
which are output by applying the getter_function
on it. The metadata is set according to its position in the process element.
Usually, the getter_function
is one of
states
: Set the metadata of all states in the systemrates
: Set the metadata of all rates in the systeminflows
: Set the metadata of all inflows in the systemoutflows
: Set the metadata of all outflows in the systemexogenous_inputs
: Set the metadata of all exogenous_inputs in the systemexogenous_outputs
: Set the metadata of all exogenous_outputs in the system
set_port_metadata!(pe::AbstractProcessElement)
Set the metadata all connectors in the processelement pe
according to the type and it position in the process element.
BioChemicalTreatment.ProcessSimulator.assemble_port_metadata_fields
— Functionassemble_port_metadata_fields(port, pe::AbstractProcessElement)
Get the metadata fields of port
which is an connector of the processelement pe
according to its position in the process element. Throws an error if the port is not found in any of the connectors of the process element pe
.
assemble_port_metadata_fields(port, parent::ModelingToolkit.AbstractSystem; port_type = :unknown)
Get the metadata fields of port
which is an connector of the system pe
. The fields are assigned as followed
name
: The name of theport
with the name of the parent stripped on the left.type
: The givenport_type
argument. Defaults to :unknown.isinput
:false
if the any part of the namespace in the name (without the parent) starts with the regexr"[Oo]ut(put)?"
. Otherwisetrue
-> Default treated asindex
: If any part of the namespace in the name (without the parent) ends with a number, the last of these numbers is the index. If none, default to 1.parent
: The given parent system
Throws a warning if the name of the port does not start with the name of the parent.
Utilities
BioChemicalTreatment.ProcessSimulator.get_default_or_guess
— Functionget_default_or_guess(x, default=nothing)
Get default if assigned or a guess. Uses get_default_or_guess
from ModelingToolkit and extends it with the possibility of specifying a default value.
BioChemicalTreatment.ProcessSimulator.get_influent_source
— Functionget_influent_source(state, val)
Internal function to get a source block for a influent depending on the type.
BioChemicalTreatment.ProcessSimulator.to_ordered_array
— Functionto_ordered_array(order, vals; default = 0)
A helper function to transform the values to a array with elements ordered as given by order
. Supports a array or variable maps (keys as Symbols, Strings or Symbolic variables). The default gets applied to all elements in order
, which do not have a corresponding entry in vals
.
If default = nothing
, no defaults are applied and an error is thrown if no value is supplied for any element in order
.
BioChemicalTreatment.ProcessSimulator.realports_from_combinedport
— Functionrealports_from_combinedport(port::ODESystem; name, output_port = false)
Helper function for StandardLibrary Ports compatibility: Generates a RealInput/RealOutput for each state in the port. Returns a ODESystem with the equations for setting the new ports and the corresponding state equal and the newly generated In-/Outputs
BioChemicalTreatment.ProcessSimulator.symbolic_to_namestring
— Functionsymbolic_to_namestring(sym; namespace_separator=ModelingToolkit.NAMESPACE_SEPARATOR)
A helper function to get the name of a symbol as string. Removes all namespacing (split by namespace_separator
) and dependencies. E.g.
julia> sym
"system₊component₊variable(t)"
julia> ProcessSimulator.symbolic_to_namestring(sym)
"variable"
BioChemicalTreatment.ProcessSimulator.strip_dependencies
— Functionstrip_dependencies(sym)
Strip the dependencies (e.g. t, in brackets (t)
) of sym
. If sym
isa Symbolics variable, it's name is taken. E.g.
julia> sym
"system₊component₊variable(t)"
julia> ProcessSimulator.strip_dependencies(sym)
"system₊component₊variable"
Works independent of the number of arguments in the dependencies:
julia> sym
"system₊component₊variable(t, x, y)"
julia> ProcessSimulator.strip_dependencies(sym)
"system₊component₊variable"
Or as well for Symbols (or everything convertable to a string) and without any dependency:
julia> sym
:system₊component₊parameter
julia> ProcessSimulator.strip_dependencies(sym)
"system₊component₊parameter"
BioChemicalTreatment.ProcessSimulator.lstrip_namespace
— Functionlstrip_namespace(sym, str; namespace_separator=ModelingToolkit.NAMESPACE_SEPARATOR)
Removes the namespacing matching str
of the beginning of symbol name of sym
. Namespacing is separated by namespace_separator
. E.g.
julia> sym
"system₊component₊subcomponent"
julia> ProcessSimulator.lstrip_namespace(sym, "system")
"component₊subcomponent"
julia> ProcessSimulator.lstrip_namespace(sym, "system₊component")
"subcomponent"
If sym
does not contain str
in the beginning, nothing is stripped (no matter of how much matches) and the input is returned as a whole:
julia> ProcessSimulator.lstrip_namespace(sym, "system₊component1")
"system₊component₊subcomponent"
BioChemicalTreatment.ProcessSimulator.rstrip_namespace
— Functionrstrip_namespace(sym, str; namespace_separator=ModelingToolkit.NAMESPACE_SEPARATOR)
Removes the namespacing matching str
of the end of symbol name of sym
. Namespace separator is set by namespace_separator
E.g.
julia> sym
"system₊component₊subcomponent"
julia> ProcessSimulator.rstrip_namespace(sym, "subcomponent")
"system₊component"
julia> ProcessSimulator.rstrip_namespace(sym, "component₊subcomponent")
"system"
If sym
does not contain str
in the beginning, nothing is stripped (no matter of how much matches) and the input is returned as a whole:
julia> ProcessSimulator.rstrip_namespace(sym, "component1₊subcomponent")
"system₊component₊subcomponent"
BioChemicalTreatment.ProcessSimulator.split_namespace
— Functionsplit_namespace(sym; namespace_separator=ModelingToolkit.NAMESPACE_SEPARATOR)
Get the namespace parts of a symbol as strings. The namespace is assumed to be split by the given namespace_separator
. Defaults to ModelingToolkit.NAMESPACE_SEPARATOR
. E.g.
julia> sym
"system₊component₊subcomponent"
julia> ProcessSimulator.split_namespace(sym)
3-element Vector{SubString{String}}:
"system"
"component"
"subcomponent"
julia> sym = "system.component.subcomponent"
"system.component.subcomponent"
julia> ProcessSimulator.split_namespace(sym, namespace_separator='.')
3-element Vector{SubString{String}}:
"system"
"component"
"subcomponent"
BioChemicalTreatment.ProcessSimulator.get_namespaced_property
— Functionget_namespaced_property(value, name; namespace_separator=ModelingToolkit.NAMESPACE_SEPARATOR)
Get a propery of value
, where name
can contain namespacing separated by namespace_separator
. Works on any value. E.g.
julia> a
(b = (c = 2,),)
julia> BioChemicalTreatment.ProcessSimulator.get_namespaced_property(a, "b.c"; namespace_separator='.')
2
julia> BioChemicalTreatment.ProcessSimulator.get_namespaced_property(a, "b/c"; namespace_separator='/')
2
ProcessDiagram
BioChemicalTreatment.ProcessSimulator.metadatachain_to_elementchain
— Functionmetadatachain_to_elementchain(chains::AbstractVector{<:AbstractVector})
Transform each PortMetadata
in the chains to its parent. If the element is not a PortMetadata
, leave as it is.
BioChemicalTreatment.ProcessSimulator.remove_elem!
— Functionremove_elem!(pd, chain_idx, column_idx)
Remove the element at (chain_idx
, column_idx
) and return it. If there is none, return missing
. If an element is removed, it is replaced by missing
and no elements are shifted.
chain_idx
is the row, column_idx
the column index. The indices are relative to a grid and start from the top left (treated including empty positions).
This can mess up the ProcessDiagram
and make it incomplete if a non-missing element is removed.
BioChemicalTreatment.ProcessSimulator.isempty_column
— Functionisempty_column(pd::ProcessDiagram, column_idx::Integer)
Get if the column at column_idx
is empty (does not contain any systems.) The indices are 1-based and from the left.
BioChemicalTreatment.ProcessSimulator.strip_namespace
— Functionstrip_namespace(sym; namespace_separator=ModelingToolkit.NAMESPACE_SEPARATOR)
Removes the entire namespacing of the symbol name of sym
. Namespace is separated by namespace_separator
. E.g.
julia> sym
"system₊component₊subcomponent"
julia> ProcessSimulator.strip_namespace(sym)
"subcomponent"
BioChemicalTreatment.ProcessSimulator.get_label
— Functionget_label(elem)
Get the node label for printing using Latex/Tikz. Is the text printed within the node.
BioChemicalTreatment.ProcessSimulator.remove_column!
— Functionremove_column!(pd::ProcessDiagram, column_idx::Integer)
Remove column column_idx
from the ProcessDiagram
and return it. The indices are 1-based and from the left. Indices of columns with indices >= column_idx
are decreased by 1
.
This can mess up the ProcessDiagram
and make it incomplete if non-empty columns are removed.
BioChemicalTreatment.ProcessSimulator.remove_empty_columns!
— Functionremove_empty_columns!(pd)
Remove all empty columns in the ProcessDiagram
pd
.
BioChemicalTreatment.ProcessSimulator.inchain
— Functioninchain(elem, chains)
Get if elem
is contained in the chains
.
inchain(elem::PortMetadata, chains)
Get if the parent of elem
is contained in the chains
.
BioChemicalTreatment.ProcessSimulator.strip_chain!
— Functionstrip_chain!(pd, chain_idx)
Strip the chain (row) at chain_idx
by removing all leading and trailing missing
and adapting the shift to retain the same positioning.
The indices are 1-based and start from the top.
BioChemicalTreatment.ProcessSimulator.get_tikzpicture_body
— Functionget_tikzpicture_body(pd::ProcessDiagram)
Get the body for the tikzpicture showing pd
.
Does the heavylifting for making a tikzpicture from the ProcessDiagram
.
Contains placing all nodes and the connections inbetween.
Places the nodes according to the ordering in pd
and considers it for drawing the connections inbetween.
BioChemicalTreatment.ProcessSimulator.get_portmetadata
— Functionget_portmetadata(sys, systems)
Get the port metadata of the system.
If it is already in the metadata of sys
, simply return it.
If not, find the parent system in systems
and use assemble_port_metadata_fields
to get the associated metadata. A system in systems
is considered a potential parent of sys
if:
- The name of
sys
(including namespacing) starts with the name of the parent - If there are multiple, take the one with the shortest name (the most upper parent)
BioChemicalTreatment.ProcessSimulator.find_reference_chains
— Functionfind_reference_chains(chains)
Find the indices of the reference chains to start horizontally ordering the other chains.
Chosen as (first that matches):
- First and last elements in the chain do not occur in other chains (multiple can match).
- First **or"" last element in the chain does not match another chain. If multiple, pick the longest one.
- Pick the longest chain
BioChemicalTreatment.ProcessSimulator.strip_chains!
— Functionstrip_chains!(pd)
Strip all chains in the ProcessDiagram
pd
. See strip_chain!
for details on stripping.
BioChemicalTreatment.ProcessSimulator.get_blocking
— Functionget_blocking(pd::ProcessDiagram, chain_idx::Integer, ref_chain::Integer)
Get a boolean array which is true where the chain at index idx
blocks the space for the elements in ref_chain
. A chain is blocking each index which is:
- Occupied by an element
- A connection going to the left/right, but not to the reference chain
BioChemicalTreatment.ProcessSimulator.insert_elem!
— Functioninsert_elem!(pd, chain_idx, column_idx, elem)
Insert the element elem
at (chain_idx
, column_idx
). All elements right of this one, until the next missing one, are shifted to the right.
chain_idx
is the row, column_idx
the column index. The indices are relative to a grid and start from the top left (treated including empty positions).
This can mess up the ProcessDiagram
and make it incomplete if a non-existant element is inserted.
BioChemicalTreatment.ProcessSimulator.order_connections
— Functionorder_connections(pd, elem)
Order the connections of the node elem
in direction of connection (left, right, up, down) and edge direction.
Is used for nice printing of the connections, that they exit the node at a good position.
Returns an Array of Arrays (similar to 2d-matrix), each element of which is associated with a direction for the connection and a direction of the first edge. The ordering is as follows:
Node edge \ first bend direction | left or up | straight | down or right |
---|---|---|---|
left | left-up | left-straight | left-down |
right | right-up | right-straight | right-down |
top | top-left | top-straight | top-right |
bottom | bottom-left | bottom-straight | bottom-right |
Each element in this table is a vector of connections (tuples of the connected port metadata, always the one being part of elem.parent
first) which have this property.
BioChemicalTreatment.ProcessSimulator.get_name
— Functionget_name(elem)
Get the node name for printing using Latex/Tikz. Needs to be a unique key to refer to in the diagram.
BioChemicalTreatment.ProcessSimulator.get_node_style
— Functionget_node_style(elem; opts=missing, get_def=false)
Get the node style for printing the element using Latex/Tikz. Determines the styling of the resulting node. Parameters for it are given in opts
. Check ProcessDiagram
for the defined options.
If get_def=true
, return the style definition (i.e. what should be inserted as STYLEOPTIONS into `\tikzstyle{name} = [STYLEOPTIONS]`), otherwise only the name.
If get_def = true
, opts must be given as well.
Defaults to a simple rectangular block.
If you add new styles, make sure that the resulting block has:
- width:
blockwidth
- height:
blockheight
as otherwise the plotting will not match. If you need the text width,
BioChemicalTreatment.ProcessSimulator.remove_empty_chains!
— Functionremove_empty_chains!(pd)
Remove all empty chains (rows) in the ProcessDiagram
pd
.
BioChemicalTreatment.ProcessSimulator.get_element_positions
— Functionget_element_positions(chains, shifts, elem)
Get all positions where elem
is found in chains
considering the chain shifts in shifts
.
Returns a vector with tuples of the form (chain_idx
, column_idxs
) for each chain containing elem
. chain_idx
is the index of the chain, column_idxs
is an array with all columns in this chain which contain this elem.
get_element_positions(pd::ProcessDiagram, elem)
Get all positions where elem
is found in the chains of pd
. See get_element_positions
for details.
BioChemicalTreatment.ProcessSimulator.assemble_chains
— Functionassemble_chains(metadata_connections; conn_type_precedence = [[:flow], [:state, :rate, :exogenous, :unknown]])
Assemble chains (considered straight rows) of systems from the given metadata connections and directly order them vertically.
Parameters
metadata_connections
: The connections that define theProcessDiagram
.conn_type_precedence
: The precedences between the different connectors. First only connections of the first precedence type are considered, then of the second etc. Is a vector of vectors the inner of which contain the types of same precedence.
Assembling a chain
To assemble a chain it is proceeded as follows (details below):
- Find a first element for the chain
- Find next elements in the chain
- Order the chain vertically into the already assembled chains
Finding the first element
Determined as (First rule that matches a system):
- Systems that have only outputs and no inputs (in this precedence type)
- Any that has an output (in this precedence type)
- Any that has a connector (in- or output) in this precedence type
If none is found, go to the next precedence types or stop
For all of these rules, they is always first checked for systems already in the chain before going to new ones.
Finding the next elements in the chain
Add new systems to the chain, until no matching is found anymore.
A new system is considered matching if and only if:
- The system is connected at output port 1 with a connection of the given precedence type.
- The system is not yet in the current chain
- The system is not yet in any already assembled chain
For the last element in the chain, an exception is made and it can be already in an assembled chain or the first one of the current chain.
Like this, the first and last elements in each chain can occur in an other chain and then indicate that there is a connection between them. This is later used to horizontally order the chains.
Ordering the chain vertically into the already ordered chains
This is done using the following heuristic:
Default to the bottom (last index), then move up one by one as long as any of the following holds (checked in this order):
- No connection to the next upper one and none to any of the lower ones
- No connection to any lower one but a connection between any lower one and an upper one
- No connection to any lower one but the next upper one is connected to a lower one
BioChemicalTreatment.ProcessSimulator.get_element_chains
— Functionget_element_chains(elem, chains::AbstractVector)
Get all indices of the chains (row indices) which contain elem
.
get_element_chains(elem::PortMetadata, chains::AbstractVector)
Get all indices of the chains (row indices) which contain the parent of elem
.
BioChemicalTreatment.ProcessSimulator.insert_column!
— Functioninsert_column!(pd::ProcessDiagram, column_idx::Integer, column::AbstractArray)
Insert column
at column_idx
into the ProcessDiagram
. The indices are 1-based and from the left. Indices of columns with indices >= column_idx
are increased by 1
.
This can mess up the ProcessDiagram
and make it incomplete if new systems are inserted.
BioChemicalTreatment.ProcessSimulator.metadata_connections
— Functionmetadata_connections(eqns, systems)
Transform the connection equations (made using connect()
) to tuples of PortMetadata
of the connected ports.
Considers only "connection" equations (the ones made using connect()
) and all others are filtered out
BioChemicalTreatment.ProcessSimulator.order_chains_horizontally
— Functionorder_chains_horizontally(chains)
Get the horizontal shifts to move the chains on the grid.
Logic:
- Find a reference chain (see
find_reference_chains
) - While not all chains are ordered, pick a random (not yet ordered) chain to order from a pot (initially containing all unordered chains)
- If the first or last element of this chain occurs in the middle of any unordered chain
- Remove this one from the pot
- Put the ones this connects to the middle of back into the pot
- Pick a new one
- If there is no match of the first or last element in this chain with an already ordered one
- Remove this one from the pot
- If there are no new ones to pick, pick new reference chains from all not yet ordered ones and put all not yet ordered ones back into the pot
- Pick a new one
- Find the horizontal index of the first and last elements in the already ordered chains
- If the chain has only one or two elements and all of them are contained in others
- Remove it (as it is an internal connection of another chain)
- Pick a new one
- If only one end of the chain matches
- Make it the first one (reverse if it is the last one)
- Insert missing columns of the length of this chain (no vertical overlap with any except the first element)
- Remove the first element from the chain (the same as contained in the already ordered chain)
- Set the shift to have the first element overlapping with the matching one
- If both ends of the chain match
- If the first is more to the right as the last
- Reverse the chain
- If neccessary, insert as much columns s.t. the first and last element can match
- Remove the first and last elements from the chain (the same as contained in the already ordered chain)
- Set the shift to have the first element overlapping with the matching one
- If the first is more to the right as the last
- If the first or last element of this chain occurs in the middle of any unordered chain
The random picking of chains is done intelligently: If a chain is not yet to be ordered, it is removed from the pot. As soon as a new chain has been ordered, all non-ordered chains are put back into the pot.
Returns the new chains (redundant removed and reversed where neccessary) and the shifts to shift the chains horizontally.
BioChemicalTreatment.ProcessSimulator.get_port_label
— Functionget_port_label(elem)
Get the node label for printing of the port using Latex/Tikz. Is the text printed within the port node.
BioChemicalTreatment.ProcessSimulator.squeeze_vertically!
— Functionsqueeze_vertically!(pd::ProcessDiagram)
Squeeze the process diagram in the vertical direction by merging neighbouring chains as long as possible.
Merging is considered possible as long as:
- There are no overlaps between systems
- There is no connection:
- From the first to the left to an element further than (or at same distance) the last of the other chain
- From the last to the right to an element further than (or at same distance) the first of the other chain
Repeated iteratively, as long as chains can be merged.
BioChemicalTreatment.ProcessSimulator.get_tikzpicture_options
— Functionget_tikzpicture_options(pd::ProcessDiagram)
Get the options for the tikzpicture showing pd
.
BioChemicalTreatment.ProcessSimulator.straightify_verticals!
— Functionstraightify_verticals!(pd)
Shift full chains to make the vertical connections in the ProcessDiagram
as straight as possible.
Heuristic
Go over all chains and do (in this sequence):
- If one of the endings is not connecting (starting/ending node) -> do not shift
- If both ends are connected in one and the same direction only -> shift by the minimal shift in this direction
- If no end has a non-straight connection to a middle element -> do not shift
- If any end has a non-straight connection to a middle element and all of those are in the same direction -> shift by the most often occuring shift in this direction
BioChemicalTreatment.ProcessSimulator.get_connected
— Functionget_connected(conn_props, elem)
Get all elements connected by the conn_props
to the given element elem
.
get_connected(conn_props, elem::PortMetadata)
Get all elements connected by the conn_props
to the parent of elem
.
Reactions Internal API
BioChemicalReactionSystem
BioChemicalTreatment.Reactions.parameter_expressions
— Functionparameter_expressions(sys::BioChemicalReactionSystem, default=0; kwargs...)
Return a dict mapping parameters associated with expressions to the corresponding expression. Defaults for each parameter can be overridden in kwargs
, overall default (if nothing else provided) is set by default
.
BioChemicalTreatment.Reactions.parameter_values
— Functionparameter_values(sys::BioChemicalReactionSystem, default=0; kwargs...)
Return a dict mapping parameters to the corresponding values. For parameters associated with expressions, the expression is evaluated. Currently only works if there are no dependencies of expressions on other non-numeric parameters. Defaults for each parameter can be overridden in kwargs
, overall default (if nothing else provided) is set by default
.
BioChemicalTreatment.Reactions.parameter_value_or_expression
— Functionparameter_value_or_expression(sys::BioChemicalReactionSystem, default=0; kwargs...)
Get the value or expression for each parameter of the given sys
. Defaults can be overridden by providing corresponding kwargs
.
Returns a dict with keys being the parameters and values the corresponding value or expression. For values, the following values are taken (always the first in the list that exists):
- Value or expression provided in
kwargs
- Default value provided in system
- Default expression provided in system
default
parameter provided to this function
Parsing
BioChemicalTreatment.Reactions.DEFAULT_PARSING_CONTEXT
— ConstantDEFAULT_PARSING_CONTEXT
The default context used for parsing expressions. Contains only mathematical functions.
This is done to restrict to mathematical functions which hopefully should prevent remote code execution problems. (Generally anyways no problem as only trusted models should be included. But add additional layer of security)
BioChemicalTreatment.Reactions.parse_equation
— Functionparse_equation(eqn, symbols; context::AbstractDict = DEFAULT_PARSING_CONTEXT)
Parse an equation given as Expr or String. Only Numbers, Variables given in symbols
, and functions from context
are parsed. If a symbol not found in any of those, an error is thrown.
Purpose of this is to restrict available functionality when parsing strings. Should mitigate problematic remote code execution if symbols
and context
is chosen appropriately.
BioChemicalTreatment.Reactions.parse_variable
— Functionparse_variable(var;
dependency::AbstractArray{<:Union{Symbolics.BasicSymbolic, Symbolics.Num}} = Symbolics.Num[],
description::AbstractString = "", particulate::Union{Bool, Nothing} = nothing,
colloidal::Union{Bool, Nothing} = nothing, soluble::Union{Bool, Nothing} = nothing)
Parse the variable var
and return a Symbolics variable with this name. Keyword arguments to specify properties of it.
Available keyworks/properties:
- description: Description of the variable
- particulate: If the represented compound is particulate
- colloidal: If the represented compound is colloidal
- soluble: If the represented compound is soluble
BioChemicalTreatment.Reactions.parse_states
— Functionparse_states(statemat, header)
Parse the states given in matrix statemat
with header header
. Required fields are:
name
: The name of the statedescription
: The description of the stateparticle_size
: The particle size;particulate
,colloidal
orsoluble
. Multiple can be specified (just need to contain all of them)
BioChemicalTreatment.Reactions.parse_params
— Functionparse_params(paramsmat, header)
Parse the parameters given in matrix paramsmat
with header header
. Required fields are:
name
: The name of the parameterdescription
: The description of the parameter
Optional fields are:
expression
: An expression to set the parameter if no value is given. Can consist of the parameters and the functions inDEFAULT_PARSING_CONTEXT
.
BioChemicalTreatment.Reactions.parse_stoichmat
— Functionparse_stoichmat(stoichmat, mat_states, mat_rates, params)
Parse the stoichiometric matrix given in stoichmat
with columns specified by state variables mat_states
and rows by rate variables mat_rates
. It can contain numbers, the parameters in params
and mathematical functions in DEFAULT_PARSING_CONTEXT
.
BioChemicalTreatment.Reactions.parse_processrates
— Functionparse_processrates(statemat, header, states, params)
Parse the process rates given in matrix ratesmat
with header header
. Required fields are:
name
: The name of the parameterdescription
: The description of the parameterequation
: The rate equation. Allowed symbols are the variables defined instates
andparams
and the functions inDEFAULT_PARSING_CONTEXT
.
BioChemicalTreatment.Reactions.parse_compmat
— Functionparse_compmat(compmat, mat_states, composition_names, params)
Parse the composition matrix given in compmat
with columns specified by state variables mat_states
and the composition in the rows are named composition_names
. It can contain numbers, the parameters in params
and mathematical functions in DEFAULT_PARSING_CONTEXT
.
Utilities
BioChemicalTreatment.Reactions.symbolic_to_namestring
— Functionsymbolic_to_namestring(sym)
A helper function to get the name of a symbol as string cutting the dependencies.