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 Project, Speos
from ansys.speos.core.launcher import launch_local_speos_rpc_server
from ansys.speos.core.sensor import 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": "902e01b3-6c1e-47a4-a266-91c286d6e397",
    "sources": [
        {
            "name": "Surface.1:7758",
            "metadata": {
                "UniqueId": "a2e740fa-389d-4adf-829a-8171ee191ee6"
            },
            "source_guid": "68998f7f-aa95-4961-b0e4-39dfe7ebff21",
            "description": "",
            "source": {
                "name": "Surface.1:7758",
                "surface": {
                    "radiant_flux": {
                        "radiant_value": 8.346636625511586
                    },
                    "intensity_guid": "646223f8-4ab6-4515-8cb7-82c05b12545c",
                    "exitance_constant": {
                        "geo_paths": [
                            {
                                "geo_path": "SourceBody:1801986974/face.1:544289899",
                                "reverse_normal": false
                            }
                        ]
                    },
                    "spectrum_guid": "47734aae-9d6e-4ab5-b8bb-a75f906f023b",
                    "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": "3bcc45de-6dfe-4124-b57b-0a766cf6f071"
            },
            "sensor_guid": "2e5224e4-7f3d-432d-b666-9ed693cd6858",
            "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": "3fd7dde9-9ef2-4770-a523-1b82491b0c21"
            },
            "simulation_guid": "66a48f0a-6145-4812-8e81-8ae16b6e5ed4",
            "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": "9c0cbaa2-924c-474b-bb94-e3d2a0c8defa",
                "simulation_path": "Prism",
                "job_type": "CPU"
            }
        }
    ],
    "materials": [
        {
            "name": "Material.1",
            "metadata": {
                "UniqueId": "8bbef980-fb22-4cd7-bc90-e906a8f3a919"
            },
            "sop_guids": [
                "db87b5a7-0e67-4323-b511-097813d86fcd"
            ],
            "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": "af966ddd-e9ec-49f5-aa24-65a21052a876"
            },
            "vop_guid": "809211db-fa1a-4eb3-a853-d8fbf15879da",
            "sop_guids": [
                "795df2dc-21db-409e-8979-5c36f2c3bb4d"
            ],
            "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": "c77e09c5-4329-48e9-b622-5afdac96b9d7"
            },
            "sop_guids": [
                "22200a9d-2070-4b38-9bb7-f83e10539be3"
            ],
            "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": "6d4579cf-a2e1-46c8-9d1a-0be4182b4172"
            },
            "vop_guid": "84385aa1-125b-484f-b907-535846957249",
            "description": "",
            "sop_guids": [],
            "vop": {
                "optic": {
                    "index": 1.0,
                    "absorption": 0.0
                },
                "name": "",
                "description": "",
                "metadata": {}
            }
        }
    ],
    "metadata": {},
    "scenes": []
}

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: "cf49eb9f-887c-4b3a-a801-b339f9134ad1"
    file_name: "Prism.Irradiance.1.xmp"
    file_size: 1569108
  }
  upload_duration {
    nanos: 3644368
  }
}
, upload_response {
  info {
    uri: "2a33e866-d0c1-4afc-938a-c6ed95501a8f"
    file_name: "Prism.html"
    file_size: 65534
  }
  upload_duration {
    nanos: 302355
  }
}
]

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 0x7f5db7866f80>

Re-run the simulation with new sensor definition.#

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