Project#

The Project class can be accessed through the Optislang instance. This class provides methods for obtaining information about the loaded project, its content and to execute operations on it:

from ansys.optislang.core import Optislang
from ansys.optislang.core import examples
from pathlib.Path import Path

example = examples.get_files("calculator_with_params")[1][0]
osl = Optislang(project_path=example)
osl.application.save_copy(Path.cwd() / "project_content.opf")
project = osl.application.project

# print project info
print(project)
# obtain these information directly
name = project.get_name()
location = project.get_location()
status = project.get_status()

Project structure#

The optiSLang project can be represented by a rooted tree structure. This structure consists of nodes and the connections between these nodes. On the top level, there is one node designated as a project root system. It is represented by the RootSystem instance. Each System , such as the RootSystem class or ParametricSystem class, has a get_nodes() method that returns all its direct children nodes. This provides the ability to determine the entire project structure.

The code shows how to go through all nodes in the project and print information about them:

# ...


def print_node_info(node):
    name = node.get_name()
    type_ = node.get_type()
    status = node.get_status()
    print(name, type_, status)


def process_nodes(nodes):
    for node in nodes:
        print_node_info(node)
        if isinstance(node, System):
            process_nodes(node.get_nodes())


root_system = project.root_system
nodes = root_system.get_nodes()
process_nodes(nodes)

Parameters#

To obtain defined parameters of any parametric system, an instance of the ParameterManager class can be used. This class contains the get_parameters(), method for returning tuple with detailed information for instances of the OptimizationParameter, StochasticParameter, MixedParameter, and DepenedentParameter classes.

The get_parameters_names() method returns a tuple with only the names of the parameters:

# ...

parameter_manager = root_system.parameter_manager
parameters = parameter_manager.get_parameters()
parameters_names = parameter_manager.get_parameters_names()

To add new parameter, use method add_parameter():

# ...

from ansys.optislang.core.project_parametric import OptimizationParameter

new_parameter = OptimizationParameter(
    name="new_parameter", reference_value=2.5, range=(-5, 10)
)
parameter_manager.add_parameter(new_parameter)

To modify parameter, use method modify_parameter(), or modify directly parameter property via modify_parameter_property(). Parameter name is used as identifier in both cases:

# ...

# create new instance of parameter with modified properties
from ansys.optislang.core.project_parametric import MixedParameter

modified_parameter = MixedParameter(
    name="new_parameter", reference_value=5, range=(0, 10)
)
parameter_manager.modify_parameter(modified_parameter)

# modify desired property directly
parameter_manager.modify_parameter_property(
    parameter_name="new_parameter",
    property_name="reference_value",
    property_value=2,
)

Criteria#

To obtain defined criteria of any parametric system, an instance of the CriteriaManager class can be used. This class contains the get_criteria(), method for returning tuple with detailed information for instances of the ConstraintCriterion, ObjectiveCriterion, LimitStateCriterion, and VariableCriterion classes.

# ...

criteria_manager = root_system.criteria_manager
criteria = criteria_manager.get_criteria()
criteria_names = criteria_manager.get_criteria_names()

To add new criterion, use method add_criterion():

# ...

from ansys.optislang.core.project_parametric import ConstraintCriterion, ComparisonType

new_criterion = ConstraintCriterion(
    name="new_criterion",
    expression="1",
    criterion=ComparisonType.LESSEQUAL,
    limit_expression="2",
)
criteria_manager.add_criterion(new_criterion)

To modify criterion, use method modify_criterion(), or modify directly criterion property via modify_criterion_property(). Criterion name is used as identifier in both cases:

# ...

# create new instance of criterion with modified properties
from ansys.optislang.core.project_parametric import LimitStateCriterion

modified_criterion = LimitStateCriterionCriterion(
    name="new_criterion",
    expression="2**2",
    criterion=ComparisonType.LESSLIMITSTATE,
    limit_expression="2^3",
)
criteria_manager.modify_criterion(modified_criterion)

# modify desired property directly
criteria_manager.modify_criterion_property(
    criterion_name="new_criterion",
    property_name="limit",
    property_value="2^2+1",
)

Responses#

To obtain defined responses of any parametric system, an instance of the ResponseManager class can be used. This class contains the get_responses(), method for returning tuple with detailed information for instance of the Response class.

# ...

response_manager = root_system.response_manager
responses = response_manager.get_responses()
responses_names = response_manager.get_responses_names()

When the Optislang instance is no longer needed, stop the connection with optiSLang server by running:

osl.dispose()