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 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=SourceSurface)
source1.commit()
[4]:
<ansys.speos.core.source.SourceSurface at 0x7fc3e2128880>
create feature - e.g. sensor
[5]:
sensor1 = p.create_sensor(name="Sensor.1")
sensor1.commit()
[5]:
<ansys.speos.core.sensor.SensorIrradiance at 0x7fc3e2128a00>
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 0x7fc3e2129510>
Read Project#
User can read the content of a project via simplily printing the project
[7]:
print(p)
{
"sources": [
{
"name": "Source.1",
"metadata": {
"UniqueId": "3eaca563-eb82-4537-b098-b05eab21208b"
},
"source_guid": "f01f85d2-5dee-4a46-8e23-5f3fb7c78487",
"description": "",
"source": {
"name": "Source.1",
"surface": {
"luminous_flux": {
"luminous_value": 683.0
},
"intensity_guid": "75280aaa-1790-43d4-968e-aef9bf237f50",
"exitance_constant": {
"geo_paths": []
},
"spectrum_guid": "59fb7a1a-ed29-47b0-ba29-670686a1e16f",
"intensity": {
"name": "Source.1.Intensity",
"cos": {
"N": 1.0,
"total_angle": 180.0
},
"description": "",
"metadata": {}
},
"spectrum": {
"name": "Source.1.Spectrum",
"monochromatic": {
"wavelength": 555.0
},
"description": "",
"metadata": {}
}
},
"description": "",
"metadata": {}
}
}
],
"sensors": [
{
"name": "Sensor.1",
"metadata": {
"UniqueId": "a7fcd232-7927-4334-8b6c-cba55ff6bed3"
},
"sensor_guid": "5c10caf7-4761-4b96-9f8d-735230572e62",
"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": "de0e480e-72a6-4cdd-855b-a1151c365ecb"
},
"sop_guids": [
"24963391-20f0-436a-8b87-7dc98eb4c6dc"
],
"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)
(".sources[.name='Source.1'].source.surface.spectrum.monochromatic", {'wavelength': 555.0})
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": "a7fcd232-7927-4334-8b6c-cba55ff6bed3"
},
"sensor_guid": "5c10caf7-4761-4b96-9f8d-735230572e62",
"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=SourceSurface)
print(features)
[]
[12]:
features = p.find(name="Sensor.1", feature_type=SensorIrradiance)
print(features[0])
{
"name": "Sensor.1",
"metadata": {
"UniqueId": "a7fcd232-7927-4334-8b6c-cba55ff6bed3"
},
"sensor_guid": "5c10caf7-4761-4b96-9f8d-735230572e62",
"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.SourceSurface'> : 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": "781dfeae-c4f4-41ab-91eb-1ac835571bb7",
"sources": [
{
"name": "Dom Source 2 (0) in SOURCE2",
"metadata": {
"UniqueId": "5e77f062-33af-4b02-9c1a-920ab2d9ba23"
},
"source_guid": "dd0137a9-3fb7-41d5-8d98-738223ec3dea",
"description": "",
"source": {
"name": "Dom Source 2 (0) in SOURCE2",
"surface": {
"radiant_flux": {
"radiant_value": 6.590041607465698
},
"intensity_guid": "316fc2ee-84b7-41ca-857d-33902c848f4f",
"exitance_constant": {
"geo_paths": [
{
"geo_path": "Solid Body in SOURCE2:2920204960/Face in SOURCE2:222",
"reverse_normal": false
}
]
},
"spectrum_guid": "ead9ef9f-92c0-4a80-9de8-b8487f5dd0ac",
"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": "44461cc3-6cdd-42c2-8628-8ddae522a362"
},
"source_guid": "00c73f8d-729f-4c5a-b117-2bddee919863",
"description": "",
"source": {
"name": "Surface Source (0) in SOURCE1",
"surface": {
"radiant_flux": {
"radiant_value": 9.290411220389682
},
"intensity_guid": "355dda5c-5756-4868-beba-240064198794",
"exitance_constant": {
"geo_paths": [
{
"geo_path": "Solid Body in SOURCE1:2494956811/Face in SOURCE1:187",
"reverse_normal": false
}
]
},
"spectrum_guid": "194a0e8d-c2b6-4fc1-8029-a81cdad087e9",
"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": "b3140ff3-3c81-490e-806f-31d694cf6d24"
},
"sensor_guid": "ec63d585-58b5-4ec3-8940-9dd9702affad",
"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": "a18d5b00-4ef8-4880-b3c8-cf0c7236de2c"
},
"simulation_guid": "15e4ba52-ddb9-4a0b-bbe6-97efcaf8b0dc",
"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": "7acdd11b-16c2-46ba-b1e1-d3c20c79c3d5",
"simulation_path": "ASSEMBLY1.DS (0)",
"job_type": "CPU"
}
}
],
"materials": [
{
"name": "Material.1",
"metadata": {
"UniqueId": "407e0809-4324-4dcd-b6dc-bb841cc8c55e"
},
"sop_guids": [
"69de0af1-e052-400a-86ed-579b5a3809e5"
],
"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": "e1866976-9c2a-4543-afde-82894ab071b8"
},
"vop_guid": "ac68a6d3-a53c-4b61-97e5-f78833eb3fa1",
"sop_guids": [
"69de0af1-e052-400a-86ed-579b5a3809e5"
],
"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": "ee48046a-76d5-4bb1-9901-1ce46d1730fa"
},
"vop_guid": "9ce3d564-d16d-4394-87e3-f06bc67b5f9f",
"sop_guids": [
"cdf662eb-18e8-4a79-a213-c300b88a64bc"
],
"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": "cf75b054-2512-46bd-839c-cf4b8f2fe147"
},
"vop_guid": "cbaccbe8-153e-4a42-9630-d163d0117a15",
"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': '316fc2ee-84b7-41ca-857d-33902c848f4f', 'exitance_constant': {'geo_paths': [{'geo_path': 'Solid Body in SOURCE2:2920204960/Face in SOURCE2:222', 'reverse_normal': False}]}, 'spectrum_guid': 'ead9ef9f-92c0-4a80-9de8-b8487f5dd0ac', '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': '355dda5c-5756-4868-beba-240064198794', 'exitance_constant': {'geo_paths': [{'geo_path': 'Solid Body in SOURCE1:2494956811/Face in SOURCE1:187', 'reverse_normal': False}]}, 'spectrum_guid': '194a0e8d-c2b6-4fc1-8029-a81cdad087e9', '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)
for feat in features:
print(str(type(feat)) + " : name=" + feat._name)
src = features[1]
<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 0x7fc3e212a140>
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": "a18d5b00-4ef8-4880-b3c8-cf0c7236de2c"
},
"simulation_guid": "15e4ba52-ddb9-4a0b-bbe6-97efcaf8b0dc",
"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": "7acdd11b-16c2-46ba-b1e1-d3c20c79c3d5",
"simulation_path": "ASSEMBLY1.DS (0)",
"job_type": "CPU"
}
}
[23]:
sim_feat.compute_CPU()
[23]:
[upload_response {
info {
uri: "304fad63-6dee-46f1-a57a-df0c6f08536a"
file_name: "ASSEMBLY1.DS (0).Dom Irradiance Sensor (0).xmp"
file_size: 1471213
}
upload_duration {
nanos: 2259634
}
}
, upload_response {
info {
uri: "4f135162-0306-4983-af8c-9effe5968f96"
file_name: "ASSEMBLY1.DS (0).html"
file_size: 495759
}
upload_duration {
nanos: 790957
}
}
]
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",
)