How to preview a light expert result#

This tutorial demonstrates how to review the light expert simulation result.

Prerequisites#

Perform imports#

[1]:
from pathlib import Path

from ansys.speos.core import LightPathFinder, Project, Speos, launcher
from ansys.speos.core.simulation import SimulationInteractive

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.
RESULT_NAME = "Direct.1.Irradiance.1.lpf"

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 = launcher.launch_local_speos_rpc_server(port=GRPC_PORT)

Create a new project#

In this example, a project is created via reading a pre-defined .speos file. It can be found there is volume conflict in this project.

[5]:
p = Project(
    speos=speos,
    path=str(assets_data_path / "error_data.speos" / "error_data.speos"),
)
p.preview(viz_args={"opacity": 0.7})
/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(

Retrieve the simulation feature, add light expert and run#

[6]:
sim = p.find("Direct.1")[0]
sim.set_light_expert(True)
sim.commit()
sim.compute_CPU()
/tmp/ipykernel_8493/3328249474.py:2: UserWarning: Please note that setting a value for light expert option forces a sensorcommit when committing the Simulation class
  sim.set_light_expert(True)
[6]:
[upload_response {
  info {
    uri: "a6736664-a06a-4768-b060-e486691bc069"
    file_name: "Direct.1.Irradiance.1.xmp"
    file_size: 1721902
  }
  upload_duration {
    nanos: 5069346
  }
}
, upload_response {
  info {
    uri: "c4d17073-7a2c-4efe-8358-41932156faa2"
    file_name: "Direct.1.Irradiance.1.lpf"
    file_size: 4690879
  }
  upload_duration {
    nanos: 6343087
  }
}
, upload_response {
  info {
    uri: "87cea39f-db18-4542-878f-ca2814027b51"
    file_name: "Direct.1.html"
    file_size: 276582
  }
  upload_duration {
    nanos: 724915
  }
}
]

If looking to the simulation report, we will find that we have 40% simulation error

[7]:
import ansys.speos.core.workflow.open_result as orf

# Methods from workflow class provide a way to find the correct result file.
# Detailed information can be found in the workflow_open_result example.
data = orf._find_correct_result(sim, "Direct.1.html")

when reviewing The ray data using LightPathFinder class. We can see a lot of rays missing

[8]:
path = orf._find_correct_result(sim, RESULT_NAME, download_if_distant=False)
lxp = LightPathFinder(speos, path)
lxp.preview(project=p)
[8]:
<ansys.speos.core.lxp.LightPathFinder at 0x7f2328358df0>

Create an Interactive simulation with light expert#

We will define an interactive simulation to have a look at the rays in error as a direct simulation will only show the rays hitting the sensor not the rays in error.

[9]:
interactive_sim = p.create_simulation("error", feature_type=SimulationInteractive)
interactive_sim.set_light_expert(True)
interactive_sim.set_sensor_paths(["Irradiance.1:70"])
interactive_sim.set_source_paths(["Surface.1:4830"])
interactive_sim.commit()
[9]:
<ansys.speos.core.simulation.SimulationInteractive at 0x7f2328359030>

Preview the light expert result#

Here, we will run the simulation and preview the result via LightPathFinder class. By default, the LightPathFinder class will preview the first 100 rays stored in the lpf-file.

[10]:
results = interactive_sim.compute_CPU()
path = orf._find_correct_result(interactive_sim, "error.lpf", download_if_distant=False)
lxp = LightPathFinder(speos, path)
lxp.preview(project=p)
[10]:
<ansys.speos.core.lxp.LightPathFinder at 0x7f2328359210>

Preview the light expert result with error filter#

ray_filter option is provided in the preview function that user can filter the rays to see only rays in error. In this example, error rays are generated due to a volume conflict between two solids.

[11]:
lxp.filter_error_rays()
lxp.preview(project=p, ray_filter=True)
[11]:
<ansys.speos.core.lxp.LightPathFinder at 0x7f2328359210>
[12]:
speos.close()
[12]:
True