How to create a source#
This tutorial demonstrates how to create a source.
There are different type of sources available: luminaire source, surface source, ray file source.
Prerequisites#
Perform imports#
[1]:
from pathlib import Path
from ansys.speos.core import GeoRef, Project, Speos
from ansys.speos.core.source import (
SourceLuminaire,
SourceRayFile,
SourceSurface,
)
Define constants#
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.
IES = "IES_C_DETECTOR.ies"
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")
Connect to the 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.
[4]:
speos = Speos(host=HOSTNAME, port=GRPC_PORT)
Create a new project#
The only way to create a source using the core layer, is to create it from a project. The Project
class is instantiated by passing a Speos
instance
[5]:
p = Project(speos=speos)
print(p)
{
"name": "",
"description": "",
"metadata": {},
"part_guid": "",
"sources": [],
"sensors": [],
"simulations": [],
"materials": [],
"scenes": []
}
Source Creation#
Create locally: The mention “local: “ is added when printing the source data and information is not yet pushed to the RPC server
[6]:
intensity_file_path = str(assets_data_path / IES)
source1 = p.create_source(name="Luminaire.1", feature_type=SourceLuminaire) # type luminaire
source1.set_intensity_file_uri(uri=intensity_file_path)
print(source1)
local: {
"name": "Luminaire.1",
"description": "",
"metadata": {},
"source_guid": "",
"source": {
"name": "Luminaire.1",
"luminaire": {
"flux_from_intensity_file": {},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "",
"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": {}
}
}
Push it to the server.
After it is committed to the server, the mention “local: “ is no more present when printing the source.
[7]:
source1.commit()
print(source1)
{
"name": "Luminaire.1",
"metadata": {
"UniqueId": "93a5d90f-9d81-4a2b-abd0-8c32a4dde6cc"
},
"source_guid": "f5415a39-f3f3-4d4d-8b56-da7495f6d8a9",
"description": "",
"source": {
"name": "Luminaire.1",
"luminaire": {
"flux_from_intensity_file": {},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "89bd8588-498b-403a-85e4-fb20dcc15c4d",
"spectrum": {
"name": "Luminaire.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": {}
}
}
Changing additional Source Properties
Setting several more characteristics.
[8]:
intensity_file_path = str(assets_data_path / IES)
source2 = p.create_source(name="Luminaire.2", feature_type=SourceLuminaire)
source2.set_intensity_file_uri(uri=intensity_file_path)
source2.set_flux_radiant() # select flux radiant with default value
# choose the source location [Origin, Xvector, Yvector, Zvector]
source2.set_axis_system(axis_system=[20, 50, 10, 1, 0, 0, 0, 1, 0, 0, 0, 1])
source2.set_spectrum().set_blackbody() # choose blackbody with default value for the spectrum
source2.commit() # Push to the server
print(source2)
{
"name": "Luminaire.2",
"metadata": {
"UniqueId": "1068dc7d-c0b8-4fa3-8d52-81b66ea1a61d"
},
"source_guid": "8f6a160a-7d5a-4e14-ae7d-cfab97b6b02a",
"description": "",
"source": {
"name": "Luminaire.2",
"luminaire": {
"radiant_flux": {
"radiant_value": 1.0
},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "f3557958-4f27-498d-8097-f4adb0180c4c",
"spectrum": {
"name": "Luminaire.2.Spectrum",
"blackbody": {
"temperature": 2856.0
},
"description": "",
"metadata": {}
},
"axis_system": [
20.0,
50.0,
10.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
]
},
"description": "",
"metadata": {}
}
}
Source Instance
As mention “local: “ is added if it is not yet committed to the server.
[9]:
print(source1)
{
"name": "Luminaire.1",
"metadata": {
"UniqueId": "93a5d90f-9d81-4a2b-abd0-8c32a4dde6cc"
},
"source_guid": "f5415a39-f3f3-4d4d-8b56-da7495f6d8a9",
"description": "",
"source": {
"name": "Luminaire.1",
"luminaire": {
"flux_from_intensity_file": {},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "89bd8588-498b-403a-85e4-fb20dcc15c4d",
"spectrum": {
"name": "Luminaire.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": {}
}
}
Project:
Committed feature will appear inside the project information.
[10]:
print(p)
{
"sources": [
{
"name": "Luminaire.1",
"metadata": {
"UniqueId": "93a5d90f-9d81-4a2b-abd0-8c32a4dde6cc"
},
"source_guid": "f5415a39-f3f3-4d4d-8b56-da7495f6d8a9",
"description": "",
"source": {
"name": "Luminaire.1",
"luminaire": {
"flux_from_intensity_file": {},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "89bd8588-498b-403a-85e4-fb20dcc15c4d",
"spectrum": {
"name": "Luminaire.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": {}
}
},
{
"name": "Luminaire.2",
"metadata": {
"UniqueId": "1068dc7d-c0b8-4fa3-8d52-81b66ea1a61d"
},
"source_guid": "8f6a160a-7d5a-4e14-ae7d-cfab97b6b02a",
"description": "",
"source": {
"name": "Luminaire.2",
"luminaire": {
"radiant_flux": {
"radiant_value": 1.0
},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "f3557958-4f27-498d-8097-f4adb0180c4c",
"spectrum": {
"name": "Luminaire.2.Spectrum",
"blackbody": {
"temperature": 2856.0
},
"description": "",
"metadata": {}
},
"axis_system": [
20.0,
50.0,
10.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
]
},
"description": "",
"metadata": {}
}
}
],
"name": "",
"description": "",
"metadata": {},
"part_guid": "",
"sensors": [],
"simulations": [],
"materials": [],
"scenes": []
}
Update:
Note: If you are manipulating a source already committed, don’t forget to commit your changes. If you don’t, you will still only watch what is committed on the server.
[11]:
source1.set_flux_radiant(value=1.2) # modify radiant flux value
source1.set_axis_system(axis_system=[17, 10, 10, 1, 0, 0, 0, 1, 0, 0, 0, 1]) # modify axis system
source1.set_spectrum().set_halogen() # modify spectrum by choosing halogen
source1.commit() # Push changes to the server
print(source1)
{
"name": "Luminaire.1",
"metadata": {
"UniqueId": "93a5d90f-9d81-4a2b-abd0-8c32a4dde6cc"
},
"source_guid": "f5415a39-f3f3-4d4d-8b56-da7495f6d8a9",
"description": "",
"source": {
"name": "Luminaire.1",
"luminaire": {
"radiant_flux": {
"radiant_value": 1.2
},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "89bd8588-498b-403a-85e4-fb20dcc15c4d",
"spectrum": {
"name": "Luminaire.1.Spectrum",
"predefined": {
"halogen": {}
},
"description": "",
"metadata": {}
},
"axis_system": [
17.0,
10.0,
10.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
]
},
"description": "",
"metadata": {}
}
}
Reset
Possibility to reset local values from the one available in the server.
[12]:
source1.set_flux_luminous() # modify to luminous flux BUT no commit
source1.reset()
# reset -> this will apply the server value to the local value the local value will be back to
# halogen
source1.delete() # delete (to display the local value with the below print)
print(source1)
local: {
"name": "Luminaire.1",
"description": "",
"metadata": {},
"source_guid": "",
"source": {
"name": "Luminaire.1",
"luminaire": {
"radiant_flux": {
"radiant_value": 1.2
},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "89bd8588-498b-403a-85e4-fb20dcc15c4d",
"spectrum": {
"name": "Luminaire.1.Spectrum",
"predefined": {
"halogen": {}
},
"description": "",
"metadata": {}
},
"axis_system": [
17.0,
10.0,
10.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
]
},
"description": "",
"metadata": {}
}
}
Delete
Once the data is deleted from the server, you can still work with local data and maybe commit later.
[13]:
source2.delete()
print(source2)
source1.delete()
print(p)
local: {
"name": "Luminaire.2",
"description": "",
"metadata": {},
"source_guid": "",
"source": {
"name": "Luminaire.2",
"luminaire": {
"radiant_flux": {
"radiant_value": 1.0
},
"intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
"spectrum_guid": "f3557958-4f27-498d-8097-f4adb0180c4c",
"spectrum": {
"name": "Luminaire.2.Spectrum",
"blackbody": {
"temperature": 2856.0
},
"description": "",
"metadata": {}
},
"axis_system": [
20.0,
50.0,
10.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
]
},
"description": "",
"metadata": {}
}
}
{
"name": "",
"description": "",
"metadata": {},
"part_guid": "",
"sources": [],
"sensors": [],
"simulations": [],
"materials": [],
"scenes": []
}
Other Sources Examples#
Ray-file source#
[14]:
ray_file_path = str(assets_data_path / "Rays.ray")
source3 = p.create_source(name="Ray-file.1", feature_type=SourceRayFile) # type ray file
source3.set_ray_file_uri(uri=ray_file_path)
source3.commit()
print(source3)
{
"name": "Ray-file.1",
"metadata": {
"UniqueId": "fb1867f2-8afa-4f05-9379-96c58c9f717e"
},
"source_guid": "d39327f5-3a11-400a-b775-1ee89b352dc7",
"description": "",
"source": {
"name": "Ray-file.1",
"rayfile": {
"ray_file_uri": "/app/assets/Rays.ray",
"flux_from_ray_file": {},
"spectrum_from_ray_file": {},
"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": {}
}
}
[15]:
source3.set_flux_luminous()
source3.commit()
print(source3)
{
"name": "Ray-file.1",
"metadata": {
"UniqueId": "fb1867f2-8afa-4f05-9379-96c58c9f717e"
},
"source_guid": "d39327f5-3a11-400a-b775-1ee89b352dc7",
"description": "",
"source": {
"name": "Ray-file.1",
"rayfile": {
"ray_file_uri": "/app/assets/Rays.ray",
"luminous_flux": {
"luminous_value": 683.0
},
"spectrum_from_ray_file": {},
"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": {}
}
}
[16]:
source3.delete()
[16]:
<ansys.speos.core.source.SourceRayFile at 0x7f32aa8cfe80>
Surface source#
[17]:
source4 = p.create_source(name="Surface.1", feature_type=SourceSurface)
source4.set_exitance_constant(
geometries=[
(GeoRef.from_native_link("TheBodyB/TheFaceF"), False),
(GeoRef.from_native_link("TheBodyB/TheFaceG"), True),
]
)
source4.commit()
print(source4)
{
"name": "Surface.1",
"metadata": {
"UniqueId": "ad35b066-5b82-41e0-9cb1-590573a42e33"
},
"source_guid": "5ef47954-1f93-474f-9c3e-c70a488e8781",
"description": "",
"source": {
"name": "Surface.1",
"surface": {
"luminous_flux": {
"luminous_value": 683.0
},
"intensity_guid": "9f9d4fd2-e3e7-467e-9015-65dd971250e8",
"exitance_constant": {
"geo_paths": [
{
"geo_path": "TheBodyB/TheFaceF",
"reverse_normal": false
},
{
"geo_path": "TheBodyB/TheFaceG",
"reverse_normal": true
}
]
},
"spectrum_guid": "329ba2d3-8711-44af-b939-6d2e1219b4f3",
"intensity": {
"name": "Surface.1.Intensity",
"cos": {
"N": 1.0,
"total_angle": 180.0
},
"description": "",
"metadata": {}
},
"spectrum": {
"name": "Surface.1.Spectrum",
"monochromatic": {
"wavelength": 555.0
},
"description": "",
"metadata": {}
}
},
"description": "",
"metadata": {}
}
}
[18]:
source4.set_flux_luminous_intensity()
source4.set_intensity().set_gaussian().set_axis_system(
axis_system=[10, 50, 20, 1, 0, 0, 0, 1, 0, 0, 0, 1]
)
source4.commit()
print(source4)
{
"name": "Surface.1",
"metadata": {
"UniqueId": "ad35b066-5b82-41e0-9cb1-590573a42e33"
},
"source_guid": "5ef47954-1f93-474f-9c3e-c70a488e8781",
"description": "",
"source": {
"name": "Surface.1",
"surface": {
"luminous_intensity_flux": {
"luminous_intensity_value": 5.0
},
"intensity_guid": "9f9d4fd2-e3e7-467e-9015-65dd971250e8",
"exitance_constant": {
"geo_paths": [
{
"geo_path": "TheBodyB/TheFaceF",
"reverse_normal": false
},
{
"geo_path": "TheBodyB/TheFaceG",
"reverse_normal": true
}
]
},
"spectrum_guid": "329ba2d3-8711-44af-b939-6d2e1219b4f3",
"intensity": {
"name": "Surface.1.Intensity",
"gaussian": {
"FWHM_angle_x": 30.0,
"FWHM_angle_y": 30.0,
"total_angle": 180.0,
"axis_system": [
10.0,
50.0,
20.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
]
},
"description": "",
"metadata": {}
},
"spectrum": {
"name": "Surface.1.Spectrum",
"monochromatic": {
"wavelength": 555.0
},
"description": "",
"metadata": {}
}
},
"description": "",
"metadata": {}
}
}
[19]:
source4.delete()
print(source4)
local: {
"name": "Surface.1",
"description": "",
"metadata": {},
"source_guid": "",
"source": {
"name": "Surface.1",
"surface": {
"luminous_intensity_flux": {
"luminous_intensity_value": 5.0
},
"intensity_guid": "9f9d4fd2-e3e7-467e-9015-65dd971250e8",
"exitance_constant": {
"geo_paths": [
{
"geo_path": "TheBodyB/TheFaceF",
"reverse_normal": false
},
{
"geo_path": "TheBodyB/TheFaceG",
"reverse_normal": true
}
]
},
"spectrum_guid": "329ba2d3-8711-44af-b939-6d2e1219b4f3",
"intensity": {
"name": "Surface.1.Intensity",
"gaussian": {
"FWHM_angle_x": 30.0,
"FWHM_angle_y": 30.0,
"total_angle": 180.0,
"axis_system": [
10.0,
50.0,
20.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0
]
},
"description": "",
"metadata": {}
},
"spectrum": {
"name": "Surface.1.Spectrum",
"monochromatic": {
"wavelength": 555.0
},
"description": "",
"metadata": {}
}
},
"description": "",
"metadata": {}
}
}
When creating sources, this creates some intermediate objects (spectrums, intensity templates).
Deleting a source does not delete in cascade those objects because they could be used by some other entities from core layer.
Then at the end of the example, we just clean all databases
[20]:
for item in speos.client.intensity_templates().list() + speos.client.spectrums().list():
item.delete()