Prism example#

This tutorial demonstrates how to open an existing Speos file, run the simulation and open the result. After that it explains how to edit the File and adjust Parameters of the sensor.

Prerequisites#

Perform imports#

[1]:
import os
from pathlib import Path

from ansys.speos.core import Body, Project, Speos
from ansys.speos.core.launcher import launch_local_speos_rpc_server
from ansys.speos.core.sensor import Sensor3DIrradiance, SensorIrradiance
from ansys.speos.core.simulation import SimulationDirect

Define constants#

The constants help ensure consistency and avoid repetition throughout the example.

[2]:
HOSTNAME = "localhost"
GRPC_PORT = 50098  # Be sure the Speos GRPC Server has been started on this port.
USE_DOCKER = True  # Set to False if you're running this example locally as a Notebook.

Model Setup#

Load assets#

The assets used to run this example are available in the PySpeos repository on GitHub.

Note: Make sure you have downloaded simulation assets and set assets_data_path to point to the assets folder.

[3]:
if USE_DOCKER:  # Running on the remote server.
    assets_data_path = Path("/app") / "assets"
else:
    assets_data_path = Path("/path/to/your/download/assets/directory")

Start/Connect to Speos RPC Server#

This Python client connects to a server where the Speos engine is running as a service. In this example, the server and client are the same machine. The launch_local_speos_rpc_method can be used to start a local instance of the service.

[4]:
if USE_DOCKER:
    speos = Speos(host=HOSTNAME, port=GRPC_PORT)
else:
    speos = launch_local_speos_rpc_server(port=GRPC_PORT)

Create project#

Load a project from .speos file.

