How to use scene and job#
This tutorial demonstrates how to create a scene, and fill it from a speos file. Then this demonstrates how to create a job from the scene, and run it.
[1]:
from pathlib import Path
import time
from ansys.speos.core.kernel.job import ProtoJob
from ansys.speos.core.speos import Speos
# 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)
Scene#
Create an empty scene
[3]:
my_scene = speos.client.scenes().create()
Load a file to fill the scene
[4]:
speos_file = str(
assets_data_path / "LG_50M_Colorimetric_short.sv5" / "LG_50M_Colorimetric_short.sv5"
)
my_scene.load_file(file_uri=speos_file)
Print scene data model
Here it is possible to see that the scene contains two surface sources, one irradiance sensor.
[5]:
print(my_scene)
ansys.api.speos.scene.v2.Scene
{
"name": "LG_50M_Colorimetric_short",
"description": "From /app/assets/LG_50M_Colorimetric_short.sv5/LG_50M_Colorimetric_short.sv5",
"part_guid": "bfca8264-98e0-4921-b595-1b304bed1e97",
"sources": [
{
"name": "Dom Source 2 (0) in SOURCE2",
"source_guid": "0b149248-bcb9-4924-be85-a09adc3f8715",
"surface_properties": {
"exitance_constant_properties": {
"geo_paths": [
{
"geo_path": "Solid Body in SOURCE2:2920204960/Face in SOURCE2:222",
"reverse_normal": false
}
]
}
},
"description": "",
"metadata": {}
},
{
"name": "Surface Source (0) in SOURCE1",
"source_guid": "03290871-32a4-4231-8735-1a564d66fed6",
"surface_properties": {
"exitance_constant_properties": {
"geo_paths": [
{
"geo_path": "Solid Body in SOURCE1:2494956811/Face in SOURCE1:187",
"reverse_normal": false
}
]
}
},
"description": "",
"metadata": {}
}
],
"sensors": [
{
"name": "Dom Irradiance Sensor (0)",
"sensor_guid": "d959ee84-2921-49c9-98e5-3907d5bc0405",
"result_file_name": "ASSEMBLY1.DS (0).Dom Irradiance Sensor (0)",
"irradiance_properties": {
"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"
},
"description": "",
"metadata": {}
}
],
"simulations": [
{
"name": "ASSEMBLY1.DS (0)",
"simulation_guid": "ed0b2017-082b-455a-854b-eeb0250d3027",
"sensor_paths": [
"Dom Irradiance Sensor (0)"
],
"source_paths": [
"Dom Source 2 (0) in SOURCE2",
"Surface Source (0) in SOURCE1"
],
"description": "",
"metadata": {}
}
],
"materials": [
{
"name": "Material.1",
"sop_guids": [
"f78f7196-a557-474b-a769-485e9d696130"
],
"geometries": {
"geo_paths": [
"Solid Body in GUIDE:1379760262/Face in GUIDE:169"
]
},
"description": "",
"metadata": {}
},
{
"name": "Material.2",
"vop_guid": "1878f4ce-567b-40b9-a153-d8eea40db5d4",
"sop_guids": [
"caaa9c64-a25d-444f-84c2-a86314c70d63"
],
"geometries": {
"geo_paths": [
"Solid Body in GUIDE:1379760262"
]
},
"description": "",
"metadata": {}
},
{
"name": "Material.3",
"vop_guid": "88f460f8-f56c-45f5-ba7f-9aed6c9f57be",
"sop_guids": [
"f78f7196-a557-474b-a769-485e9d696130"
],
"geometries": {
"geo_paths": [
"Solid Body in SOURCE2:2920204960",
"Solid Body in SOURCE1:2494956811"
]
},
"description": "",
"metadata": {}
},
{
"name": "Material.4",
"vop_guid": "1e9f7074-f93a-4ae9-a8b9-a02687e28f48",
"description": "",
"metadata": {},
"sop_guids": []
}
],
"metadata": {},
"scenes": []
}
Job#
Create a job for the first simulation. When loaded from a speos file, there is always only one simulation in the scene.
[6]:
# First create the protobuf message
job_message = ProtoJob(name="my_job")
job_message.scene_guid = my_scene.key # The job needs a scene guid
job_message.simulation_path = (
my_scene.get().simulations[0].name
) # And needs to know which simulation in the scene is involved.
job_message.job_type = ProtoJob.Type.CPU # Choose type of job, can also be GPU.
job_message.direct_mc_simulation_properties.automatic_save_frequency = 1800
job_message.direct_mc_simulation_properties.stop_condition_rays_number = (
200000 # Stop condition, here 200000 rays will be sent.
)
# Create the JobLink
job_link = speos.client.jobs().create(message=job_message)
Start the job
[7]:
job_link.start()
Verify state of the job
[8]:
job_link.get_state()
[8]:
state: RUNNING
Wait that the job is finished
[9]:
job_state_res = job_link.get_state()
while (
job_state_res.state != ProtoJob.State.FINISHED
and job_state_res.state != ProtoJob.State.STOPPED
and job_state_res.state != ProtoJob.State.IN_ERROR
):
time.sleep(2)
job_state_res = job_link.get_state()
Retrieve results of the job
Two results are generated : the result of irradiance sensor: “ASSEMBLY1.DS (0).Dom Irradiance Sensor (0).xmp” and the simulation report in html
[10]:
results = job_link.get_results().results
print(results)
[upload_response {
info {
uri: "c3228df2-6034-4cba-be33-7ae953e4b131"
file_name: "ASSEMBLY1.DS (0).Dom Irradiance Sensor (0).xmp"
file_size: 1435848
}
upload_duration {
nanos: 2175309
}
}
, upload_response {
info {
uri: "dbc1a583-7733-45eb-9c2a-14900281e4af"
file_name: "ASSEMBLY1.DS (0).html"
file_size: 506162
}
upload_duration {
nanos: 772721
}
}
]
Once no more needed: delete the job
[11]:
job_link.delete()
When loading a speos file into a scene, this creates many objects (source templates, sensor templates, vop template, sop templates). Then at the end of the example, we just clean all databases
[12]:
for item in (
speos.client.scenes().list()
+ speos.client.simulation_templates().list()
+ speos.client.sensor_templates().list()
+ speos.client.source_templates().list()
+ speos.client.intensity_templates().list()
+ speos.client.spectrums().list()
+ speos.client.vop_templates().list()
+ speos.client.sop_templates().list()
+ speos.client.parts().list()
+ speos.client.bodies().list()
+ speos.client.faces().list()
):
item.delete()