How to create a project#
This tutorial demonstrates how to create a project.
What is a project?#
A project is a speos simulation container that includes parts, material properties, sensor, sources and simulations.
In this tutorial you will learn how to create a project from scratch or from a pre-defined .speos file.
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
from ansys.speos.core.source import SourceLuminaire, SourceSurface
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)
New empty project#
An empty project can be created by only passing speos rpc server to the Project class.
[5]:
p = Project(speos=speos)
print(p)
{
"name": "",
"description": "",
"metadata": {},
"part_guid": "",
"sources": [],
"sensors": [],
"simulations": [],
"materials": [],
"scenes": []
}
Create features#
The Project class has a multitude of method to create Speos features. each create methedo takes the name and the Feature type as arguments and returns the created Feature #### Source
[6]:
source1 = p.create_source(name="Source.1", feature_type=SourceLuminaire)
source1.set_intensity_file_uri(uri=str(assets_data_path / "IES_C_DETECTOR.ies"))
source1.commit()
/home/runner/work/pyspeos/pyspeos/.venv/lib/python3.10/site-packages/ansys/speos/core/project.py:214: UserWarning: The pySpeos feature : SourceLuminaire needs a Speos Version of 2025 R2 SP0 or higher.
feature = SourceLuminaire(
[6]:
<ansys.speos.core.source.SourceLuminaire at 0x7f8419fda590>
Sensor#
[7]:
sensor1 = p.create_sensor(name="Sensor.1")
sensor1.commit()
[7]:
<ansys.speos.core.sensor.SensorIrradiance at 0x7f8419fd98d0>
Optical property#
[8]:
opt_prop1 = p.create_optical_property(name="Material.1")
opt_prop1.commit()
[8]:
<ansys.speos.core.opt_prop.OptProp at 0x7f8419fdabf0>
Read Project#
User can read the content of a project via simply printing the project
[9]:
print(p)
{
"sources": [
{
"name": "Source.1",
"metadata": {
"UniqueId": "50a31509-4942-4d64-af09-ed90914e4fe7"
},
"source_guid": "06991719-c6d9-4887-9f2e-934c002f1d5d",
"description": "",
"source": {
"name": "Source.1",
"luminaire": {
"flux_from_intensity_file": {},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "7591db82-3040-4369-9201-5153a8d0e349",
"spectrum": {
"name": "Source.1.Spectrum",
"predefined": {
"incandescent": {}
},
"description": "",
"metadata": {}
},
"axis_system": [
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
]
},
"description": "",
"metadata": {}
}
}
],
"sensors": [
{
"name": "Sensor.1",
"metadata": {
"UniqueId": "52305e67-4cab-40be-b216-28bf519d9d13"
},
"sensor_guid": "6a34c6aa-1148-4211-8520-2225f26158a2",
"description": "",
"result_file_name": "",
"sensor": {
"irradiance_sensor_template": {
"sensor_type_photometric": {},
"illuminance_type_planar": {},
"dimensions": {
"x_start": -50.0,
"x_end": 50.0,
"x_sampling": 100,
"y_start": -50.0,
"y_end": 50.0,
"y_sampling": 100
},
"axis_system": [
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
],
"layer_type_none": {},
"ray_file_type": "RayFileNone",
"integration_direction": []
},
"name": "Sensor.1",
"description": "",
"metadata": {}
}
}
],
"materials": [
{
"name": "Material.1",
"metadata": {
"UniqueId": "1ee551da-c0b2-4990-81d9-18a7f6216f48"
},
"sop_guid": "426c297f-5deb-41bc-8a8d-75098d991d47",
"description": "",
"sop_guids": [],
"sop": {
"name": "Material.1.SOP",
"mirror": {
"reflectance": 100.0
},
"description": "",
"metadata": {}
}
}
],
"name": "",
"description": "",
"metadata": {},
"part_guid": "",
"simulations": [],
"scenes": []
}
Or, user can use the find_key method to read a specific feature:
[10]:
for it in p.find_key(key="monochromatic"):
print(it)
Find a feature inside a project#
Use find method with an exact name#
If no feature is found, an empty list is returned.
[11]:
features = p.find(name="UnexistingName")
print(features)
[]
[12]:
features = p.find(name="Sensor.1")
print(features[0])
{
"name": "Sensor.1",
"metadata": {
"UniqueId": "52305e67-4cab-40be-b216-28bf519d9d13"
},
"sensor_guid": "6a34c6aa-1148-4211-8520-2225f26158a2",
"description": "",
"result_file_name": "",
"sensor": {
"irradiance_sensor_template": {
"sensor_type_photometric": {},
"illuminance_type_planar": {},
"dimensions": {
"x_start": -50.0,
"x_end": 50.0,
"x_sampling": 100,
"y_start": -50.0,
"y_end": 50.0,
"y_sampling": 100
},
"axis_system": [
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
],
"layer_type_none": {},
"ray_file_type": "RayFileNone",
"integration_direction": []
},
"name": "Sensor.1",
"description": "",
"metadata": {}
}
}
Use find method with feature type#
Here a wrong type is given: no source is called Sensor.1 in the project
[13]:
features = p.find(name="Sensor.1", feature_type=SourceLuminaire)
print(features)
[]
[14]:
features = p.find(name="Sensor.1", feature_type=SensorIrradiance)
print(features[0])
{
"name": "Sensor.1",
"metadata": {
"UniqueId": "52305e67-4cab-40be-b216-28bf519d9d13"
},
"sensor_guid": "6a34c6aa-1148-4211-8520-2225f26158a2",
"description": "",
"result_file_name": "",
"sensor": {
"irradiance_sensor_template": {
"sensor_type_photometric": {},
"illuminance_type_planar": {},
"dimensions": {
"x_start": -50.0,
"x_end": 50.0,
"x_sampling": 100,
"y_start": -50.0,
"y_end": 50.0,
"y_sampling": 100
},
"axis_system": [
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
],
"layer_type_none": {},
"ray_file_type": "RayFileNone",
"integration_direction": []
},
"name": "Sensor.1",
"description": "",
"metadata": {}
}
}
Use find method with approximation name with regex#
find a feature with name starting with Mat
[15]:
features = p.find(name="Mat.*", name_regex=True)
for feat in features:
print(str(type(feat)) + " : name=" + feat._name)
<class 'ansys.speos.core.opt_prop.OptProp'> : name=Material.1
find all features without defining any name
[16]:
features = p.find(name=".*", name_regex=True)
for feat in features:
print(str(type(feat)) + " : name=" + feat._name)
<class 'ansys.speos.core.source.SourceLuminaire'> : name=Source.1
<class 'ansys.speos.core.sensor.SensorIrradiance'> : name=Sensor.1
<class 'ansys.speos.core.opt_prop.OptProp'> : name=Material.1
Delete#
This erases the scene content in server database.
This deletes also each feature of the project
[17]:
p.delete()
print(p)
{
"name": "",
"description": "",
"metadata": {},
"part_guid": "",
"sources": [],
"sensors": [],
"simulations": [],
"materials": [],
"scenes": []
}
As the features were deleted just above -> this returns an empty vector
[18]:
print(p.find(name="Sensor.1"))
[]
Create project from pre-defined speos project#
Via passing the .speos/.sv5 file path to the Project class.
[19]:
p2 = Project(
speos=speos,
path=str(assets_data_path / "LG_50M_Colorimetric_short.sv5" / "LG_50M_Colorimetric_short.sv5"),
)
print(p2)
/home/runner/work/pyspeos/pyspeos/.venv/lib/python3.10/site-packages/ansys/speos/core/project.py:698: UserWarning: The pySpeos feature : FaceStub.read_batch needs a Speos Version of 2025 R2 SP0 or higher.
f_data_list = face_db.read_batch(refs=f_links)
{
"name": "LG_50M_Colorimetric_short",
"description": "From /app/assets/LG_50M_Colorimetric_short.sv5/LG_50M_Colorimetric_short.sv5",
"part_guid": "f510201b-fdf5-4213-ac2d-b85c86df2a10",
"sources": [
{
"name": "Dom Source 2 (0) in SOURCE2",
"metadata": {
"UniqueId": "28ca5226-0de1-4ca0-92b6-7535c69d8086"
},
"source_guid": "f76e56af-64f2-4f0a-b9e0-5f28a24e19bf",
"description": "",
"source": {
"name": "Dom Source 2 (0) in SOURCE2",
"surface": {
"radiant_flux": {
"radiant_value": 6.590041607465698
},
"intensity_guid": "267d8fd3-0b26-47da-9608-0dabfd573d59",
"exitance_constant": {
"geo_paths": [
{
"geo_path": "Solid Body in SOURCE2:2920204960/Face in SOURCE2:222",
"reverse_normal": false
}
]
},
"spectrum_guid": "e54ab6fa-9a2c-4378-aee0-58bf576feb20",
"intensity": {
"cos": {
"N": 1.0,
"total_angle": 180.0
},
"name": "",
"description": "",
"metadata": {}
},
"spectrum": {
"library": {
"file_uri": "/app/assets/LG_50M_Colorimetric_short.sv5/Red Spectrum.spectrum"
},
"name": "",
"description": "",
"metadata": {}
}
},
"description": "",
"metadata": {}
}
},
{
"name": "Surface Source (0) in SOURCE1",
"metadata": {
"UniqueId": "84bc18c0-1e2c-4455-8fd3-12e4c0a09912"
},
"source_guid": "19b42602-9ca3-4cb0-aa5b-14046a9990dc",
"description": "",
"source": {
"name": "Surface Source (0) in SOURCE1",
"surface": {
"radiant_flux": {
"radiant_value": 9.290411220389682
},
"intensity_guid": "af33332d-81b1-40e0-9878-396abdaedf2a",
"exitance_constant": {
"geo_paths": [
{
"geo_path": "Solid Body in SOURCE1:2494956811/Face in SOURCE1:187",
"reverse_normal": false
}
]
},
"spectrum_guid": "18672d5d-7e83-4018-afb0-097957eb6e18",
"intensity": {
"cos": {
"N": 1.0,
"total_angle": 180.0
},
"name": "",
"description": "",
"metadata": {}
},
"spectrum": {
"library": {
"file_uri": "/app/assets/LG_50M_Colorimetric_short.sv5/Blue Spectrum.spectrum"
},
"name": "",
"description": "",
"metadata": {}
}
},
"description": "",
"metadata": {}
}
}
],
"sensors": [
{
"name": "Dom Irradiance Sensor (0)",
"metadata": {
"UniqueId": "760796b0-983a-471b-8504-2cc944df6759"
},
"sensor_guid": "0c5b10d2-ead1-493a-8596-7eac53ad86b6",
"result_file_name": "ASSEMBLY1.DS (0).Dom Irradiance Sensor (0)",
"description": "",
"sensor": {
"irradiance_sensor_template": {
"sensor_type_colorimetric": {
"wavelengths_range": {
"w_start": 400.0,
"w_end": 700.0,
"w_sampling": 25
}
},
"illuminance_type_planar": {},
"dimensions": {
"x_start": -20.0,
"x_end": 20.0,
"x_sampling": 500,
"y_start": -20.0,
"y_end": 20.0,
"y_sampling": 500
},
"axis_system": [
-42.0,
2.0,
5.0,
0.0,
1.0,
0.0,
0.0,
0.0,
-1.0,
-1.0,
0.0,
0.0
],
"layer_type_source": {},
"integration_direction": [
1.0,
-0.0,
-0.0
],
"ray_file_type": "RayFileNone"
},
"name": "Dom Irradiance Sensor (0)",
"description": "",
"metadata": {}
}
}
],
"simulations": [
{
"name": "ASSEMBLY1.DS (0)",
"metadata": {
"UniqueId": "1967c207-9930-4d91-a9ea-be875cbcc2dd"
},
"simulation_guid": "2a64904b-876c-4df0-a92f-8e202f2a7d64",
"sensor_paths": [
"Dom Irradiance Sensor (0)"
],
"source_paths": [
"Dom Source 2 (0) in SOURCE2",
"Surface Source (0) in SOURCE1"
],
"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": "ASSEMBLY1.DS (0)",
"metadata": {},
"description": "",
"scene_guid": "b1f17cf9-6549-4e42-a893-537996f4e9c1",
"simulation_path": "ASSEMBLY1.DS (0)",
"job_type": "CPU"
}
}
],
"materials": [
{
"name": "Material.1",
"metadata": {
"UniqueId": "be258707-4209-4dc0-9db8-ddf1098bab8e"
},
"geometries": {
"geo_paths": [
"Solid Body in GUIDE:1379760262/Face in GUIDE:169"
]
},
"sop_guid": "5377f576-b722-4a82-9973-bbc79ccae4bd",
"description": "",
"sop_guids": [],
"sop": {
"mirror": {
"reflectance": 100.0
},
"name": "",
"description": "",
"metadata": {}
}
},
{
"name": "Material.2",
"metadata": {
"UniqueId": "aef78787-2a12-4bde-98ba-4315f1407009"
},
"vop_guid": "62efd9d5-eabe-4920-9602-ed5883ceb965",
"geometries": {
"geo_paths": [
"Solid Body in SOURCE2:2920204960",
"Solid Body in SOURCE1:2494956811"
]
},
"sop_guid": "5377f576-b722-4a82-9973-bbc79ccae4bd",
"description": "",
"sop_guids": [],
"vop": {
"opaque": {},
"name": "",
"description": "",
"metadata": {}
},
"sop": {
"mirror": {
"reflectance": 100.0
},
"name": "",
"description": "",
"metadata": {}
}
},
{
"name": "Material.3",
"metadata": {
"UniqueId": "fc0d263d-b4d4-4cd2-bf79-84bd9e8cca67"
},
"vop_guid": "2323c1c8-7bd9-46f9-a80d-946564f794af",
"geometries": {
"geo_paths": [
"Solid Body in GUIDE:1379760262"
]
},
"sop_guid": "7e3108e4-c6d6-449a-9125-aa270922d7b3",
"description": "",
"sop_guids": [],
"vop": {
"optic": {
"index": 1.4,
"constringence": 60.0,
"absorption": 0.0
},
"name": "",
"description": "",
"metadata": {}
},
"sop": {
"optical_polished": {},
"name": "",
"description": "",
"metadata": {}
}
},
{
"name": "Material.4",
"metadata": {
"UniqueId": "70cbb371-0bc2-4598-b0bf-8e0cf56a750c"
},
"vop_guid": "63eec954-df9d-42bc-bdae-ba708b0d91f0",
"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:804: 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:861: UserWarning: The pySpeos feature : SimulationDirect needs a Speos Version of 2025 R2 SP0 or higher.
sim_feat = SimulationDirect(
Preview the part information#
User can check the project part using preview method.
[20]:
p2.preview()
use find_key method to find specific information
[21]:
for it in p2.find_key(key="surface"):
print(it)
(".sources[.name='Dom Source 2 (0) in SOURCE2'].source.surface", {'radiant_flux': {'radiant_value': 6.590041607465698}, 'intensity_guid': '267d8fd3-0b26-47da-9608-0dabfd573d59', 'exitance_constant': {'geo_paths': [{'geo_path': 'Solid Body in SOURCE2:2920204960/Face in SOURCE2:222', 'reverse_normal': False}]}, 'spectrum_guid': 'e54ab6fa-9a2c-4378-aee0-58bf576feb20', 'intensity': {'cos': {'N': 1.0, 'total_angle': 180.0}, 'name': '', 'description': '', 'metadata': {}}, 'spectrum': {'library': {'file_uri': '/app/assets/LG_50M_Colorimetric_short.sv5/Red Spectrum.spectrum'}, 'name': '', 'description': '', 'metadata': {}}})
(".sources[.name='Surface Source (0) in SOURCE1'].source.surface", {'radiant_flux': {'radiant_value': 9.290411220389682}, 'intensity_guid': 'af33332d-81b1-40e0-9878-396abdaedf2a', 'exitance_constant': {'geo_paths': [{'geo_path': 'Solid Body in SOURCE1:2494956811/Face in SOURCE1:187', 'reverse_normal': False}]}, 'spectrum_guid': '18672d5d-7e83-4018-afb0-097957eb6e18', 'intensity': {'cos': {'N': 1.0, 'total_angle': 180.0}, 'name': '', 'description': '', 'metadata': {}}, 'spectrum': {'library': {'file_uri': '/app/assets/LG_50M_Colorimetric_short.sv5/Blue Spectrum.spectrum'}, 'name': '', 'description': '', 'metadata': {}}})
Use find method to retrieve feature:
e.g. surface source
[22]:
features = p2.find(name=".*", name_regex=True, feature_type=SourceSurface)
print(features)
for feat in features:
print(str(type(feat)) + " : name=" + feat._name)
src = features[1]
[<ansys.speos.core.source.SourceSurface object at 0x7f8414bd82e0>, <ansys.speos.core.source.SourceSurface object at 0x7f8414bd8490>]
<class 'ansys.speos.core.source.SourceSurface'> : name=Dom Source 2 (0) in SOURCE2
<class 'ansys.speos.core.source.SourceSurface'> : name=Surface Source (0) in SOURCE1
modify the surface source, e.g. surface source wavelength:
[23]:
src.set_spectrum().set_monochromatic(wavelength=550)
src.commit()
[23]:
<ansys.speos.core.source.SourceSurface at 0x7f8414bd8490>
Retrieve a simulation feature:
[24]:
features = p2.find(name=".*", name_regex=True, feature_type=SimulationDirect)
sim_feat = features[0]
print(sim_feat)
{
"name": "ASSEMBLY1.DS (0)",
"metadata": {
"UniqueId": "1967c207-9930-4d91-a9ea-be875cbcc2dd"
},
"simulation_guid": "2a64904b-876c-4df0-a92f-8e202f2a7d64",
"sensor_paths": [
"Dom Irradiance Sensor (0)"
],
"source_paths": [
"Dom Source 2 (0) in SOURCE2",
"Surface Source (0) in SOURCE1"
],
"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": "ASSEMBLY1.DS (0)",
"metadata": {},
"description": "",
"scene_guid": "b1f17cf9-6549-4e42-a893-537996f4e9c1",
"simulation_path": "ASSEMBLY1.DS (0)",
"job_type": "CPU"
}
}
[25]:
sim_feat.compute_CPU()
[25]:
[upload_response {
info {
uri: "52a710da-3a9b-47c4-8b5c-5ddaa62979ee"
file_name: "ASSEMBLY1.DS (0).Dom Irradiance Sensor (0).xmp"
file_size: 1473234
}
upload_duration {
nanos: 2094470
}
}
, upload_response {
info {
uri: "9c1d3c80-de5c-40ec-a097-08e566bf168a"
file_name: "ASSEMBLY1.DS (0).html"
file_size: 495380
}
upload_duration {
nanos: 781825
}
}
]
Preview simulation result (only windows)
[26]:
if os.name == "nt":
from ansys.speos.core.workflow.open_result import open_result_image
open_result_image(
simulation_feature=sim_feat,
result_name="ASSEMBLY1.DS (0).Dom Irradiance Sensor (0).xmp",
)
[27]:
speos.close()
[27]:
True