Skip to content

Commit

Permalink
Feat/project path (#66)
Browse files Browse the repository at this point in the history
Co-authored-by: Ronny Fahlberg <[email protected]>
  • Loading branch information
PanekOndrej and rfahlberg authored Nov 2, 2022
1 parent 89e79db commit bd295cd
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 256 deletions.
30 changes: 22 additions & 8 deletions src/ansys/optislang/core/examples/downloads.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
"""Download files from repository."""

import os
from pathlib import Path
from typing import Tuple

import ansys.optislang.core.examples as examples

# TODO: implement automatic download from online repository
# EXAMPLE_REPO = "https://github.com/pyansys/pyoptislang/tree/main/examples/files"


def _download_file(scriptname: str) -> str:
"""Check if file exists, otherwise download it. Return path to file then."""
def _download_files(scriptname: str) -> Tuple[Path, ...]:
"""Check if files exists, otherwise download them. Return path to files then."""
# check if file was downloaded
for file_path in examples.example_files[scriptname]:
if not os.path.isfile(file_path):
if not file_path.is_file():
NotImplementedError("Automatic download not implemented.")

return examples.example_files[scriptname]


def _download_script(scriptname: str) -> str:
def _download_script(scriptname: str) -> Path:
"""Check if file exists, otherwise download it. Return path to file then."""
# check if script was downloaded, download it if not
local_path = examples.example_scripts[scriptname]

if not os.path.isfile(local_path):
if not local_path.is_file():
NotImplementedError("Automatic download not implemented.")

return local_path


def get_files(scriptname: str) -> str:
"""Insert name of the example script (without ``.py``) and get local_path."""
def get_files(scriptname: str) -> Tuple[Path, Tuple[Path, ...]]:
"""Get tuple of files necessary for running example.
Parameters
----------
scriptname: str
Name of the example script (without ``.py``).
Returns
-------
Tuple[Path, Tuple[Path, ...]]
Tuple[0]: path to script
Tuple[1]: tuple of paths to files necessary for running script
"""
if examples.example_files[scriptname] is not None:
file_path = _download_file(scriptname)
file_path = _download_files(scriptname)
else:
file_path = None

Expand Down
189 changes: 92 additions & 97 deletions src/ansys/optislang/core/examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,138 +2,133 @@

import inspect
import os
from pathlib import Path

module_path = os.path.dirname(inspect.getfile(inspect.currentframe()))
module_path = Path(inspect.getfile(inspect.currentframe())).parent

# dictionary of files, that must be available to run scripts
example_files = {
"ansys_workbench_portscan": None,
"arsm_ten_bar_truss": (
os.path.join(module_path, "00_run_script", "ten_bar_truss", "files", "ten_bar_truss.s"),
os.path.join(module_path, "00_run_script", "ten_bar_truss", "files", "ten_bar_truss.out"),
module_path / "00_run_script" / "ten_bar_truss" / "files" / "ten_bar_truss.s",
module_path / "00_run_script" / "ten_bar_truss" / "files" / "ten_bar_truss.out",
),
"ansys_workbench_ten_bar_truss": (
os.path.join(
module_path, "00_run_script", "ten_bar_truss", "files", "ten_bar_truss_apdl.wbpz"
)
module_path / "00_run_script" / "ten_bar_truss" / "files" / "ten_bar_truss_apdl.wbpz",
),
"ten_bar_modify_parameters": None,
"ten_bar_truss_lc2": (
os.path.join(module_path, "00_run_script", "ten_bar_truss", "files", "ten_bar_truss2.s"),
os.path.join(module_path, "00_run_script", "ten_bar_truss", "files", "ten_bar_truss2.out"),
module_path / "00_run_script" / "ten_bar_truss" / "files" / "ten_bar_truss2.s",
module_path / "00_run_script" / "ten_bar_truss" / "files" / "ten_bar_truss2.out",
),
"oscillatorcalibration_system_ascii": (
os.path.join(
module_path, "00_run_script", "oscillator", "scripts", "_create_oscillator.py"
),
os.path.join(module_path, "00_run_script", "oscillator", "files", "oscillator.s"),
os.path.join(module_path, "00_run_script", "oscillator", "files", "oscillator.bat"),
os.path.join(module_path, "00_run_script", "oscillator", "files", "oscillator.sh"),
os.path.join(module_path, "00_run_script", "oscillator", "files", "oscillator_signal.txt"),
os.path.join(
module_path, "00_run_script", "oscillator", "files", "oscillator_reference.txt"
),
module_path / "00_run_script" / "oscillator" / "scripts" / "_create_oscillator.py",
module_path / "00_run_script" / "oscillator" / "files" / "oscillator.s",
module_path / "00_run_script" / "oscillator" / "files" / "oscillator.bat",
module_path / "00_run_script" / "oscillator" / "files" / "oscillator.sh",
module_path / "00_run_script" / "oscillator" / "files" / "oscillator_signal.txt",
module_path / "00_run_script" / "oscillator" / "files" / "oscillator_reference.txt",
),
"oscillatorcalibration_system_python": (
os.path.join(
module_path, "00_run_script", "oscillator", "scripts", "_create_oscillator.py"
),
os.path.join(
module_path, "00_run_script", "oscillator", "files", "oscillator_reference.txt"
),
module_path / "00_run_script" / "oscillator" / "scripts" / "_create_oscillator.py",
module_path / "00_run_script" / "oscillator" / "files" / "oscillator_reference.txt",
),
"oscillator_optimization_ea": (
os.path.join(module_path, "00_run_script", "oscillator", "scripts", "_create_oscillator.py")
module_path / "00_run_script" / "oscillator" / "scripts" / "_create_oscillator.py",
),
"oscillator_optimization_on_mop": (
os.path.join(module_path, "00_run_script", "oscillator", "scripts", "_create_oscillator.py")
module_path / "00_run_script" / "oscillator" / "scripts" / "_create_oscillator.py",
),
"oscillator_robustness_arsm": (
os.path.join(module_path, "00_run_script", "oscillator", "scripts", "_create_oscillator.py")
module_path / "00_run_script" / "oscillator" / "scripts" / "_create_oscillator.py",
),
"oscillator_sensitivity_mop": (
os.path.join(module_path, "00_run_script", "oscillator", "scripts", "_create_oscillator.py")
module_path / "00_run_script" / "oscillator" / "scripts" / "_create_oscillator.py",
),
"oscillator_system_python": (
os.path.join(module_path, "00_run_script", "oscillator", "scripts", "_create_oscillator.py")
module_path / "00_run_script" / "oscillator" / "scripts" / "_create_oscillator.py",
),
"create_all_possible_nodes": None,
"etk_abaqus": (
os.path.join(module_path, "00_run_script", "etk_abaqus", "files", "oscillator.inp"),
os.path.join(module_path, "00_run_script", "etk_abaqus", "files", "oscillator.odb"),
module_path / "00_run_script" / "etk_abaqus" / "files" / "oscillator.inp",
module_path / "00_run_script" / "etk_abaqus" / "files" / "oscillator.odb",
),
"python_help": None,
"python_node": None,
"optimizer_settings": None,
"sensitivity_settings": None,
"simple_calculator": os.path.join(module_path, "00_run_script", "files", "calculator.opf"),
"simple_calculator": (module_path / "00_run_script" / "files" / "calculator.opf",),
}

# dictionary of scripts to be run
example_scripts = {
"ansys_workbench_portscan": os.path.join(
module_path, "00_run_script", "scripts", "ansys_workbench_portscan.py"
),
"arsm_ten_bar_truss": os.path.join(
module_path, "00_run_script", "ten_bar_truss", "scripts", "arsm_ten_bar_truss.py"
),
"ansys_workbench_ten_bar_truss": os.path.join(
module_path, "00_run_script", "ten_bar_truss", "scripts", "ansys_workbench_ten_bar_truss.py"
),
"ten_bar_modify_parameters": os.path.join(
module_path, "00_run_script", "ten_bar_truss", "scripts", "ten_bar_modify_parameters.py"
),
"ten_bar_truss_lc2": os.path.join(
module_path, "00_run_script", "ten_bar_truss", "scripts", "ten_bar_truss_lc2.py"
),
"oscillatorcalibration_system_ascii": os.path.join(
module_path,
"00_run_script",
"oscillator",
"scripts",
"oscillatorcalibration_system_ascii.py",
),
"oscillatorcalibration_system_python": os.path.join(
module_path,
"00_run_script",
"oscillator",
"scripts",
"oscillatorcalibration_system_python.py",
),
"oscillator_optimization_ea": os.path.join(
module_path, "00_run_script", "oscillator", "scripts", "oscillator_optimization_ea.py"
),
"oscillator_optimization_on_mop": os.path.join(
module_path, "00_run_script", "oscillator", "scripts", "oscillator_optimization_on_mop.py"
),
"oscillator_robustness_arsm": os.path.join(
module_path, "00_run_script", "oscillator", "scripts", "oscillator_robustness_arsm.py"
),
"oscillator_sensitivity_mop": os.path.join(
module_path, "00_run_script", "oscillator", "scripts", "oscillator_sensitivity_mop.py"
),
"oscillator_system_python": os.path.join(
module_path, "00_run_script", "oscillator", "scripts", "oscillator_system_python.py"
),
"create_all_possible_nodes": os.path.join(
module_path, "00_run_script", "scripts", "create_all_possible_nodes.py"
),
"etk_abaqus": os.path.join(
module_path, "00_run_script", "etk_abaqus", "scripts", "etk_abaqus.py"
),
"python_help": os.path.join(
module_path, "00_run_script", "python", "scripts", "python_help.py"
),
"python_node": os.path.join(
module_path, "00_run_script", "python", "scripts", "python_node.py"
),
"optimizer_settings": os.path.join(
module_path, "00_run_script", "scripts", "optimizer_settings.py"
),
"sensitivity_settings": os.path.join(
module_path, "00_run_script", "scripts", "sensitivity_settings.py"
),
"simple_calculator": os.path.join(
module_path, "00_run_script", "scripts", "simple_calculator.py"
),
"ansys_workbench_portscan": module_path
/ "00_run_script"
/ "scripts"
/ "ansys_workbench_portscan.py",
"arsm_ten_bar_truss": module_path
/ "00_run_script"
/ "ten_bar_truss"
/ "scripts"
/ "arsm_ten_bar_truss.py",
"ansys_workbench_ten_bar_truss": module_path
/ "00_run_script"
/ "ten_bar_truss"
/ "scripts"
/ "ansys_workbench_ten_bar_truss.py",
"ten_bar_modify_parameters": module_path
/ "00_run_script"
/ "ten_bar_truss"
/ "scripts"
/ "ten_bar_modify_parameters.py",
"ten_bar_truss_lc2": module_path
/ "00_run_script"
/ "ten_bar_truss"
/ "scripts"
/ "ten_bar_truss_lc2.py",
"oscillatorcalibration_system_ascii": module_path
/ "00_run_script"
/ "oscillator"
/ "scripts"
/ "oscillatorcalibration_system_ascii.py",
"oscillatorcalibration_system_python": module_path
/ "00_run_script"
/ "oscillator"
/ "scripts"
/ "oscillatorcalibration_system_python.py",
"oscillator_optimization_ea": module_path
/ "00_run_script"
/ "oscillator"
/ "scripts"
/ "oscillator_optimization_ea.py",
"oscillator_optimization_on_mop": module_path
/ "00_run_script"
/ "oscillator"
/ "scripts"
/ "oscillator_optimization_on_mop.py",
"oscillator_robustness_arsm": module_path
/ "00_run_script"
/ "oscillator"
/ "scripts"
/ "oscillator_robustness_arsm.py",
"oscillator_sensitivity_mop": module_path
/ "00_run_script"
/ "oscillator"
/ "scripts"
/ "oscillator_sensitivity_mop.py",
"oscillator_system_python": module_path
/ "00_run_script"
/ "oscillator"
/ "scripts"
/ "oscillator_system_python.py",
"create_all_possible_nodes": module_path
/ "00_run_script"
/ "scripts"
/ "create_all_possible_nodes.py",
"etk_abaqus": module_path / "00_run_script" / "etk_abaqus" / "scripts" / "etk_abaqus.py",
"python_help": module_path / "00_run_script" / "python" / "scripts" / "python_help.py",
"python_node": module_path / "00_run_script" / "python" / "scripts" / "python_node.py",
"optimizer_settings": module_path / "00_run_script" / "scripts" / "optimizer_settings.py",
"sensitivity_settings": module_path / "00_run_script" / "scripts" / "sensitivity_settings.py",
"simple_calculator": module_path / "00_run_script" / "scripts" / "simple_calculator.py",
}
29 changes: 15 additions & 14 deletions src/ansys/optislang/core/optislang.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Contains Optislang class which provides python API for optiSLang application."""
from pathlib import Path
from typing import Sequence, Tuple, Union

from importlib_metadata import version
Expand All @@ -25,10 +26,10 @@ class Optislang:
Defaults to ``None``.
port : int, optional
A numeric port number of running optiSLang server. Defaults to ``None``.
executable : str, optional
executable : Union[str, Path], optional
Path to the optiSLang executable file which supposed to be executed on localhost.
It is ignored when the host and port parameters are specified. Defaults to ``None``.
project_path : str, optional
project_path : Union[str, Path], optional
Path to the optiSLang project file which is supposed to be used by new local optiSLang
server. It is ignored when the host and port parameters are specified.
- If the project file exists, it is opened.
Expand Down Expand Up @@ -77,8 +78,8 @@ def __init__(
self,
host: str = None,
port: int = None,
executable: str = None,
project_path: str = None,
executable: Union[str, Path] = None,
project_path: Union[str, Path] = None,
no_save: bool = False,
ini_timeout: Union[int, float] = 20,
name: str = None,
Expand All @@ -89,8 +90,8 @@ def __init__(
"""Initialize a new instance of the ``Optislang`` class."""
self.__host = host
self.__port = port
self.__executable = executable
self.__project_path = project_path
self.__executable = Path(executable) if executable is not None else None
self.__project_path = Path(project_path) if project_path is not None else None
self.__no_save = no_save
self.__ini_timeout = ini_timeout
self.__name = name
Expand Down Expand Up @@ -216,12 +217,12 @@ def get_project_description(self) -> str:
"""
return self.__osl_server.get_project_description()

def get_project_location(self) -> str:
def get_project_location(self) -> Path:
"""Get path to the optiSLang project file.
Returns
-------
str
Path
Path to the optiSLang project file. If no project is loaded in the optiSLang,
returns ``None``.
Expand Down Expand Up @@ -299,12 +300,12 @@ def get_timeout(self) -> Union[float, None]:
"""
return self.__osl_server.get_timeout()

def get_working_dir(self) -> str:
def get_working_dir(self) -> Path:
"""Get path to the optiSLang project working directory.
Returns
-------
str
Path
Path to the optiSLang project working directory. If no project is loaded
in the optiSLang, returns ``None``.
Expand Down Expand Up @@ -365,14 +366,14 @@ def run_python_script(

def run_python_file(
self,
file_path: str,
file_path: Union[str, Path],
args: Union[Sequence[object], None] = None,
) -> Tuple[str, str]:
"""Read python script from the file, load it in a project context and execute it.
Parameters
----------
file_path : str
file_path : Union[str, Path]
Path to the Python script file which content is supposed to be executed on the server.
args : Sequence[object], None, optional
Sequence of arguments used in Python script. Defaults to ``None``.
Expand All @@ -395,12 +396,12 @@ def run_python_file(
"""
return self.__osl_server.run_python_file(file_path, args)

def save_copy(self, file_path: str) -> None:
def save_copy(self, file_path: Union[str, Path]) -> None:
"""Save the current project as a copy to a location.
Parameters
----------
file_path : str
file_path : Union[str, Path]
Path where to save the project copy.
Raises
Expand Down
Loading

0 comments on commit bd295cd

Please sign in to comment.