MAPDL motorbike frame: Project query#

This example builds upon the MAPDL motorbike frame: Project creation example. It shows how to query resources of an existing project and how to download output files.

You can download the ZIP file for the MAPDL motorbike frame example and use a tool such as 7-Zip to extract the files.

Here is the file for this project:
Example to query resources 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.hps.client import Client, HPSError
from ansys.hps.client.jms import JmsApi, ProjectApi

log = logging.getLogger(__name__)

def print_value_stats(values, title):
    """Print value stats.""""{title}")
    #"Num      : {len(values)}")"Mean     : {mean(values)} Stdev: {stdev(values)}")"(Min, Max): ({min(values)}, {max(values)})")

def query_stats(client, project_name):
    """Query statistics.""""=== Query values and compoute statistics ===")
    jms_api = JmsApi(client)"=== Project")
    project = jms_api.get_project_by_name(name=project_name)"ID: {}")"Created on: {project.creation_time}")

    project_api = ProjectApi(client,"=== Query jobs")
    jobs = project_api.get_jobs(eval_status="evaluated", fields=["id", "values", "elapsed_time"])
    if not jobs:"No evaluated jobs found to compute statistics from, exiting")
        return"Statistics across {len(jobs)} jobs")

    values = [job.values["mapdl_elapsed_time_obtain_license"] for job in jobs]
        values, "=== License checkoput (parameter: mapdl_elapsed_time_obtain_license)"

    values = [job.values["mapdl_elapsed_time"] for job in jobs]
    print_value_stats(values, "=== Elapsed time MAPDL (mapdl_elapsed_time)")

    values = [job.elapsed_time for job in jobs]
    print_value_stats(values, "=== Elapsed time HPS (elapsed_time)")"=== Query tasks")
    tasks = project_api.get_tasks(
        eval_status="evaluated", fields=["id", "prolog_time", "running_time", "finished_time"]
    )"Statistics across tasks")

    values = [(t.running_time - t.prolog_time).total_seconds() for t in tasks]
    print_value_stats(values, "=== Prolog time (running_time - prolog_time")

    values = [(t.finished_time - t.running_time).total_seconds() for t in tasks]
    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")"Downloading files to {out_path}")

    jms_api = JmsApi(client)
    project = jms_api.get_project_by_name(name=project_name)
    # Todo: Fix needed in the backend:
    # Currently only the get_project() called with id returns all fields of project.
    project = jms_api.get_project("Project id: {}")
    project_api = ProjectApi(client,

    jobs = project_api.get_jobs(eval_status="evaluated", fields=["id", "values", "elapsed_time"])"# evaluated jobs: {len(jobs)}")
    num = min(len(jobs), 10)
        f"=== Example 1: Downloading output files of {num} jobs using ProjectApi.download_file()"
    for job in jobs[0:num]:"Job {}")
        for task in project_api.get_tasks(
  "Task {}")
            files = project_api.get_files(id=task.output_file_ids)
            for f in files:
                fpath = os.path.join(out_path, f"task_{}")
      "Download output file {f.evaluation_path} to {fpath}")
                project_api.download_file(file=f, target_path=fpath)

    num = 10
    files = project_api.get_files(type="image/jpeg", limit=num, content=True)
    num = len(jobs)"=== 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.evaluation_path}")
  "Download image file to {fpath}")
            with open(fpath, "wb") as bf:

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("-n", "--name", type=str, default="Mapdl Motorbike Frame")
    parser.add_argument("-j", "--num-jobs", type=int, default=500)
    parser.add_argument("-U", "--url", default="")
    parser.add_argument("-u", "--username", default="repuser")
    parser.add_argument("-p", "--password", default="repuser")
    args = parser.parse_args()

    logger = logging.getLogger()
    logging.basicConfig(format="%(message)s", level=logging.DEBUG)

    try:"Connect to HPC Platform Services")
        client = Client(url=args.url, username=args.username, password=args.password)"HPS URL: {client.url}")


    except HPSError as e: