Node Module

src/engine/node.py PLASMAG 2024 Software, LPP

class src.model.node.CalculationNode(name, engine, strategy: CalculationStrategy | None = None)

Bases: object

Represents a node in the calculation graph, capable of performing a calculation using a specified strategy and resolving dependencies dynamically.

There are two types of nodes in the calculation graph:

  • Leaf nodes: These nodes directly use input parameters without further calculation.

  • Internal nodes: These nodes use a calculation strategy to perform a calculation based on dependencies and

parameters.

Attributes:

name (str): Unique identifier for the node. engine (CalculationEngine): Reference to the engine that manages the calculation process. _strategy (CalculationStrategy, optional): The strategy used for calculation. May be None

for leaf nodes that use direct parameters.

Methods:

resolve_dependencies: Dynamically resolves (= get the value of) the dependencies required by the strategy. calculate: Performs the calculation based on the strategy and updates the results. set_strategy: Assigns a new calculation strategy to the node.

calculate() any

Checks if this node’s result is already calculated and if recalculation is not needed :return: The calculated value of this node. Can be a scalar or a tensor.

get_strategy()

Returns the calculation strategy associated with this node. :return: CalculationStrategy: The strategy used for calculations.

mark_for_recalculation()

Marks this node as needing recalculation, even if the value is already calculated. :return: None

resolve_dependencies() dict

Dynamically resolves and calculates the dependencies required by this node’s strategy.

This method iterates over the dependencies required by the strategy and get their values. It doesn’t calculate the value of the dependencies if they have already been calculated and stored in the CalculationResults class.

Returns:

dict: A dictionary where keys are dependency names and values are their calculated results.

set_strategy(strategy)

Assigns a new calculation strategy to this node and invalidates any previously calculated value, forcing a recalculation (to avoid corrupted results) with the new strategy on the next calculate call.

Parameters:

strategy (CalculationStrategy): The new strategy to use for calculations.