""" Entry point for the simulation API
This module is for managing the simulation side of MORe in a scripting environment.
"""
import typing
from abc import abstractmethod, ABC
from more.api.simulation.data_tables.data_table_setup import DataTableSetup
from more.api.simulation.load_cases.load_case_container_setup import LoadCaseContainerSetup
from more.api.simulation.studies.studies_setup import StudiesSetup
[docs]
class SimulationSetup(ABC):
"""Entry point for simulation API
Parameters
----------
proj : Project
The project instance
"""
def __init__(self, proj):
self._proj = proj
def _get_wrapped_object(self):
return self._proj.simulation
[docs]
@abstractmethod
def get_load_case_container_setup(self, name: str) -> LoadCaseContainerSetup:
""" Creates a :class:`~api.simulation.load_cases.load_case_container_setup.LoadCaseContainerSetup` for an already existing load case container
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
..
>>> _ = simulation_setup.create_load_case_container_setup().set_name(name='load_case_container_name') # Underscore necessary so that doctests do not fail
>>> load_case_container_setup = simulation_setup.get_load_case_container_setup(name='load_case_container_name')
..
>>> # Test assertions for doctest, will not be rendered in the documentation because of the '..' sphinx comment above
>>> len(proj.simulation.load_case_containers_container.elements) # Take into account Empty LCC
2
>>> proj.simulation.load_case_containers_container.elements[1].name
'load_case_container_name'
>>> from more.load_case_container.load_case_container.load_case_container import LoadCaseContainer
>>> isinstance(proj.simulation.load_case_containers_container.elements[1], LoadCaseContainer)
True
>>> load_case_container_setup.load_case_container == proj.simulation.load_case_containers_container.elements[1]
True
>>> simulation_setup.get_load_case_container_setup(name='non_existent_load_case_container_name')
Traceback (most recent call last):
...
more.api.exceptions.api_exception.NameNotFoundError: Item: non_existent_load_case_container_name not found, available names are: "dict_keys(['Empty', 'load_case_container_name'])"
Parameters
----------
name : str
The name of the existing load case container for which to return a :class:`~api.simulation.load_cases.load_case_container_setup.LoadCaseContainerSetup`
Returns
-------
:class:`~api.simulation.load_cases.load_case_container_setup.LoadCaseContainerSetup`
A :class:`~api.simulation.load_cases.load_case_container_setup.LoadCaseContainerSetup` object for the found load case container
Raises
------
NameNotFoundError
Raised if no load case container with the given name was found
"""
pass
[docs]
@abstractmethod
def get_data_table_setup(self, name: str) -> DataTableSetup:
""" Creates and returns a :class:`~api.simulation.data_tables.data_table_setup.DataTableSetup` for an already existing data table, if plugins are installed it automatically finds the correct data table setup object
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
..
>>> _ = simulation_setup.create_data_table_setup(data_table_type_name='Transient data table').set_name(name='data_table_name')
>>> data_table_setup = simulation_setup.get_data_table_setup(name='data_table_name')
..
>>> # Test assertions for doctest, will not be rendered because of the '..' sphinx comment above
>>> len(proj.simulation.data_table_container.elements)
1
>>> proj.simulation.data_table_container.elements[0].name
'data_table_name'
>>> from more.transient_data_table import TransientDataTable
>>> isinstance(proj.simulation.data_table_container.elements[0], TransientDataTable)
True
>>> data_table_setup.transient_data_table == proj.simulation.data_table_container.elements[0]
True
>>> simulation_setup.get_data_table_setup(name='non_existent_data_table_name')
Traceback (most recent call last):
...
more.api.exceptions.api_exception.NameNotFoundError: ...
Parameters
----------
name : str
The name of the existing data table for which to return a :class:`~api.simulation.data_tables.data_table_setup.DataTableSetup`
Returns
-------
:class:`~api.simulation.data_tables.data_table_setup.DataTableSetup`
A :class:`~api.simulation.data_tables.data_table_setup.DataTableSetup` object for the found data table
Raises
------
NotImplementedError
Raised if no :class:`~api.simulation.data_tables.data_table_setup.DataTableSetup` was implemented for the given data table, may happen when using plugins
NameNotFoundError
Raised if no data table with the given name was found
"""
pass
[docs]
@abstractmethod
def get_study_setup(self, name: str) -> StudiesSetup:
""" Creates and returns a :class:`~api.simulation.studies.studies_setup.StudiesSetup` for an already existing study
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
..
>>> _ = simulation_setup.create_study_setup().set_study_name(name='study_name')
>>> study_setup = simulation_setup.get_study_setup(name='study_name')
..
>>> # Test assertions for doctest, will not be rendered because of the '..' sphinx comment above
>>> len(proj.simulation.study_container.elements)
1
>>> proj.simulation.study_container.elements[0].name
'study_name'
>>> from more.tabular_study.tabular_study import TabularStudy
>>> isinstance(proj.simulation.study_container.elements[0], TabularStudy)
True
>>> study_setup._study == proj.simulation.study_container.elements[0]
True
>>> study_setup = simulation_setup.get_study_setup(name='non_existent_study_name')
Traceback (most recent call last):
...
more.api.exceptions.api_exception.NameNotFoundError: Item: non_existent_study_name not found, available names are: "dict_keys(['study_name'])"
Parameters
----------
name : str
The name of the existing study for which to return a :class:`~api.simulation.studies.studies_setup.StudiesSetup`
Returns
-------
:class:`~api.simulation.studies.studies_setup.StudiesSetup`
A :class:`~api.simulation.studies.studies_setup.StudiesSetup` object for the found study
Raises
------
NameNotFoundError
Raised if no study with the given name was found
"""
pass
[docs]
@abstractmethod
def create_load_case_container_setup(self) -> LoadCaseContainerSetup:
""" Creates a load case container and returns a corresponding :class:`~api.simulation.load_cases.load_case_container_setup.LoadCaseContainerSetup` object
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
>>> load_case_container_setup = simulation_setup.create_load_case_container_setup() \\
... .set_name(name='load_case_container_name')
..
>>> # Test assertions for doctest, will not be rendered in the documentation because of the '..' sphinx comment above
>>> len(proj.simulation.load_case_containers_container.elements) # Take into account Empty LCC
2
>>> proj.simulation.load_case_containers_container.elements[1].name
'load_case_container_name'
>>> from more.load_case_container.load_case_container.load_case_container import LoadCaseContainer
>>> isinstance(proj.simulation.load_case_containers_container.elements[1], LoadCaseContainer)
True
>>> load_case_container_setup.load_case_container == proj.simulation.load_case_containers_container.elements[1]
True
Returns
-------
:class:`~api.simulation.load_cases.load_case_container_setup.LoadCaseContainerSetup`
A :class:`~api.simulation.load_cases.load_case_container_setup.LoadCaseContainerSetup` object for the created load case container
"""
pass
[docs]
@abstractmethod
def create_data_table_setup(self, data_table_type_name: str = 'Transient data table') -> DataTableSetup:
""" Creates a data table and returns a corresponding :class:`~api.simulation.data_tables.data_table_setup.DataTableSetup` object
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
>>> # print(simulation_setup.get_available_data_table_types_names()) # Optionally Check which data table types are available
>>> data_table_setup = simulation_setup.create_data_table_setup(data_table_type_name='Transient data table')
..
>>> # Test assertions for doctest, will not be rendered because of the '..' sphinx comment above
>>> len(proj.simulation.data_table_container.elements)
1
>>> proj.simulation.data_table_container.elements[0].name
'Transient Data Table'
>>> from more.transient_data_table import TransientDataTable
>>> isinstance(proj.simulation.data_table_container.elements[0], TransientDataTable)
True
>>> data_table_setup.transient_data_table == proj.simulation.data_table_container.elements[0]
True
>>> data_table_setup = simulation_setup.create_data_table_setup(data_table_type_name='Non existent type name')
Traceback (most recent call last):
...
more.api.exceptions.api_exception.NameNotFoundError: ...
Parameters
---------
data_table_type_name: : str, default='Transient data table'
The name of the type of data table to add, if no name is given then a 'Transient data table' is created by default
Returns
-------
:class:`~api.simulation.data_tables.data_table_setup.DataTableSetup`
A :class:`~api.simulation.data_tables.data_table_setup.DataTableSetup` object for the created data table
Raises
------
NameNotFoundError
Raised if no data table type with the given name exists
"""
pass
[docs]
@abstractmethod
def create_study_setup(self) -> StudiesSetup:
""" Creates a study and returns a corresponding :class:`~api.simulation.studies.studies_setup.StudiesSetup` object
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
>>> study_setup = simulation_setup.create_study_setup()
..
>>> # Test assertions for doctest, will not be rendered because of the '..' sphinx comment above
>>> len(proj.simulation.study_container.elements)
1
>>> from more.tabular_study.tabular_study import TabularStudy
>>> isinstance(proj.simulation.study_container.elements[0], TabularStudy)
True
>>> study_setup._study == proj.simulation.study_container.elements[0]
True
Returns
-------
:class:`~api.simulation.studies.studies_setup.StudiesSetup`
A :class:`~api.simulation.studies.studies_setup.StudiesSetup` object for the created study
"""
pass
[docs]
@abstractmethod
def remove_load_case_container_setup(self, name: str) -> 'SimulationSetup':
""" Removes a load case container
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
>>> load_case_container_setup = simulation_setup.create_load_case_container_setup().set_name(name='load_case_container_name') # Creates a load case container so there is something to remove
..
>>> isinstance(load_case_container_setup, LoadCaseContainerSetup)
True
>>> simulation_setup.remove_load_case_container_setup(name='load_case_container_name')
<more...>
..
>>> # Test assertions for doctest, will not be rendered in the documentation because of the '..' sphinx comment above
>>> len(proj.simulation.load_case_containers_container.elements) # Take into account Empty LCC
1
>>> from more.design_parameters.registered_list_injector_singleton import LazyRegisteredListInjectorSingleton
>>> 'Empty' in LazyRegisteredListInjectorSingleton.get()['load_case_containers'].names_id_dict
True
>>> len(LazyRegisteredListInjectorSingleton.get()['load_case_containers'].names_id_dict)
1
>>> len(LazyRegisteredListInjectorSingleton.get()['load_case_containers'].id_values_dict)
1
>>> simulation_setup.remove_load_case_container_setup(name='non_existent_lcc')
Traceback (most recent call last):
...
more.api.exceptions.api_exception.NameNotFoundError: Item: non_existent_lcc not found, available names are: "dict_keys(['Empty'])"
Parameters
----------
name : str
The name of the load case container to remove
Returns
-------
self : SimulationSetup
Raises
------
NameNotFoundError
Raised if no load case container with the given name exists
"""
pass
[docs]
@abstractmethod
def remove_data_table_setup(self, name: str) -> 'SimulationSetup':
""" Removes a data table
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
>>> data_table_setup = simulation_setup.create_data_table_setup().set_name(name='data_table_name') # Creates a transient data table so that there is something to remove
..
>>> isinstance(data_table_setup, DataTableSetup)
True
>>> simulation_setup.remove_data_table_setup(name='data_table_name') # Assumes this data table exists
<more...>
..
>>> isinstance(simulation_setup, SimulationSetup)
True
>>> # Test assertions for doctest, will not be rendered in the documentation because of the '..' sphinx comment above
>>> len(proj.simulation.data_table_container.elements)
0
>>> from more.design_parameters.registered_list_injector_singleton import LazyRegisteredListInjectorSingleton
>>> len(LazyRegisteredListInjectorSingleton.get()['all_data_tables'].names_id_dict)
0
>>> len(LazyRegisteredListInjectorSingleton.get()['all_data_tables'].id_values_dict)
0
>>> simulation_setup.remove_data_table_setup(name='non_existent_data_table_name')
Traceback (most recent call last):
...
more.api.exceptions.api_exception.NameNotFoundError: ...
Parameters
----------
name : str
The name of the data table to remove
Returns
-------
self : SimulationSetup
Raises
------
NameNotFoundError
Raised if no data table with the given name exists
"""
pass
[docs]
@abstractmethod
def remove_study_setup(self, name: str) -> 'SimulationSetup':
""" Removes a study
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
>>> study_setup = simulation_setup.create_study_setup().set_name(name='study_name')
..
>>> isinstance(study_setup, StudiesSetup)
True
>>> simulation_setup.remove_study_setup(name='study_name')
<more...>
..
>>> # Test assertions for doctest, will not be rendered in the documentation because of the '..' sphinx comment above
>>> len(proj.simulation.study_container.elements)
0
>>> simulation_setup.remove_study_setup(name='non_existent_study_name')
Traceback (most recent call last):
...
more.api.exceptions.api_exception.NameNotFoundError: Item: non_existent_study_name not found, available names are: "dict_keys([])"
Parameters
----------
name : str
The name of the study to remove
Returns
-------
self : SimulationSetup
Raises
------
NameNotFoundError
Raised if no study with the given name exists
"""
pass
[docs]
@abstractmethod
def get_available_data_tables_names(self) -> typing.List[str]:
""" Gets the names of existing data tables
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
>>> simulation_setup \\
... .create_data_table_setup(data_table_type_name='Transient data table') \\
... .set_name('first_data_table')
<more.transient_data_table.scripting_api.transient_data_table_setup.TransientDataTableSetup object at 0x...>
>>> simulation_setup \\
... .create_data_table_setup(data_table_type_name='Transient data table') \\
... .set_name('second_data_table')
<more.transient_data_table.scripting_api.transient_data_table_setup.TransientDataTableSetup object at 0x...>
>>> simulation_setup.get_available_data_tables_names()
['first_data_table', 'second_data_table']
Returns
-------
list of strings
Names of existing data tables
"""
pass
[docs]
@abstractmethod
def get_available_studies_names(self) -> typing.List[str]:
""" Gets the names of existing studies
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
>>> studies_setup_1 = simulation_setup \\
... .create_study_setup() \\
... .set_name('first_study')
..
>>> isinstance(studies_setup_1, StudiesSetup)
True
>>> studies_setup_2 = simulation_setup \\
... .create_study_setup() \\
... .set_name('second_study')
..
>>> isinstance(studies_setup_2, StudiesSetup)
True
>>> simulation_setup.get_available_studies_names()
['first_study', 'second_study']
Returns
-------
list of strings
Names of existing studies
"""
pass
[docs]
@abstractmethod
def get_available_load_case_containers_names(self) -> typing.List[str]:
""" Gets the names of existing load case containers
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
>>> lcc_setup_1 = simulation_setup.create_load_case_container_setup().set_name(name='first_lcc')
..
>>> isinstance(lcc_setup_1, LoadCaseContainerSetup)
True
>>> lcc_setup_2 = simulation_setup.create_load_case_container_setup().set_name(name='second_lcc')
..
>>> isinstance(lcc_setup_2, LoadCaseContainerSetup)
True
>>> simulation_setup.get_available_load_case_containers_names()
['Empty', 'first_lcc', 'second_lcc']
Returns
-------
list of strings
Names of existing load case containers
"""
pass
[docs]
@abstractmethod
def get_available_data_table_types_names(self) -> typing.List[str]:
""" Gets the names of the installed data table types
.. admonition:: Example
:class: note
..
>>> import more.project
>>> proj = more.project.Project()
>>> from more.api import ApiGateway
>>> api = ApiGateway(proj=proj)
>>> simulation_setup = api.create_simulation_setup()
>>> simulation_setup.get_available_data_table_types_names() # Will return all names of available data table types
... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
[...'Transient data table'...]
Returns
-------
list of strings
Names of installed data table type names
"""
pass
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass