.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/00_basic_simai_ex/02-run_predictions.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_00_basic_simai_ex_02-run_predictions.py: .. _ref_basic_run_predictions: Running Physics Predictions ================================================ This example demonstrates how to upload geometries to a workspace and run predictions using a trained SimAI model. It also shows how to extract and save prediction results, including global coefficients and confidence scores. Before you begin ---------------------------------------------------- - Complete ":ref:`ref_basic_build_model`" to train a SimAI model. - Ensure the model training completed successfully. - Have a dataset folder with subdirectories containing geometry files. These geometry files can come from a Generative Design model. - (Optional) Prepare boundary condition JSON files if your model requires them. .. GENERATED FROM PYTHON SOURCE LINES 43-45 Import necessary libraries ---------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 45-53 .. code-block:: Python import json import os import ansys.simai.core as asc from ansys.simai.core.data.geometries import Geometry from ansys.simai.core.data.predictions import Prediction .. GENERATED FROM PYTHON SOURCE LINES 54-57 Configure your settings ---------------------------------------------------- Update these variables with your specific settings: .. GENERATED FROM PYTHON SOURCE LINES 57-62 .. code-block:: Python ORGANIZATION_NAME = "" # Replace with your organization name WORKSPACE_NAME = "" # Replace with your workspace name DATASET_PATH = "" # Path to your dataset directory .. GENERATED FROM PYTHON SOURCE LINES 63-66 Initialize the client and set workspace ---------------------------------------------------- Connect to SimAI and set the workspace to use for predictions: .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: Python simai_client = asc.SimAIClient(organization=ORGANIZATION_NAME) .. GENERATED FROM PYTHON SOURCE LINES 70-71 Set the current workspace: .. GENERATED FROM PYTHON SOURCE LINES 71-76 .. code-block:: Python simai_client.set_current_workspace(WORKSPACE_NAME) current_workspace = simai_client.current_workspace print(f"Using workspace: {current_workspace.name}") .. GENERATED FROM PYTHON SOURCE LINES 77-78 A workspace contains a trained model and is created when a model build completes successfully. .. GENERATED FROM PYTHON SOURCE LINES 80-83 Prepare data structures ---------------------------------------------------- Create lists to store geometries and their boundary conditions: .. GENERATED FROM PYTHON SOURCE LINES 83-87 .. code-block:: Python geometries: list[Geometry] = [] boundary_conditions: list[dict] = [] .. GENERATED FROM PYTHON SOURCE LINES 88-91 Upload geometries and load boundary conditions ---------------------------------------------------- Process each subdirectory in the dataset: .. GENERATED FROM PYTHON SOURCE LINES 91-132 .. code-block:: Python for dir in os.listdir(f"{DATASET_PATH}"): print(f"Processing directory: {dir}") # Define the geometry file path # The file is uploaded with a specific name for easy identification file = ( f"{DATASET_PATH}/{dir}/surface.vtp", # Local file path f"{dir}.vtp", # Name to use in SimAI ) # Check if this geometry already exists in the workspace geoms = simai_client.geometries.list( workspace=current_workspace, ) if f"{dir}.vtp" in [geom.name for geom in geoms]: print(f"Geometry {dir}.vtp already uploaded, skipping upload.") # Find and use the existing geometry existing_geom = [geom for geom in geoms if geom.name == f"{dir}.vtp"][0] geometries.append(existing_geom) else: print(f"Uploading geometry {dir}.vtp...") geom = simai_client.geometries.upload(file, workspace=current_workspace) geometries.append(geom) ########################################################################### # Load boundary conditions (if applicable) # ---------------------------------------------------- # This section is optional and only needed if your model requires boundary conditions. # If your model does not use boundary conditions, you can skip this section. try: with open(f"{DATASET_PATH}/{dir}/boundary_conditions.json", "r") as f: print(f"Loading boundary conditions for {dir}...") boundary_conditions_for_geom = json.load(f) boundary_conditions.append(boundary_conditions_for_geom) except FileNotFoundError: print(f"No boundary conditions file found for {dir}, using empty boundary conditions.") boundary_conditions.append({}) .. GENERATED FROM PYTHON SOURCE LINES 133-136 Run predictions on all geometries ---------------------------------------------------- Wait for all geometries to be processed, then run predictions: .. GENERATED FROM PYTHON SOURCE LINES 136-156 .. code-block:: Python preds: list[Prediction] = [] print("\nWaiting for geometries to be processed...") for geom in geometries: print(f"Waiting for geometry {geom.name}...") geom.wait() print(f"Geometry {geom.name} is processed.") # Run prediction with boundary conditions if available if boundary_conditions[geometries.index(geom)]: preds.append( geom.run_prediction(boundary_conditions=boundary_conditions[geometries.index(geom)]) ) print(f"Prediction started for geometry {geom.name} with boundary conditions.") else: # Run prediction without boundary conditions preds.append(geom.run_prediction()) print(f"Prediction started for geometry {geom.name}.") .. GENERATED FROM PYTHON SOURCE LINES 157-160 Collect prediction results ---------------------------------------------------- Wait for all predictions to complete and extract results: .. GENERATED FROM PYTHON SOURCE LINES 160-174 .. code-block:: Python results = {} print("\nWaiting for predictions to complete...") for pred in preds: geom_name = geometries[preds.index(pred)].name print(f"Waiting for prediction on geometry {geom_name}...") pred.wait() print(f"Prediction completed for geometry {geom_name}.") # Extract global coefficients and confidence score results[geom_name] = pred.post.global_coefficients().data results[geom_name]["confidence_score"] = pred.raw_confidence_score .. GENERATED FROM PYTHON SOURCE LINES 175-178 Save results to file ---------------------------------------------------- Export all prediction results to a JSON file for further analysis: .. GENERATED FROM PYTHON SOURCE LINES 178-185 .. code-block:: Python print("\nSaving results to results.json...") with open("results.json", "w") as f: json.dump(results, f, indent=4) print("Results saved successfully!") .. GENERATED FROM PYTHON SOURCE LINES 186-197 Understanding the results ---------------------------------------------------- The results dictionary contains: - Global coefficients: Scalar values computed from the prediction, - Confidence score: A measure of how familiar this prediction is for the model. You can also download full field results (VTP files) for visualization: - Use ``pred.post.surface_vtp()`` to get surface fields. - Use ``pred.post.volume_vtu()`` to get volume fields. .. GENERATED FROM PYTHON SOURCE LINES 199-202 Example: Download surface VTP for visualization ---------------------------------------------------- Run the following code to download the first prediction's surface VTP: .. GENERATED FROM PYTHON SOURCE LINES 202-210 .. code-block:: Python if preds: first_pred = preds[0] first_geom_name = geometries[0].name output_path = f"{first_geom_name}_result.vtp" first_pred.post.surface_vtp().data.download(output_path) print(f"Downloaded surface VTP to {output_path}") .. GENERATED FROM PYTHON SOURCE LINES 211-219 Next steps ---------------------------------------------------- With your prediction results, you can: - Analyze global coefficients to evaluate design performance. - Visualize field results in ParaView or similar tools. - Compare predictions against validation data. - Use results to guide design optimization. .. _sphx_glr_download__examples_00_basic_simai_ex_02-run_predictions.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02-run_predictions.ipynb <02-run_predictions.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02-run_predictions.py <02-run_predictions.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 02-run_predictions.zip <02-run_predictions.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_