diff --git a/examples/mapdl_motorbike_frame/design_points_stats.py b/examples/mapdl_motorbike_frame/project_query.py similarity index 59% rename from examples/mapdl_motorbike_frame/design_points_stats.py rename to examples/mapdl_motorbike_frame/project_query.py index 71dbff9a8..c75ce9e6e 100644 --- a/examples/mapdl_motorbike_frame/design_points_stats.py +++ b/examples/mapdl_motorbike_frame/project_query.py @@ -1,6 +1,13 @@ -"""Example to query evaluated jobs and compute some simple statistics on parameter values.""" +""" +Example to query things from a project. + +- Query values from evaluated jobs,computing some simple statistics on parameter values. +- Download files from the project + +""" import argparse import logging +import os from statistics import mean, stdev from ansys.rep.client import REPError @@ -19,6 +26,7 @@ def print_value_stats(values, title): def query_stats(client, project_name): """Query statistics.""" + log.info("=== Query values and compoute statistics ===") log.info("=== Project") project = client.get_project(name=project_name) log.info(f"ID: {project.id}") @@ -56,6 +64,45 @@ def query_stats(client, project_name): print_value_stats(values, "=== Running time (finished_time - running_time") +def download_files(client, project_name): + """Download files.""" + out_path = os.path.join(os.path.dirname(__file__), "downloads") + log.info(f"Downloading files to {out_path}") + + project = client.get_project(name=project_name) + # Todo: Fix needed in the backend: + # Currently only the get_project() called with id returns all fields of project. + project = client.get_project(id=project.id) + + log.info(f"Project id: {project.id}") + + jobs = project.get_jobs(eval_status="evaluated", fields=["id", "values", "elapsed_time"]) + log.info(f"# evaluated jobs: {len(jobs)}") + num = min(len(jobs), 10) + + log.info(f"=== Example 1: Downloading output files of {num} jobs using File.download()") + for job in jobs[0:num]: + log.info(f"Job {job.id}") + for task in job.get_tasks(): + log.info(f"Task {task.id}") + files = project.get_files(id=task.output_file_ids) + for f in files: + fpath = os.path.join(out_path, f"task_{task.id}") + log.info(f"Download output file {f.evaluation_path} to {fpath}") + f.download(target_path=fpath) + + num = 10 + files = project.get_files(type="image/jpeg", limit=num, content=True) + num = len(jobs) + log.info(f"=== Example 2: Global download of {num} jpeg images in project using content=True") + for f in files: + if f.content: + fpath = os.path.join(out_path, f"task_{f.id}_{f.evaluation_path}") + log.info(f"Download image file to {fpath}") + with open(fpath, "wb") as bf: + bf.write(f.content) + + if __name__ == "__main__": parser = argparse.ArgumentParser() @@ -75,5 +122,7 @@ def query_stats(client, project_name): log.info(f"REP URL: {client.rep_url}") query_stats(client=client, project_name=args.name) + download_files(client=client, project_name=args.name) + except REPError as e: log.error(str(e)) diff --git a/examples/mapdl_motorbike_frame/task_files.py b/examples/mapdl_motorbike_frame/task_files.py index d9e9113f6..aab97be54 100644 --- a/examples/mapdl_motorbike_frame/task_files.py +++ b/examples/mapdl_motorbike_frame/task_files.py @@ -14,6 +14,10 @@ def modify_task_files(client, project_name): cwd = os.path.dirname(__file__) proj = client.get_project(name=project_name) + # Todo: Fix needed in the backend: + # Currently only the get_project() called with id returns all fields of project. + proj = client.get_project(id=proj.id) + log.info(f"proj={proj}") # Identify mac input file in task definition @@ -48,7 +52,7 @@ def modify_task_files(client, project_name): # Add specific task files to tasks tasks = proj.get_tasks(limit=5) - for t in tasks: + for i, t in enumerate(tasks): log.debug(f"Modify task {t.id}") files = [] @@ -62,7 +66,7 @@ def modify_task_files(client, project_name): # Add an extra task input file mac2_fpath = os.path.join(cwd, f"task_input_file_{t.id}.mac") with open(mac2_fpath, "w") as f: - f.write(f"task_var={t.id}") + f.write(f"task_var={i}") files.append( File( name="mac2",