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.
[1]:
import os
from pathlib import Path
from ansys.speos.core import Project, Speos
from ansys.speos.core.sensor import SensorIrradiance
from ansys.speos.core.simulation import SimulationDirect
from ansys.speos.core.source import SourceLuminaire, SourceSurface
# If using docker container
assets_data_path = Path("/app") / "assets"
# If using local server
# assets_data_path = Path().resolve().parent.parent / "tests" / "assets"
# If using a different path
# assets_data_path = Path("path/to/downloaded/example/assets")
Create connection with speos rpc server#
[2]:
speos = Speos(host="localhost", port=50098)
New empty project#
An empty project can be created by only passing speos rpc server to the Project class.
[3]:
p = Project(speos=speos)
print(p)
{
"name": "",
"description": "",
"metadata": {},
"part_guid": "",
"sources": [],
"sensors": [],
"simulations": [],
"materials": [],
"scenes": []
}
create feature - e.g. source
[4]:
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()
[4]:
<ansys.speos.core.source.SourceLuminaire at 0x7f165a28aec0>
create feature - e.g. sensor
[5]:
sensor1 = p.create_sensor(name="Sensor.1")
sensor1.commit()
[5]:
<ansys.speos.core.sensor.SensorIrradiance at 0x7f165a28b1f0>
create feature - e.g. optical property
[6]:
opt_prop1 = p.create_optical_property(name="Material.1")
opt_prop1.commit()
[6]:
<ansys.speos.core.opt_prop.OptProp at 0x7f165a28ab00>
Read Project#
User can read the content of a project via simplily printing the project
[7]:
print(p)
{
"sources": [
{
"name": "Source.1",
"metadata": {
"UniqueId": "eb0dfb32-5b2b-4868-adc1-5941fa358535"
},
"source_guid": "86bff976-b290-4458-9152-a90faa31b4fc",
"description": "",
"source": {
"name": "Source.1",
"luminaire": {
"flux_from_intensity_file": {},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "fd5d2135-08d0-437e-bda3-237982c89bd8",
"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": "4840bd98-f196-465e-ada6-c9833fc15972"
},
"sensor_guid": "8348acd8-9b33-4a46-ba9f-5efbc8577ae5",
"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": "fc815763-d441-44a5-a56a-9255c31e291d"
},
"sop_guids": [
"f0483b29-6fea-4690-8a02-4254f368914b"
],
"description": "",
"sops": [
{
"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:
[8]:
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.
[9]:
features = p.find(name="UnexistingName")
print(features)
[]
[10]:
features = p.find(name="Sensor.1")
print(features[0])
{
"name": "Sensor.1",
"metadata": {
"UniqueId": "4840bd98-f196-465e-ada6-c9833fc15972"
},
"sensor_guid": "8348acd8-9b33-4a46-ba9f-5efbc8577ae5",
"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
[11]:
features = p.find(name="Sensor.1", feature_type=SourceLuminaire)
print(features)
[]
[12]:
features = p.find(name="Sensor.1", feature_type=SensorIrradiance)
print(features[0])
{
"name": "Sensor.1",
"metadata": {
"UniqueId": "4840bd98-f196-465e-ada6-c9833fc15972"
},
"sensor_guid": "8348acd8-9b33-4a46-ba9f-5efbc8577ae5",
"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
[13]:
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
[14]:
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
[15]:
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
[16]:
print(p.find(name="Sensor.1"))
[]
Create project from pre-defined speos project#
Via passing the .speos/.sv5 file path to the Project class.
[17]:
p2 = Project(
speos=speos,
path=str(assets_data_path / "LG_50M_Colorimetric_short.sv5" / "LG_50M_Colorimetric_short.sv5"),
)
print(p2)
{
"name": "LG_50M_Colorimetric_short",
"description": "From /app/assets/LG_50M_Colorimetric_short.sv5/LG_50M_Colorimetric_short.sv5",
"part_guid": "6e14b806-9f14-4a1e-8e7d-668d5082bda0",
"sources": [
{
"name": "Dom Source 2 (0) in SOURCE2",
"metadata": {
"UniqueId": "1b414996-81b4-4843-9b58-b091b222bbae"
},
"source_guid": "523f2204-e1eb-4b3c-b1f0-cb7280e1643a",
"description": "",
"source": {
"name": "Dom Source 2 (0) in SOURCE2",
"surface": {
"radiant_flux": {
"radiant_value": 6.590041607465698
},
"intensity_guid": "ab30cd22-ca8c-4442-a803-4903dfb8f80d",
"exitance_constant": {
"geo_paths": [
{
"geo_path": "Solid Body in SOURCE2:2920204960/Face in SOURCE2:222",
"reverse_normal": false
}
]
},
"spectrum_guid": "a68c90c6-5430-4530-9f10-8b6d02b9f3f7",
"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": "a2a82999-7bec-4b1e-b7ca-ec7603a6433b"
},
"source_guid": "5667b8b7-4187-47c0-9e22-8aadb58ea44b",
"description": "",
"source": {
"name": "Surface Source (0) in SOURCE1",
"surface": {
"radiant_flux": {
"radiant_value": 9.290411220389682
},
"intensity_guid": "4a46e806-089a-45cf-916f-45374b0c991e",
"exitance_constant": {
"geo_paths": [
{
"geo_path": "Solid Body in SOURCE1:2494956811/Face in SOURCE1:187",
"reverse_normal": false
}
]
},
"spectrum_guid": "914950db-3e2c-49fb-8672-44bce64a67bd",
"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": "f6813ff1-8ae4-4792-9b0a-6f5ae5815d6c"
},
"sensor_guid": "06271ff0-637d-4d35-9fb7-de6334d077ea",
"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": "59253e8f-4ee0-4389-b814-3f30c3784e01"
},
"simulation_guid": "4129de21-eb12-4996-b869-0e2d4bf1af05",
"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": "21ce5ae3-9518-40f8-963c-0d7e24516b1c",
"simulation_path": "ASSEMBLY1.DS (0)",
"job_type": "CPU"
}
}
],
"materials": [
{
"name": "Material.1",
"metadata": {
"UniqueId": "88aee1cb-6c9a-4620-a315-b325deb2951a"
},
"sop_guids": [
"90e8cc64-15f9-419e-a08b-76db7719eb57"
],
"geometries": {
"geo_paths": [
"Solid Body in GUIDE:1379760262/Face in GUIDE:169"
]
},
"description": "",
"sops": [
{
"mirror": {
"reflectance": 100.0
},
"name": "",
"description": "",
"metadata": {}
}
]
},
{
"name": "Material.2",
"metadata": {
"UniqueId": "3f743778-72e5-4a09-b000-1f57fa9ad0c9"
},
"vop_guid": "73aea630-27ac-4399-9bdf-e36e6858894d",
"sop_guids": [
"90e8cc64-15f9-419e-a08b-76db7719eb57"
],
"geometries": {
"geo_paths": [
"Solid Body in SOURCE2:2920204960",
"Solid Body in SOURCE1:2494956811"
]
},
"description": "",
"vop": {
"opaque": {},
"name": "",
"description": "",
"metadata": {}
},
"sops": [
{
"mirror": {
"reflectance": 100.0
},
"name": "",
"description": "",
"metadata": {}
}
]
},
{
"name": "Material.3",
"metadata": {
"UniqueId": "a992b3dd-fff3-4a5a-a2b0-6593d46989d6"
},
"vop_guid": "777cbd81-4982-49b4-bf78-193b2c1ad215",
"sop_guids": [
"41eaf5f4-921b-4e66-9577-025c49b60786"
],
"geometries": {
"geo_paths": [
"Solid Body in GUIDE:1379760262"
]
},
"description": "",
"vop": {
"optic": {
"index": 1.4,
"constringence": 60.0,
"absorption": 0.0
},
"name": "",
"description": "",
"metadata": {}
},
"sops": [
{
"optical_polished": {},
"name": "",
"description": "",
"metadata": {}
}
]
},
{
"name": "Material.4",
"metadata": {
"UniqueId": "34a0800d-390c-4c81-8b08-acf7785b2559"
},
"vop_guid": "08c10566-b37e-47a6-8f30-2f1250b23c40",
"description": "",
"sop_guids": [],
"vop": {
"optic": {
"index": 1.0,
"absorption": 0.0
},
"name": "",
"description": "",
"metadata": {}
}
}
],
"metadata": {},
"scenes": []
}
Preview the part information#
User can check the project part using preview method.
[18]:
p2.preview()
use find_key method to find specific information
[19]:
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': 'ab30cd22-ca8c-4442-a803-4903dfb8f80d', 'exitance_constant': {'geo_paths': [{'geo_path': 'Solid Body in SOURCE2:2920204960/Face in SOURCE2:222', 'reverse_normal': False}]}, 'spectrum_guid': 'a68c90c6-5430-4530-9f10-8b6d02b9f3f7', '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': '4a46e806-089a-45cf-916f-45374b0c991e', 'exitance_constant': {'geo_paths': [{'geo_path': 'Solid Body in SOURCE1:2494956811/Face in SOURCE1:187', 'reverse_normal': False}]}, 'spectrum_guid': '914950db-3e2c-49fb-8672-44bce64a67bd', '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
[20]:
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 0x7f165a2dfa30>, <ansys.speos.core.source.SourceSurface object at 0x7f165a2df790>]
<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:
[21]:
src.set_spectrum().set_monochromatic(wavelength=550)
src.commit()
[21]:
<ansys.speos.core.source.SourceSurface at 0x7f165a2df790>
Retrieve a simulation feature:
[22]:
features = p2.find(name=".*", name_regex=True, feature_type=SimulationDirect)
sim_feat = features[0]
print(sim_feat)
{
"name": "ASSEMBLY1.DS (0)",
"metadata": {
"UniqueId": "59253e8f-4ee0-4389-b814-3f30c3784e01"
},
"simulation_guid": "4129de21-eb12-4996-b869-0e2d4bf1af05",
"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": "21ce5ae3-9518-40f8-963c-0d7e24516b1c",
"simulation_path": "ASSEMBLY1.DS (0)",
"job_type": "CPU"
}
}
[23]:
sim_feat.compute_CPU()
[23]:
[upload_response {
info {
uri: "9d8b3d0d-550a-40cf-8838-8084f74be96d"
file_name: "ASSEMBLY1.DS (0).Dom Irradiance Sensor (0).xmp"
file_size: 1469200
}
upload_duration {
nanos: 2360824
}
}
, upload_response {
info {
uri: "b2c7d448-333b-4ad1-9b60-9f1e6486b936"
file_name: "ASSEMBLY1.DS (0).html"
file_size: 492660
}
upload_duration {
nanos: 727571
}
}
]
Preview simulation result (only windows)
[24]:
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",
)