Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples to optionally use the official execution scripts #240

Merged
merged 5 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/source/examples/ex_lsdyna_job.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Usage:
python lsdyna_job.py monitor
python lsdyna_job.py download

.. note::
This example only runs on Windows platform as the LS-PrePost task requires to open the LS-PREPOST GUI.

.. note::
The ``download`` action requires ``tqdm`` and ``humanize`` packages to show a progress bar during the result files download. You can install them with ``python -m pip install tqdm humanize``.

Expand Down
39 changes: 31 additions & 8 deletions examples/fluent_2d_heat_exchanger/project_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
log = logging.getLogger(__name__)


def main(client: Client, name: str, version: str) -> Project:

def create_project(
client, name, version=__ansys_apps_version__, use_exec_script=False, active=True
) -> Project:
log.info("=== Create Project")
jms_api = JmsApi(client)
proj = Project(name=name, priority=1, active=True)
Expand All @@ -36,13 +37,13 @@ def main(client: Client, name: str, version: str) -> Project:

files = [
File(
name="cas_h5",
name="case",
evaluation_path="heat_exchanger.cas.h5",
type="application/octet-stream",
src=os.path.join(cwd, "heat_exchanger.cas.h5"),
),
File(
name="journal",
name="jou",
evaluation_path="heat_exchanger.jou",
type="text/plain",
src=os.path.join(cwd, "heat_exchanger.jou"),
Expand Down Expand Up @@ -72,22 +73,37 @@ def main(client: Client, name: str, version: str) -> Project:
log.debug("=== Job Definition with simulation workflow")

# Task Definition
task_defs = []
task_def = TaskDefinition(
name="Fluent Run",
software_requirements=[Software(name="Ansys Fluent", version=version)],
execution_command="%executable% 2d -g -tm %resource:num_cores% -i %file:journal%",
execution_command="%executable% 2d -g -tm %resource:num_cores% -i %file:jou%",
resource_requirements=ResourceRequirements(
num_cores=4,
memory=4000,
disk_space=500,
distributed=True,
),
execution_context={
"dimension": "2d",
},
max_execution_time=600.0,
execution_level=0,
num_trials=1,
input_file_ids=[file_ids["cas_h5"], file_ids["journal"]],
input_file_ids=[file_ids["case"], file_ids["jou"]],
output_file_ids=[file_ids["trn"], file_ids["output_cas"], file_ids["output_data"]],
)
task_def = project_api.create_task_definitions([task_def])[0]

if use_exec_script:
task_def.use_execution_script = True
exec_script_file = project_api.copy_default_execution_script(
f"fluent-v{version[2:4]}{version[6]}-exec_fluent.py"
)
task_def.execution_script_id = exec_script_file.id

task_defs.append(task_def)

task_def = project_api.create_task_definitions(task_defs)[0]

# Create job_definition in project
job_def = JobDefinition(name="JobDefinition.1", active=True)
Expand All @@ -111,6 +127,7 @@ def main(client: Client, name: str, version: str) -> Project:
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--name", type=str, default="Fluent 2D Heat Exchanger")
parser.add_argument("-es", "--use-exec-script", default=False, type=bool)
parser.add_argument("-U", "--url", default="https://localhost:8443/rep")
parser.add_argument("-u", "--username", default="repadmin")
parser.add_argument("-p", "--password", default="repadmin")
Expand All @@ -125,6 +142,12 @@ def main(client: Client, name: str, version: str) -> Project:
client = Client(rep_url=args.url, username=args.username, password=args.password)

try:
main(client, name=args.name, version=args.ansys_version)
log.info(f"REP URL: {client.rep_url}")
proj = create_project(
client=client,
name=args.name,
version=args.ansys_version,
use_exec_script=args.use_exec_script,
)
except REPError as e:
log.error(str(e))
40 changes: 30 additions & 10 deletions examples/lsdyna_cylinder_plate/lsdyna_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
USERNAME = "repadmin"
PASSWORD = "repadmin"
USE_LSDYNA_MPP = False
ANSYS_VERSION = "2023 R1"
ANSYS_VERSION = "2024 R1"


class REPJob:
Expand Down Expand Up @@ -86,7 +86,7 @@ def load(cls):
return job


def submit_job() -> REPJob:
def submit_job(use_exec_script=False) -> REPJob:
"""Create a REP project running a simple LS-DYNA
job simulating the impact of a cylinder made of Aluminum
against a plate made of steel.
Expand All @@ -109,7 +109,7 @@ def submit_job() -> REPJob:
# input files
files.append(
File(
name="input_deck",
name="inp",
evaluation_path="cylinder_plate.k",
type="text/plain",
src=os.path.join(cwd, "cylinder_plate.k"),
Expand Down Expand Up @@ -177,12 +177,11 @@ def submit_job() -> REPJob:
job_def = JobDefinition(name="JobDefinition.1", active=True)

# Define process steps (task definitions)
ls_dyna_command = "%executable% i=%file:input_deck% ncpu=%resource:num_cores% memory=300m"
ls_dyna_command = "%executable% i=%file:inp% ncpu=%resource:num_cores% memory=300m"
if USE_LSDYNA_MPP:
ls_dyna_command = (
"%executable% -dis -np %resource:num_cores% i=%file:input_deck% memory=300m"
)
ls_dyna_command = "%executable% -dis -np %resource:num_cores% i=%file:inp% memory=300m"

task_defs = []
task_def1 = TaskDefinition(
name="LS-DYNA Run",
software_requirements=[Software(name="Ansys LS-DYNA", version=ANSYS_VERSION)],
Expand All @@ -192,10 +191,11 @@ def submit_job() -> REPJob:
num_cores=6,
memory=6000 * 1024 * 1024,
disk_space=4000 * 1024 * 1024,
distributed=USE_LSDYNA_MPP,
),
execution_level=0,
num_trials=1,
input_file_ids=[file_ids["input_deck"]],
input_file_ids=[file_ids["inp"]],
output_file_ids=[file_ids["d3hsp"], file_ids["messag"], file_ids["d3plot"]],
success_criteria=SuccessCriteria(
return_code=0,
Expand All @@ -205,6 +205,16 @@ def submit_job() -> REPJob:
),
)

if use_exec_script:
exec_script_file = project_api.copy_default_execution_script(
f"lsdyna-v{ANSYS_VERSION[2:4]}{ANSYS_VERSION[6]}-exec_lsdyna.py"
)

task_def1.use_execution_script = True
task_def1.execution_script_id = exec_script_file.id

task_defs.append(task_def1)

task_def2 = TaskDefinition(
name="LS-PrePost Run",
software_requirements=[Software(name="Ansys LS-PrePost", version=ANSYS_VERSION)],
Expand All @@ -214,6 +224,8 @@ def submit_job() -> REPJob:
num_cores=2,
memory=3000,
disk_space=4000,
distributed=False,
platform="Windows",
),
execution_level=1,
num_trials=1,
Expand All @@ -226,7 +238,9 @@ def submit_job() -> REPJob:
),
)

task_definitions = project_api.create_task_definitions([task_def1, task_def2])
task_defs.append(task_def2)

task_definitions = project_api.create_task_definitions(task_defs)

# Create job definition in project
job_def = JobDefinition(name="JobDefinition.1", active=True)
Expand Down Expand Up @@ -373,11 +387,17 @@ def download_results(app_job: REPJob):
parser.add_argument(
"action", default="submit", choices=["submit", "monitor", "download"], help="Action to run"
)
parser.add_argument(
"--exec_script",
default=False,
type=bool,
help="Use default execution script while submitting job",
)

args = parser.parse_args()
try:
if args.action == "submit":
job = submit_job()
job = submit_job(args.exec_script)
job.save()
elif args.action == "monitor":
job = REPJob.load()
Expand Down
Loading