[5]:
p = Project(speos=speos, path=str(assets_data_path / "Prism.speos" / "Prism.speos"))
print(p)
{
    "name": "Prism",
    "description": "From /app/assets/Prism.speos/Prism.speos",
    "part_guid": "9d9ec141-1276-492a-9f0d-d279b7a0a2aa",
    "sources": [
        {
            "name": "Surface.1:7758",
            "metadata": {
                "UniqueId": "125b2205-7852-4dc2-9116-d73514c2afbb"
            },
            "source_guid": "ab3027b5-70c6-4aaf-9491-9db2629d70cd",
            "description": "",
            "source": {
                "name": "Surface.1:7758",
                "surface": {
                    "radiant_flux": {
                        "radiant_value": 8.346636625511586
                    },
                    "intensity_guid": "0ea35dfe-27b4-4b89-b0a9-f87409d6f011",
                    "exitance_constant": {
                        "geo_paths": [
                            {
                                "geo_path": "SourceBody:1801986974/face.1:544289899",
                                "reverse_normal": false
                            }
                        ]
                    },
                    "spectrum_guid": "6ca90f93-ac39-446e-866d-a49ac3d01b4e",
                    "intensity": {
                        "cos": {
                            "N": 1.0,
                            "total_angle": 0.1
                        },
                        "name": "",
                        "description": "",
                        "metadata": {}
                    },
                    "spectrum": {
                        "blackbody": {
                            "temperature": 5000.0
                        },
                        "name": "",
                        "description": "",
                        "metadata": {}
                    }
                },
                "description": "",
                "metadata": {}
            }
        }
    ],
    "sensors": [
        {
            "name": "Irradiance.1:564",
            "metadata": {
                "UniqueId": "33d20a83-07fa-48b3-bb97-037b750d044b"
            },
            "sensor_guid": "4f8316b5-b00a-4900-b4e4-eb8b47ae79bc",
            "result_file_name": "Prism.Irradiance.1",
            "description": "",
            "sensor": {
                "irradiance_sensor_template": {
                    "sensor_type_spectral": {
                        "wavelengths_range": {
                            "w_start": 380.0,
                            "w_end": 780.0,
                            "w_sampling": 41
                        }
                    },
                    "illuminance_type_planar": {},
                    "dimensions": {
                        "x_start": -20.0,
                        "x_end": 20.0,
                        "x_sampling": 400,
                        "y_start": -5.0,
                        "y_end": 5.0,
                        "y_sampling": 100
                    },
                    "axis_system": [
                        921.3551052231927,
                        0.0,
                        388.72197015239516,
                        -0.3887219701523952,
                        -0.0,
                        0.9213551052231927,
                        0.0,
                        1.0000000000000002,
                        0.0,
                        -0.9213551052231927,
                        0.0,
                        -0.3887219701523952
                    ],
                    "layer_type_none": {},
                    "ray_file_type": "RayFileNone",
                    "integration_direction": []
                },
                "name": "Irradiance.1:564",
                "description": "",
                "metadata": {}
            }
        }
    ],
    "simulations": [
        {
            "name": "Prism",
            "metadata": {
                "UniqueId": "43d82067-956b-4161-95e0-84537580182d"
            },
            "simulation_guid": "406e2b87-1cc9-48da-b569-08a4e76e10b8",
            "sensor_paths": [
                "Irradiance.1:564"
            ],
            "source_paths": [
                "Surface.1:7758"
            ],
            "description": "",
            "simulation": {
                "direct_mc_simulation_template": {
                    "geom_distance_tolerance": 0.05,
                    "max_impact": 100,
                    "weight": {
                        "minimum_energy_percentage": 0.005
                    },
                    "dispersion": true,
                    "colorimetric_standard": "CIE_1931",
                    "fast_transmission_gathering": false,
                    "ambient_material_uri": ""
                },
                "name": "Prism",
                "metadata": {},
                "description": "",
                "scene_guid": "1941023e-174a-4186-9719-a8aab6126b70",
                "simulation_path": "Prism",
                "job_type": "CPU"
            }
        }
    ],
    "materials": [
        {
            "name": "Material.1",
            "metadata": {
                "UniqueId": "ee9c4b43-cfc1-4253-b78b-fa432a427804"
            },
            "sop_guids": [
                "4d545651-7274-4def-ade3-195d3707a21a"
            ],
            "geometries": {
                "geo_paths": [
                    "SourceBody:1801986974/face.1:544289899",
                    "SourceBody:1801986974/face.1:2282175023",
                    "SourceBody:1801986974/face.1:2503270562",
                    "SourceBody:1801986974/face.1:2272456582",
                    "SourceBody:1801986974/face.1:1596341914",
                    "SourceBody:1801986974/face.1:387288686"
                ]
            },
            "description": "",
            "sops": [
                {
                    "mirror": {
                        "reflectance": 0.0
                    },
                    "name": "",
                    "description": "",
                    "metadata": {}
                }
            ]
        },
        {
            "name": "Material.2",
            "metadata": {
                "UniqueId": "5045d734-680f-4461-82ec-7f9ff2a7bfc6"
            },
            "vop_guid": "43dc1e80-9c67-463e-bbc4-5accf644eb26",
            "sop_guids": [
                "7c7b2113-17ed-44e4-b275-55974c5b6b82"
            ],
            "geometries": {
                "geo_paths": [
                    "PrismBody:1130610277"
                ]
            },
            "description": "",
            "vop": {
                "optic": {
                    "index": 1.5,
                    "absorption": 0.001,
                    "constringence": 60.0
                },
                "name": "",
                "description": "",
                "metadata": {}
            },
            "sops": [
                {
                    "optical_polished": {},
                    "name": "",
                    "description": "",
                    "metadata": {}
                }
            ]
        },
        {
            "name": "Material.3",
            "metadata": {
                "UniqueId": "abed0905-c2c0-453a-9549-c0611287dd69"
            },
            "sop_guids": [
                "0e8cd0e1-d08b-4c57-97f0-2e33df119a2a"
            ],
            "geometries": {
                "geo_paths": [
                    "PrismBody:1130610277/face.1:242396665"
                ]
            },
            "description": "",
            "sops": [
                {
                    "library": {
                        "sop_file_uri": "/app/assets/Prism.speos/transparent_ad06-07cb-a92e-7815..scattering"
                    },
                    "name": "",
                    "description": "",
                    "metadata": {}
                }
            ]
        },
        {
            "name": "Material.4",
            "metadata": {
                "UniqueId": "79bbc9e8-9167-419d-902b-f480731b418a"
            },
            "vop_guid": "b6c307ac-e6f2-49cf-936f-8dd62b9bd305",
            "description": "",
            "sop_guids": [],
            "vop": {
                "optic": {
                    "index": 1.0,
                    "absorption": 0.0
                },
                "name": "",
                "description": "",
                "metadata": {}
            }
        }
    ],
    "metadata": {},
    "scenes": []
}
/home/runner/work/pyspeos/pyspeos/.venv/lib/python3.10/site-packages/ansys/speos/core/project.py:793: UserWarning: The pySpeos feature : SourceSurface needs a Speos Version of 2025 R2 SP0 or higher.
  src_feat = SourceSurface(
/home/runner/work/pyspeos/pyspeos/.venv/lib/python3.10/site-packages/ansys/speos/core/project.py:850: UserWarning: The pySpeos feature : SimulationDirect needs a Speos Version of 2025 R2 SP0 or higher.
  sim_feat = SimulationDirect(

Preview#

This preview method allows you to preview the content of the Speos solver file.

[6]:
p.preview()

Retrieve the simulation feature and open result#

Run the simulation

[7]:
sim_features = p.find(name="Prism", feature_type=SimulationDirect)
sim = sim_features[0]
sim.compute_CPU()
[7]:
[upload_response {
  info {
    uri: "fab66919-f5e8-4c00-9e81-a54c0a5ebae4"
    file_name: "Prism.Irradiance.1.xmp"
    file_size: 1568416
  }
  upload_duration {
    nanos: 4098636
  }
}
, upload_response {
  info {
    uri: "4180b1bf-61e1-4ba5-8bf5-ce5d574d9c0e"
    file_name: "Prism.html"
    file_size: 65419
  }
  upload_duration {
    nanos: 255698
  }
}
]

Use the open_result_image method to review the result

[8]:
if os.name == "nt":
    from ansys.speos.core.workflow.open_result import open_result_image

    open_result_image(simulation_feature=sim, result_name="Prism.Irradiance.1.xmp")

Work with sensor#

Retrieve the sensor feature.

Modify the sensor setting, e.g. set the spectral type, etc.

[9]:
irr_features = p.find(name=".*", name_regex=True, feature_type=SensorIrradiance)
irr = irr_features[0]
irr.set_type_spectral().set_wavelengths_range().set_start(500).set_end(600).set_sampling(11)
irr.commit()
[9]:
<ansys.speos.core.sensor.SensorIrradiance at 0x7f72d2bd8ac0>

Create and add a new sensor, e.g. 3d irradiance sensor

[10]:
body = p.find(name="PrismBody", name_regex=True, feature_type=Body)[0]
sensor_3d = p.create_sensor(name="3d_irradiance", feature_type=Sensor3DIrradiance)
sensor_3d.set_geometries([body.geo_path])
sensor_3d.commit()
sim.set_sensor_paths(["Irradiance.1:564", "3d_irradiance"])
sim.commit()
p.preview()

Re-run the simulation with new sensor definition.#

[11]:
sim.compute_CPU()
if os.name == "nt":
    open_result_image(simulation_feature=sim, result_name="Prism.Irradiance.1.xmp")
[12]:
speos.close()
[12]:
True