friendlysam.parts.Cluster

class friendlysam.parts.Cluster(*parts, resource=None, name=None)

A node containing other nodes, fully connected.

A cluster is used to create a free flow of a resource R among a set of nodes. All children of a cluster get their balance_constraints turned off for the resource R, and instead the cluster makes an aggregated balance constraint for all the nodes. In this way, a Cluster is like a FlowNetwork for resource R where all the parts are connected to one another.

Parameters:
  • *parts (optional) – Zero or more parts to put in the cluster.
  • resource – The resource this cluster handles.
  • name (optional) – A name for the cluster.

Examples

Let’s create three nodes:

>>> producer = Node(name='producer')
>>> producer.production['R'] = VariableCollection('prod')
>>> consumer = Node(name='consumer')
>>> consumer.consumption['R'] = VariableCollection('cons')
>>> storage = Storage(resource='R', name='storage')
>>> nodes = [consumer, producer, storage]

Now they all make a balance constraint at any given index:

>>> sum(len(n.constraints.make(5)) for n in nodes)
3

After clustering, they don’t make balance constraints:

>>> cluster = Cluster(*nodes, resource='R', name='cluster')
>>> sum(len(n.constraints.make(5)) for n in nodes) # They all make a balance constraint
0

But the Cluster does:

>>> for constr in cluster.constraints.make(5):
...     print(constr.expr)
...
prod(5) == cons(5) + storage.volume(6) - storage.volume(5)
Cluster.add_part(part) Add a part to this cluster.
Cluster.balance_constraints(index) Balance constraints for all resources.
Cluster.cluster(resource) Get a Cluster this node is in.
Cluster.find(name) Try to find a part by name.
Cluster.iter_times(start, *range_args) A generator yielding a sequence of times.
Cluster.iter_times_between(start, end) A generator yielding all times between two points.
Cluster.parts([depth, include_self]) Get contained parts, recursively.
Cluster.remove_part(part) Remove a part from this part.
Cluster.set_cluster(cluster) Add this node to a Cluster.
Cluster.state_variables(index) Cluster does not have state variables.
Cluster.step_time(index, num_steps) A function for stepping forward or backward in time.
Cluster.times(start, *range_args) Get a sequence of times.
Cluster.times_between(start, end) Get a tuple of all times between two points.
Cluster.unset_cluster(cluster) Remove from a Cluster.
Cluster.accumulation A dictionary of accumulation functions.
Cluster.children Parts in this part, excluding self.
Cluster.children_and_self Parts in this part, including self.
Cluster.constraints For defining and generating constraints.
Cluster.consumption A dictionary of consumption functions.
Cluster.descendants All children, children of children, etc, excluding self.
Cluster.descendants_and_self All children, children of children, etc, including self.
Cluster.inflows A dictionary of sets of inflow functions.
Cluster.name A name for the object.
Cluster.outflows A dictionary of sets of outflow functions.
Cluster.production A dictionary of production functions.
Cluster.resource The resource this cluster collects.
Cluster.resources The set of resources this node handles.
Cluster.time_unit The time unit used in step_time().