Source code for loads.rail_profile.api.rail_profile_api

import typing

from traits.trait_errors import TraitError

from more.api.exceptions.api_exception import NameNotFoundError
from more.api.simulation.load_cases.load_setup import LoadSetup, TranslationSettableMixin, RotationSettableMixin
from more.api.utils.interface_registries import register_api_implementation
from more.loads.rail_profile import RailProfile
from more import log

log.init_logging()
logger = log.getLogger(__name__)


def _rail_profile_api_setup(api):
    simulation_setup = api.create_simulation_setup()
    lcc_setup = simulation_setup.create_load_case_container_setup().set_name(name='Example LCC')
    lc_setup = lcc_setup.create_load_case_setup(lc_type='Mechanical load case').set_name(name='Example LC')
    return lc_setup.create_load_setup(load_type='Rail profile')


[docs] @register_api_implementation(core_class=RailProfile) class RailProfileSetup(LoadSetup): """ The API handler for rail profiles The following example shows how to create a rail profile using the API, it is assumed that these steps were performed in each of the examples for other methods in this class. .. 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 = simulation_setup.create_load_case_container_setup().set_name(name='Example LCC') >>> lc_setup = lcc_setup.create_load_case_setup(lc_type='Mechanical load case').set_name(name='Example LC') >>> rail_profile_setup = lc_setup.create_load_setup(load_type='Rail profile') .. >>> isinstance(rail_profile_setup, RailProfileSetup) True >>> isinstance(rail_profile_setup._load, RailProfile) True The next example shows how to get an API object for an already existing load. .. 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 = simulation_setup.create_load_case_container_setup().set_name(name='Example LCC') >>> lc_setup = lcc_setup.create_load_case_setup(lc_type='Mechanical load case').set_name(name='Example LC') .. >>> rail_profile_setup = lc_setup.create_load_setup(load_type='Rail profile') >>> rail_profile_setup = lc_setup.get_load_setup(index=0) # The load under this index must already exist .. >>> isinstance(rail_profile_setup, RailProfileSetup) True >>> isinstance(rail_profile_setup._load, RailProfile) True """
[docs] def set_component_by_name(self, component_name: str) -> 'RailProfileSetup': """ Sets the component for the associated rail profile See example at the top for how to create a rail profile setup object .. admonition:: Example :class: note .. >>> import more.project >>> proj = more.project.Project() >>> from more.api import ApiGateway >>> api = ApiGateway(proj=proj) >>> rail_profile_setup = _rail_profile_api_setup(api) >>> component = proj.comp.add_component() >>> component.name = 'example component' >>> rail_profile_setup = rail_profile_setup.set_component_by_name(component_name='example component') # The component with this name must already exist .. >>> rail_profile_setup._load.component_selector.selected_entity == component True >>> rail_profile_setup = rail_profile_setup.set_component_by_name(component_name='nonexistent name') Traceback (most recent call last): ... more.api.exceptions.api_exception.NameNotFoundError: ... Returns ------- self: RailProfile Raises ------ NameNotFoundError Raised if the supplied name is not recognized """ self._load.component_selector.select_entity_by_name(name=component_name) return self
@property def _available_rail_interfaces_dict(self) -> typing.Dict[str, typing.Any]: return {cv.name: cv for cv in self._load.available_rail_interfaces}
[docs] def set_rail_interface_by_name(self, rail_interface_name) -> 'RailProfileSetup': """ Sets the rail interface for the associated rail profile Returns ------- self: RailProfile Raises ------ NameNotFoundError Raised if the supplied name is not recognized """ try: self._load.selected_rail_interface = self._available_rail_interfaces_dict[rail_interface_name] except KeyError: raise NameNotFoundError( 'Rail interface with name: {} not found, available names are: {}'.format(rail_interface_name, self._available_rail_interfaces_dict.keys())) return self
def set_profile_car_length_averaging_option(self, value: bool) -> 'RailProfileSetup': self._load.profile_car_length_averaging_option = value return self def set_profile_normal_dir(self, value) -> 'RailProfileSetup': self._load.profile_normal_dir = value return self @property def available_links_dict(self): return self._available_links_dict @property def _available_links_dict(self) -> typing.Dict[str, typing.Any]: return {cv.name: cv for cv in self._load.available_links} def set_links_by_names(self, link_names) -> 'RailProfileSetup': try: iter(link_names) except TypeError: raise TypeError('Link names must be an iterable of names of links to choose') selected_links = [] for link_name in link_names: if link_name in self._available_links_dict.keys(): selected_links.append(self._available_links_dict[link_name]) else: raise NameNotFoundError('Link with name: {} is not in the list of available links: {}'.format(link_name, self._available_links_dict.keys())) self._load.selected_links = selected_links return self def set_data(self, var, data) -> 'RailProfileSetup': self._load.profile_data.set_table_data(var, data) return self @property def available_interpolation_methods(self) -> typing.List[str]: return list(self._load.profile_data.trait('interpolation').trait_type.values) def set_interpolation_method(self, method_name: str) -> 'RailProfileSetup': try: self._load.profile_data.interpolation = method_name except TraitError: raise NameNotFoundError( 'Interpolation method: {} not found, available interpolation methods are: {}'.format(method_name, self.available_interpolation_methods)) return self def import_data(self, path) -> 'RailProfileSetup': self._load.profile_data.import_data(path) return self
[docs] def set_component(self, component) -> 'RailProfileSetup': """ Deprecated method .. admonition:: Deprecated :class: warning `set_component` will be removed in a future MORe release, it is replaced by :meth:`~.set_component_by_name` """ logger.warning( 'Deprecation: \"set_component\" will be removed in a future MORe release, it is replaced by \"set_component_by_name\"') self.set_component_by_name(component_name=component.name) return self
[docs] def set_rail_interface(self, rail_interface) -> 'RailProfileSetup': """ Deprecated method .. admonition:: Deprecated :class: warning `set_component` will be removed in a future MORe release, it is replaced by :meth:`~.set_component_by_name` """ logger.warning( 'Deprecation: \"set_component\" will be removed in a future MORe release, it is replaced by \"set_component_by_name\"') self.set_rail_interface_by_name(rail_interface_name=rail_interface.name) return self
@property def available_rail_interfaces_dict(self): """ Deprecated method .. admonition:: Deprecated :class: warning `available_rail_interfaces_dict` will be removed in a future MORe release, it is replaced by :meth:`~.available_rail_interfaces_names` """ logger.warning( 'Deprecation: \"available_rail_interfaces_dict\" will be removed in a future MORe release, it is replaced by \"available_rail_interfaces_names\"') return self._available_rail_interfaces_dict
[docs] def set_profile_data(self, profile_data): """ Deprecated method .. admonition:: Deprecated :class: warning `set_profile_data` will be removed in a future MORe release, it is replaced by :meth:`~.set_data` """ logger.warning( 'Deprecation: \"set_profile_data\" will be removed in a future MORe release, it is replaced by \"set_data\"') return self.set_data(var=profile_data.var, data=profile_data.data)