Skip to content

Commit

Permalink
Merge pull request #44 from isteinbrecher/coreform
Browse files Browse the repository at this point in the history
Allow CubitPy to run with Cubit Coreform
  • Loading branch information
isteinbrecher authored Nov 21, 2024
2 parents e72cc9d + 0c06414 commit da9705e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 22 deletions.
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,26 @@ A few things to keep in mind:

CubitPy is developed with `python3.12`.
Other versions of Python might lead to issues.
It is recommended to use virtual environments with `python`.
On Debian systems the following packages have to be installed
```bash
sudo apt-get install python3-venv python3-dev
```

Now a virtual environment can be created (chose an appropriate directory for this, e.g., `/home/user/opt`)
```bash
python -m venv cubitpy-env
```

The created virtual environment can be loaded with
```bash
source cubitpy-env/bin/activate
```
It is recommended to use a python environment container such as `conda` or `venv`.
- `conda`:
A [conda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html) environment can be created and loaded with
```bash
conda create -n cubitpy python=3.12
conda activate cubitpy
```
- `venv`: Chose an appropriate directory for this, e.g., `/home/user/opt`.
A virtual environment can be setup with
- On Debian systems the following packages have to be installed:
```bash
sudo apt-get install python3-venv python3-dev
```
- Create and load the environment
```bash
cd <path-to-env-folder>
python -m venv cubitpy-env
source cubitpy-env/bin/activate
```

From now on we assume that the virtual environment is loaded.
To install `cubitpy` go to the repository root directory
```bash
cd path_to_cubitpy
Expand Down
44 changes: 41 additions & 3 deletions cubitpy/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import os
import getpass
from sys import platform
import glob

# Cubitpy imports.
from .cubitpy_types import (
Expand Down Expand Up @@ -101,9 +102,16 @@ def get_cubit_root_path(**kwargs):
def get_cubit_exe_path(cls, **kwargs):
cubit_root = cls.get_cubit_root_path(**kwargs)
if platform == "linux" or platform == "linux2":
return os.path.join(cubit_root, "cubit")
if cupy.is_coreform():
return os.path.join(cubit_root, "bin", "coreform_cubit")
else:
return os.path.join(cubit_root, "cubit")
elif platform == "darwin":
return os.path.join(cubit_root, "Contents/MacOS/Cubit")
if cupy.is_coreform():
cubit_exe_name = cubit_root.split("/")[-1].split(".app")[0]
return os.path.join(cubit_root, "Contents/MacOS", cubit_exe_name)
else:
return os.path.join(cubit_root, "Contents/MacOS/Cubit")
else:
raise ValueError("Got unexpected platform")

Expand All @@ -113,10 +121,40 @@ def get_cubit_lib_path(cls, **kwargs):
if platform == "linux" or platform == "linux2":
return os.path.join(cubit_root, "bin")
elif platform == "darwin":
return os.path.join(cubit_root, "Contents/MacOS")
if cls.is_coreform():
return os.path.join(cubit_root, "Contents/lib")
else:
return os.path.join(cubit_root, "Contents/MacOS")
else:
raise ValueError("Got unexpected platform")

@classmethod
def get_cubit_interpreter(cls):
"""Get the path to the python interpreter to be used for CubitPy"""
cubit_root = cls.get_cubit_root_path()
if cls.is_coreform():
pattern = "**/python3"
full_pattern = os.path.join(cubit_root, pattern)
python3_matches = glob.glob(full_pattern, recursive=True)
python3_files = [path for path in python3_matches if os.path.isfile(path)]
if not len(python3_files) == 1:
raise ValueError(
"Could not find the path to the cubit python interpreter"
)
cubit_python_interpreter = python3_files[0]
return cubit_python_interpreter
else:
return "python2.7"

@classmethod
def is_coreform(cls):
"""Return if the given path is a path to cubit coreform"""
cubit_root = cls.get_cubit_root_path()
if "15.2" in cubit_root:
return False
else:
return True


# Global object with options for cubitpy.
cupy = CubitOptions()
5 changes: 4 additions & 1 deletion cubitpy/cubit_wrapper/cubit_wrapper_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
*,
cubit_args=None,
cubit_lib=None,
interpreter="popen//python=python2.7",
interpreter=None,
):
"""Initialize the connection between the client (cubit) python interpreter and this one.
Also load the cubit module in the remote interpreter.
Expand All @@ -75,6 +75,9 @@ def __init__(
Python interpreter to be used for running cubit.
"""

if interpreter is None:
interpreter = f"popen//python={cupy.get_cubit_interpreter()}"

if cubit_lib is None:
cubit_lib = cupy.get_cubit_lib_path()

Expand Down
10 changes: 8 additions & 2 deletions cubitpy/cubitpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ def set_line_interval(self, item, n_el):

def export_cub(self, path):
"""Export the cubit input."""
self.cubit.cmd('save as "{}" overwrite'.format(path))
if cupy.is_coreform():
self.cubit.cmd(f'save cub5 "{path}" overwrite journal')
else:
self.cubit.cmd('save as "{}" overwrite'.format(path))

def export_exo(self, path):
"""Export the mesh."""
Expand Down Expand Up @@ -411,7 +414,10 @@ def display_in_cubit(self, labels=[], delay=0.5, testing=False):
# TODO: find a way to do this without the wait command, but to check if
# the file is readable.
os.makedirs(cupy.temp_dir, exist_ok=True)
state_path = os.path.join(cupy.temp_dir, "state.cub")
if cupy.is_coreform():
state_path = os.path.join(cupy.temp_dir, "state.cub5")
else:
state_path = os.path.join(cupy.temp_dir, "state.cub")
self.export_cub(state_path)
time.sleep(delay)

Expand Down

0 comments on commit da9705e

Please sign in to comment.