.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/generative_design_ex/02-generate_random_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_02-generate_random_geometries.py: .. _ref_generate_random_geometries: Generating Random Geometries ========================================== This example demonstrates how to generate random geometries using random latent parameters. Before you begin ------------------------------------------- - Complete ":ref:`ref_build_model`" to train a Generative Design model. - Ensure the model training completed successfully. .. GENERATED FROM PYTHON SOURCE LINES 39-41 Import necessary libraries ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-51 .. code-block:: Python import json import os import random 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 52-55 Configure your settings ------------------------------------------- Update these variables with your specific settings: .. GENERATED FROM PYTHON SOURCE LINES 55-63 .. 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 = "random_geometries" # Directory to save generated geometries NUM_GEOMETRIES = 5 # Number of random geometries to generate RESOLUTION = (100, 100, 100) # Output resolution (x, y, z) .. GENERATED FROM PYTHON SOURCE LINES 64-67 Initialize the client and get the workspace ------------------------------------------- Connect to the instance: .. GENERATED FROM PYTHON SOURCE LINES 67-71 .. code-block:: Python simai_client = asc.SimAIClient(organization=ORGANIZATION) geomai_client = simai_client.geomai .. GENERATED FROM PYTHON SOURCE LINES 72-73 Retrieve the trained workspace by its name: .. GENERATED FROM PYTHON SOURCE LINES 73-78 .. code-block:: Python workspace = geomai_client.workspaces.get(name=WORKSPACE_NAME) print(f"Using workspace: {workspace.name}") .. GENERATED FROM PYTHON SOURCE LINES 79-82 Get the number of latent parameters ------------------------------------------- The number of latent parameters is defined during model training: .. GENERATED FROM PYTHON SOURCE LINES 82-109 .. 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 latent_dict = get_latent_parameters(workspace) nb_latent_params = len(next(iter(latent_dict.values()))) print(f"Workspace uses {nb_latent_params} latent parameters") .. GENERATED FROM PYTHON SOURCE LINES 110-113 Create output directory ------------------------------------------- Create a directory to save the generated geometries: .. GENERATED FROM PYTHON SOURCE LINES 113-118 .. code-block:: Python output_dir = os.path.join(OUTPUT_DIR, workspace.name) os.makedirs(output_dir, exist_ok=True) print(f"Output directory: {output_dir}") .. GENERATED FROM PYTHON SOURCE LINES 119-123 Generate random geometries ------------------------------------------- Generate geometries by creating random latent parameter vectors. Each latent parameter is randomly sampled from a standard normal distribution. .. GENERATED FROM PYTHON SOURCE LINES 123-143 .. code-block:: Python predictions: list[Prediction] = [] print(f"\nGenerating {NUM_GEOMETRIES} random geometries...") for i in range(NUM_GEOMETRIES): # Generate random latent parameters (from standard normal distribution) latent_params = [random.gauss(0, 1) for _ in range(nb_latent_params)] # Create prediction configuration config = GeomAIPredictionConfiguration( latent_params=latent_params, resolution=RESOLUTION, ) # Run the prediction prediction = geomai_client.predictions.run(config, workspace) print(f"Prediction {i + 1}/{NUM_GEOMETRIES}: {prediction.id} started...") predictions.append(prediction) .. GENERATED FROM PYTHON SOURCE LINES 144-146 Download generated geometries ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 146-161 .. code-block:: Python for i, prediction in enumerate(predictions): # Wait for prediction to complete if prediction.wait(timeout=600): # Wait up to 10 minutes if prediction.has_failed: print(f"✗ Prediction {i + 1} failed: {prediction.failure_reason}") continue # Download the generated geometry output_path = os.path.join(output_dir, f"random_{i + 1:02d}_{prediction.id}.vtp") geomai_client.predictions.download(prediction.id, output_path) print(f"✓ Saved geometry to {output_path}") else: print(f"✗ Prediction {i + 1} timed out") .. GENERATED FROM PYTHON SOURCE LINES 162-167 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 169-176 Tips for better results ------------------------------------------- - Latent parameters typically range from -3 to +3 for meaningful results. - Adjust the resolution to balance quality and file size. - Increase timeout for complex geometries. - Use the workspace's latent space statistics (min, max) for better sampling. .. GENERATED FROM PYTHON SOURCE LINES 178-181 Next steps ------------------------------------------- To generate geometries with more control, see :ref:`ref_interpolate_geometries`. .. _sphx_glr_download__examples_generative_design_ex_02-generate_random_geometries.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02-generate_random_geometries.ipynb <02-generate_random_geometries.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02-generate_random_geometries.py <02-generate_random_geometries.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 02-generate_random_geometries.zip <02-generate_random_geometries.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_