.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/generative_design_ex/03-interpolate_geometries.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_generative_design_ex_03-interpolate_geometries.py: .. _ref_interpolate_geometries: Interpolating Between Geometries ============================================== This example demonstrates how to interpolate between two geometries in latent space. Latent space interpolation allows you to: - Create smooth transitions between existing designs. - Explore the design space systematically. - Generate variations that blend features from multiple geometries. Before you begin ------------------------------------------- - Complete ":ref:`ref_build_model`" to train a Generative design model. - Ensure that the model training has been completed successfully. To do so, verify if a new workspace was created for the trained model. .. GENERATED FROM PYTHON SOURCE LINES 46-48 Import necessary libraries ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-57 .. code-block:: Python import json import os from typing import Dict, List import ansys.simai.core as asc from ansys.simai.core.data.geomai.predictions import GeomAIPredictionConfiguration from ansys.simai.core.data.predictions import Prediction .. GENERATED FROM PYTHON SOURCE LINES 58-61 Configure your settings ------------------------------------------- Update these variables with your specific settings: .. GENERATED FROM PYTHON SOURCE LINES 61-74 .. code-block:: Python ORGANIZATION = "my_organization" # Replace with your organization name PROJECT_NAME = "new-bracket-project" # Replace with your project name WORKSPACE_NAME = "new-bracket-project #1" # Typically "{PROJECT_NAME} #{number}" OUTPUT_DIR = "interpolations" # Directory to save interpolated geometries NUM_STEPS = 10 # Number of interpolation steps RESOLUTION = (100, 100, 100) # Output resolution (x, y, z) # Choose geometries to interpolate between (by name) GEOM_A_NAME = "geometry_name_a" # Replace with actual geometry name GEOM_B_NAME = "geometry_name_b" # Replace with actual geometry name .. GENERATED FROM PYTHON SOURCE LINES 75-81 Define functions for interpolation ------------------------------------------- Before interpolating between two geometries, we need two key functions: 1. A function to efficiently extract the latent parameters from the training data. 2. A function to interpolate between two latent vectors. .. GENERATED FROM PYTHON SOURCE LINES 81-125 .. code-block:: Python def get_latent_parameters(workspace) -> Dict[str, List[float]]: """Download and load latent parameters for all geometries in the workspace. Parameters ---------- workspace : Workspace The workspace containing the trained model. Returns ------- Dict[str, List[float]] Dictionary mapping geometry names to their latent parameter vectors. """ os.makedirs("latent-parameters", exist_ok=True) path = os.path.join("latent-parameters", f"{workspace.name}.json") workspace.download_latent_parameters_json(path) with open(path, "r") as f: latent_dict = json.load(f) print(f"Loaded {len(latent_dict)} geometries' latent parameters.") return latent_dict def interpolate_latents(vec1: List[float], vec2: List[float], alpha: float) -> List[float]: """Perform linear interpolation between two latent vectors. Parameters ---------- vec1 : List[float] First latent parameter vector. vec2 : List[float] Second latent parameter vector. alpha : float Interpolation factor (0.0 = vec1, 1.0 = vec2). Returns ------- List[float] Interpolated latent parameter vector. """ return [(1 - alpha) * x + alpha * y for x, y in zip(vec1, vec2)] .. GENERATED FROM PYTHON SOURCE LINES 126-129 Initialize the client and get the workspace -------------------------------------------------- Connect to GeomAI and retrieve your trained workspace: .. GENERATED FROM PYTHON SOURCE LINES 129-136 .. code-block:: Python simai_client = asc.SimAIClient(organization=ORGANIZATION) geomai_client = simai_client.geomai workspace = geomai_client.workspaces.get(name=WORKSPACE_NAME) print(f"Using workspace: {workspace.name}") .. GENERATED FROM PYTHON SOURCE LINES 137-140 Get latent parameters for all geometries ----------------------------------------------- Download the latent parameters of all training geometries in the workspace: .. GENERATED FROM PYTHON SOURCE LINES 140-143 .. code-block:: Python latent_dict = get_latent_parameters(workspace) .. GENERATED FROM PYTHON SOURCE LINES 144-147 Display available geometries ---------------------------------- List all geometries available for interpolation: .. GENERATED FROM PYTHON SOURCE LINES 147-152 .. code-block:: Python print("\nAvailable geometries:") for i, name in enumerate(latent_dict.keys()): print(f" [{i}] {name}") .. GENERATED FROM PYTHON SOURCE LINES 153-156 Validate selected geometries ------------------------------------------- Check if the selected geometries exist: .. GENERATED FROM PYTHON SOURCE LINES 156-169 .. code-block:: Python if GEOM_A_NAME not in latent_dict: print(f"Error: Geometry '{GEOM_A_NAME}' not found in workspace.") print("Please update GEOM_A_NAME with a valid geometry name from the list above.") raise ValueError(f"Geometry '{GEOM_A_NAME}' not found") if GEOM_B_NAME not in latent_dict: print(f"Error: Geometry '{GEOM_B_NAME}' not found in workspace.") print("Please update GEOM_B_NAME with a valid geometry name from the list above.") raise ValueError(f"Geometry '{GEOM_B_NAME}' not found") print(f"\nInterpolating from '{GEOM_A_NAME}' to '{GEOM_B_NAME}' in {NUM_STEPS} steps.") .. GENERATED FROM PYTHON SOURCE LINES 170-173 Interpolate in latent space and generate geometries ---------------------------------------------------------- Generate intermediate geometries by linearly interpolating in latent space: .. GENERATED FROM PYTHON SOURCE LINES 173-192 .. code-block:: Python vec_a = latent_dict[GEOM_A_NAME] vec_b = latent_dict[GEOM_B_NAME] predictions: list[Prediction] = [] for i in range(NUM_STEPS + 1): alpha = i / NUM_STEPS latent_params = interpolate_latents(vec_a, vec_b, alpha) print(f"\nGenerating geometry {i}/{NUM_STEPS} (alpha={alpha:.2f})...") config = GeomAIPredictionConfiguration( latent_params=latent_params, resolution=RESOLUTION, ) prediction = geomai_client.predictions.run(config, workspace) print(f"Prediction {i}: {prediction.id} started...") predictions.append(prediction) .. GENERATED FROM PYTHON SOURCE LINES 193-195 Download generated geometries ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 195-211 .. code-block:: Python for i, prediction in enumerate(predictions): if prediction.wait(timeout=600): # Wait up to 10 minutes if prediction.has_failed: print(f"✗ Prediction {i} failed: {prediction.failure_reason}") continue # Save result out_dir = os.path.join(OUTPUT_DIR, workspace.name) os.makedirs(out_dir, exist_ok=True) out_path = os.path.join(out_dir, f"prediction_{i:02d}_{prediction.id}.vtp") geomai_client.predictions.download(prediction.id, out_path) print(f"✓ Saved prediction to {out_path}") else: print(f"✗ Prediction {i} timed out.") .. GENERATED FROM PYTHON SOURCE LINES 212-217 The downloaded VTP files can be used for: - Visualization in your usual solver. - SimAI training data or predictions. - Further analysis and post-processing. .. GENERATED FROM PYTHON SOURCE LINES 219-225 Next steps ------------------------------------------- To go further, you can: - Interpolate between more than two geometries. - Combine interpolation with optimization for specific design goals. .. _sphx_glr_download__examples_generative_design_ex_03-interpolate_geometries.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 03-interpolate_geometries.ipynb <03-interpolate_geometries.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03-interpolate_geometries.py <03-interpolate_geometries.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 03-interpolate_geometries.zip <03-interpolate_geometries.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_