Skip to content

Commit

Permalink
Adding APDL jobs support (#3111)
Browse files Browse the repository at this point in the history
* Adding inputs and outputs

* Supporting pure APDL jobs

* Changing arguments order

* Allowing force mode

* Apply suggestions from code review

Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com>

---------

Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com>
germa89 and PipKat authored May 22, 2024
1 parent 4055c60 commit f23da00
Showing 2 changed files with 75 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/ansys/mapdl/core/cli/hpc.py
Original file line number Diff line number Diff line change
@@ -183,6 +183,17 @@
flag_value=True,
help="""Whether the terminal is to wait for job completion before returning control to the user. """,
)
@click.option(
"--mode",
default=None,
type=str,
help="""
Force the job submission to behave as if the main file was a Python,
shell, or APDL file, regardless of its extension type. Allowed values are
``"python"``, ``"shell"``, and ``"apdl"``.
By default, PyMAPDL detects the type of file from its extension.
""",
)
@click.option(
"--debug",
default=False,
@@ -213,6 +224,7 @@ def submit(
max_execution_time: int = None,
wait: bool = False,
debug: bool = False,
mode: Optional[Union["python", "shell", "apdl"]] = None,
):
import json

Check warning on line 229 in src/ansys/mapdl/core/cli/hpc.py

Codecov / codecov/patch

src/ansys/mapdl/core/cli/hpc.py#L229

Added line #L229 was not covered by tests

@@ -268,6 +280,7 @@ def submit(
disk_space=disk_space,
exclusive=exclusive,
max_execution_time=max_execution_time,
mode=mode,
)

if save_config_file:
72 changes: 62 additions & 10 deletions src/ansys/mapdl/core/hpc/pyhps.py
Original file line number Diff line number Diff line change
@@ -215,6 +215,7 @@ def is_float(num):

def create_task(

Check warning on line 216 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L216

Added line #L216 was not covered by tests
project_api,
mode,
main_file,
file_input_ids,
file_output_ids,
@@ -225,13 +226,19 @@ def create_task(
max_execution_time,
):

software = Software(name="Bash", version="0.1") # Overwriting
if mode == "apdl":
software = Software(name="Ansys Mechanical APDL", version="2024 R2")
name = "MAPDL Task"

Check warning on line 231 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L229-L231

Added lines #L229 - L231 were not covered by tests
else:
software = Software(name="Bash", version="0.1") # Overwriting
name = "PyMAPDL Task"

Check warning on line 234 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L233-L234

Added lines #L233 - L234 were not covered by tests

execution_command = f"%executable% %file:{os.path.basename(main_file)}%"
logger.debug(f"Using executable: '{execution_command}'")

Check warning on line 237 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L236-L237

Added lines #L236 - L237 were not covered by tests

# Process step
task_def = TaskDefinition(

Check warning on line 240 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L240

Added line #L240 was not covered by tests
name="PyMAPDL_task",
name=name,
software_requirements=[software],
execution_command=execution_command,
resource_requirements=ResourceRequirements(
@@ -338,15 +345,40 @@ def create_pymapdl_pyhps_job(
disk_space: int = None,
exclusive: bool = None,
max_execution_time: int = None,
mode: Optional[Union["python", "shell", "apdl"]] = None,
):
"""
Workflow
+ APDL mode: main_file
+ Others: shell_file
+ Wrapper
+ main_file
"""
if python not in [2, 2.7, 3, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12]:
warn(f"Version of Python {python} might not be supported by the cluster.")

Check warning on line 359 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L358-L359

Added lines #L358 - L359 were not covered by tests

if not os.path.exists(main_file):
raise ValueError(f"The Python file {main_file} must exist.")
raise ValueError(f"The file {main_file} must exist.")

Check warning on line 362 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L361-L362

Added lines #L361 - L362 were not covered by tests

logger.debug(f"Main file is in: {main_file}.")

Check warning on line 364 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L364

Added line #L364 was not covered by tests

_, file_extension = os.path.splitext(main_file)

Check warning on line 366 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L366

Added line #L366 was not covered by tests

if mode is None:
if file_extension.lower() in [".sh"]:
mode = "shell"
elif file_extension.lower() in [".py"]:
mode = "python"
elif file_extension.lower() in [".inp", ".mac"]:
mode = "apdl"

Check warning on line 374 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L368-L374

Added lines #L368 - L374 were not covered by tests
else:
if mode.lower() not in ["python", "shell", "apdl"]:
raise Exception("File type is not supported.")

Check warning on line 377 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L376-L377

Added lines #L376 - L377 were not covered by tests

logger.debug(f"Main Python file is in: {main_file}.")
logger.debug(

Check warning on line 379 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L379

Added line #L379 was not covered by tests
f"Submission mode set to '{mode}' because of main file ({main_file}) extension."
)

if inputs is None:
inputs = []
@@ -355,19 +387,24 @@ def create_pymapdl_pyhps_job(

input_file = None
if inputs:
if mode == "apdl":
raise ValueError("Inputs are not supported when using APDL files.")

Check warning on line 391 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L388-L391

Added lines #L388 - L391 were not covered by tests

if isinstance(inputs, str):
inputs = inputs.split(",")

Check warning on line 394 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L393-L394

Added lines #L393 - L394 were not covered by tests

if inputs:
input_file = "input.inp"
input_file = "input.inputs"

Check warning on line 397 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L396-L397

Added lines #L396 - L397 were not covered by tests

content = "\n".join(inputs)
input_file = _create_tmp_file(input_file, content)
logger.debug(f"Input file in: {input_file}")

Check warning on line 401 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L399-L401

Added lines #L399 - L401 were not covered by tests

output_parms_file = None
if outputs:
output_parms_file = "output.out"
if mode == "apdl":
raise ValueError("Outputs are not supported when using APDL files.")
output_parms_file = "output.output"
if isinstance(outputs, str):
outputs = outputs.split(",")

Check warning on line 409 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L403-L409

Added lines #L403 - L409 were not covered by tests

@@ -391,6 +428,7 @@ def create_pymapdl_pyhps_job(

if outputs:
content += f"""

Check warning on line 430 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L429-L430

Added lines #L429 - L430 were not covered by tests
# Write output data
with open("{output_parms_file}", "w") as fid:
"""
b0 = "{"
@@ -402,7 +440,7 @@ def create_pymapdl_pyhps_job(
wrapper_file = _create_tmp_file(wrapper_file, content)
logger.debug(f"Wrapper file in: {wrapper_file}")

Check warning on line 441 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L440-L441

Added lines #L440 - L441 were not covered by tests

if not requirements_file:
if not requirements_file and mode == "python":
import pkg_resources

Check warning on line 444 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L443-L444

Added lines #L443 - L444 were not covered by tests

content = "\n".join(

Check warning on line 446 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L446

Added line #L446 was not covered by tests
@@ -411,7 +449,7 @@ def create_pymapdl_pyhps_job(
requirements_file = _create_tmp_file("requirements.txt", content)
logger.debug(f"Requirements file in: {requirements_file}")

Check warning on line 450 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L449-L450

Added lines #L449 - L450 were not covered by tests

if not shell_file:
if not shell_file and mode == "python":
content = f"""

Check warning on line 453 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L452-L453

Added lines #L452 - L453 were not covered by tests
echo "Starting"
@@ -429,6 +467,14 @@ def create_pymapdl_pyhps_job(
shell_file = _create_tmp_file("main.sh", content)
logger.debug(f"Shell file in: {shell_file}")

Check warning on line 468 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L467-L468

Added lines #L467 - L468 were not covered by tests

elif shell_file and mode == "shell":
raise ValueError(

Check warning on line 471 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L470-L471

Added lines #L470 - L471 were not covered by tests
"You cannot use a shell file and specify a shell file as a main file. Avoid specifying the '--shell_file' argument."
)

elif not shell_file and mode == "shell":
shell_file = main_file

Check warning on line 476 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L475-L476

Added lines #L475 - L476 were not covered by tests

if isinstance(extra_files, str):
extra_files = extra_files.split(",")
elif extra_files is None:
@@ -443,8 +489,13 @@ def create_pymapdl_pyhps_job(
raise ValueError("One or more extra files do not exist.")

Check warning on line 489 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L488-L489

Added lines #L488 - L489 were not covered by tests

input_files = extra_files
input_files.append(requirements_file)
input_files.append(shell_file)
if mode == "python":
input_files.append(requirements_file)
input_files.append(shell_file)

Check warning on line 494 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L491-L494

Added lines #L491 - L494 were not covered by tests
else:
# we are going to refer to this from now on
shell_file = main_file

Check warning on line 497 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L497

Added line #L497 was not covered by tests

input_files.append(main_file)

Check warning on line 499 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L499

Added line #L499 was not covered by tests

if inputs:
@@ -488,6 +539,7 @@ def create_pymapdl_pyhps_job(
# Set tasks
task_def = create_task(

Check warning on line 540 in src/ansys/mapdl/core/hpc/pyhps.py

Codecov / codecov/patch

src/ansys/mapdl/core/hpc/pyhps.py#L540

Added line #L540 was not covered by tests
project_api,
mode,
shell_file,
file_input_ids,
file_output_ids,

0 comments on commit f23da00

Please sign in to comment.