How to create a simulation#

This tutorial demonstrates how to create a simulation.

What is a simulation?#

A simulation contains selected sensors, sources to model ray-trace in space.

[1]:
from pathlib import Path

from ansys.speos.core import GeoRef, Project, Speos
from ansys.speos.core.simulation import SimulationInteractive, SimulationInverse

# 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)

Create Project#

Create a new project first.

The only way to create a simulation is to create it from a project.

[3]:
p = Project(speos=speos)
print(p)
{
    "name": "",
    "description": "",
    "metadata": {},
    "part_guid": "",
    "sources": [],
    "sensors": [],
    "simulations": [],
    "materials": [],
    "scenes": []
}

Prepare prerequisites#

Create the necessary elements for a simulation: Sensor, source, root part, optical property are prerequisites.

Prepare the root part#

[4]:
root_part = p.create_root_part()
root_part.create_body(name="Body.1").create_face(name="Face.1").set_vertices(
    [0, 1, 2, 0, 2, 2, 1, 2, 2]
).set_facets([0, 1, 2]).set_normals([0, 0, 1, 0, 0, 1, 0, 0, 1])
root_part.commit()
[4]:
<ansys.speos.core.part.Part at 0x7f49c89e16f0>

Prepare an optical property#

[5]:
opt_prop = p.create_optical_property("Material.1")
opt_prop.set_volume_opaque().set_surface_mirror()  # vop as opaque and sop as mirror
# Choose the geometry for this optical property : Body.1
opt_prop.set_geometries(geometries=[GeoRef.from_native_link(geopath="Body.1")])
opt_prop.commit()
[5]:
<ansys.speos.core.opt_prop.OptProp at 0x7f49c89e1690>

Prepare an irradiance sensor#

[6]:
sensor1 = p.create_sensor(name="Irradiance.1")
# colorimetric or spectral so that the sensor can be used both in direct and inverse simulation
sensor1.set_type_colorimetric()
sensor1.commit()
[6]:
<ansys.speos.core.sensor.SensorIrradiance at 0x7f49c89e0910>

Prepare a surface source#

[7]:
source1 = p.create_source(name="Surface.1")
source1.set_exitance_constant(geometries=[(GeoRef.from_native_link(geopath="Body.1/Face.1"), True)])
# blackbody so that the source can be used both in direct and inverse simulation
source1.set_spectrum().set_blackbody()
source1.commit()
[7]:
<ansys.speos.core.source.SourceSurface at 0x7f49c89e20b0>

Create a simulation#

[8]:
simulation1 = p.create_simulation(name="Simulation.1")
simulation1.set_sensor_paths(["Irradiance.1"]).set_source_paths(["Surface.1"])
print(simulation1)

simulation1.commit()
print(simulation1)
local: {
    "name": "Simulation.1",
    "sensor_paths": [
        "Irradiance.1"
    ],
    "source_paths": [
        "Surface.1"
    ],
    "description": "",
    "metadata": {},
    "simulation_guid": "",
    "simulation": {
        "direct_mc_simulation_template": {
            "geom_distance_tolerance": 0.01,
            "max_impact": 100,
            "weight": {
                "minimum_energy_percentage": 0.005
            },
            "dispersion": true,
            "colorimetric_standard": "CIE_1931",
            "fast_transmission_gathering": false,
            "ambient_material_uri": "",
            "stop_condition_rays_number": "200000",
            "automatic_save_frequency": 1800
        },
        "name": "Simulation.1",
        "description": "",
        "metadata": {},
        "scene_guid": "ed851ee2-f072-4caf-9955-725427eafe9a",
        "simulation_path": "Simulation.1",
        "job_type": "CPU"
    }
}
{
    "name": "Simulation.1",
    "metadata": {
        "UniqueId": "820b11aa-5054-4496-9f57-c0f4519b30ed"
    },
    "simulation_guid": "218bbb39-8a89-4b4f-adc6-c40917750a5f",
    "sensor_paths": [
        "Irradiance.1"
    ],
    "source_paths": [
        "Surface.1"
    ],
    "description": "",
    "simulation": {
        "direct_mc_simulation_template": {
            "geom_distance_tolerance": 0.01,
            "max_impact": 100,
            "weight": {
                "minimum_energy_percentage": 0.005
            },
            "dispersion": true,
            "colorimetric_standard": "CIE_1931",
            "fast_transmission_gathering": false,
            "ambient_material_uri": "",
            "stop_condition_rays_number": "200000",
            "automatic_save_frequency": 1800
        },
        "name": "Simulation.1",
        "description": "",
        "metadata": {},
        "scene_guid": "ed851ee2-f072-4caf-9955-725427eafe9a",
        "simulation_path": "Simulation.1",
        "job_type": "CPU"
    }
}

Set simulation characteristics#

Simulation is defined with the same default values as the GUI speos.

If the user would like to modify the simulation characteristics, it is possible to do so by setting the simulation characteristics as below.

[9]:
simulation2_direct = p.create_simulation(name="Simulation.2")

simulation2_direct.set_ambient_material_file_uri(
    uri=str(assets_data_path / "AIR.material")
).set_colorimetric_standard_CIE_1964().set_weight_none().set_geom_distance_tolerance(
    0.01
).set_max_impact(200).set_dispersion(False)
simulation2_direct.set_sensor_paths(["Irradiance.1"]).set_source_paths(["Surface.1"]).commit()
print(simulation2_direct)
{
    "name": "Simulation.2",
    "metadata": {
        "UniqueId": "4554654d-a004-4189-959d-c436d3d89436"
    },
    "simulation_guid": "10613737-bf4e-4209-9b0e-f4dd1cd67939",
    "sensor_paths": [
        "Irradiance.1"
    ],
    "source_paths": [
        "Surface.1"
    ],
    "description": "",
    "simulation": {
        "direct_mc_simulation_template": {
            "geom_distance_tolerance": 0.01,
            "max_impact": 200,
            "colorimetric_standard": "CIE_1964",
            "ambient_material_uri": "/app/assets/AIR.material",
            "dispersion": false,
            "fast_transmission_gathering": false,
            "stop_condition_rays_number": "200000",
            "automatic_save_frequency": 1800
        },
        "name": "Simulation.2",
        "description": "",
        "metadata": {},
        "scene_guid": "ed851ee2-f072-4caf-9955-725427eafe9a",
        "simulation_path": "Simulation.2",
        "job_type": "CPU"
    }
}

Read information#

Read simulation information

[10]:
print(simulation1)
{
    "name": "Simulation.1",
    "metadata": {
        "UniqueId": "820b11aa-5054-4496-9f57-c0f4519b30ed"
    },
    "simulation_guid": "218bbb39-8a89-4b4f-adc6-c40917750a5f",
    "sensor_paths": [
        "Irradiance.1"
    ],
    "source_paths": [
        "Surface.1"
    ],
    "description": "",
    "simulation": {
        "direct_mc_simulation_template": {
            "geom_distance_tolerance": 0.01,
            "max_impact": 100,
            "weight": {
                "minimum_energy_percentage": 0.005
            },
            "dispersion": true,
            "colorimetric_standard": "CIE_1931",
            "fast_transmission_gathering": false,
            "ambient_material_uri": "",
            "stop_condition_rays_number": "200000",
            "automatic_save_frequency": 1800
        },
        "name": "Simulation.1",
        "description": "",
        "metadata": {},
        "scene_guid": "ed851ee2-f072-4caf-9955-725427eafe9a",
        "simulation_path": "Simulation.1",
        "job_type": "CPU"
    }
}

Read project information

[11]:
print(p)
{
    "part_guid": "67e8d2d7-e9b7-4fb7-aa3d-a5851277910a",
    "sources": [
        {
            "name": "Surface.1",
            "metadata": {
                "UniqueId": "f939b0a5-a8c6-49fe-9976-bf11e44a7a97"
            },
            "source_guid": "b014bdbd-a578-4ce4-990b-f0ebb524cf34",
            "description": "",
            "source": {
                "name": "Surface.1",
                "surface": {
                    "luminous_flux": {
                        "luminous_value": 683.0
                    },
                    "intensity_guid": "262e2ee8-51fb-41dc-9bf0-3c95f023ef42",
                    "exitance_constant": {
                        "geo_paths": [
                            {
                                "geo_path": "Body.1/Face.1",
                                "reverse_normal": true
                            }
                        ]
                    },
                    "spectrum_guid": "ad681ee6-a9a3-4dc6-a268-600bce88daf7",
                    "intensity": {
                        "name": "Surface.1.Intensity",
                        "cos": {
                            "N": 1.0,
                            "total_angle": 180.0
                        },
                        "description": "",
                        "metadata": {}
                    },
                    "spectrum": {
                        "name": "Surface.1.Spectrum",
                        "blackbody": {
                            "temperature": 2856.0
                        },
                        "description": "",
                        "metadata": {}
                    }
                },
                "description": "",
                "metadata": {}
            }
        }
    ],
    "sensors": [
        {
            "name": "Irradiance.1",
            "metadata": {
                "UniqueId": "4e36a83b-77ba-4626-b267-ffa1665e3e74"
            },
            "sensor_guid": "1eeb5bdf-2310-4402-8563-e95535142b54",
            "description": "",
            "result_file_name": "",
            "sensor": {
                "irradiance_sensor_template": {
                    "sensor_type_colorimetric": {
                        "wavelengths_range": {
                            "w_start": 400.0,
                            "w_end": 700.0,
                            "w_sampling": 13
                        }
                    },
                    "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": "Irradiance.1",
                "description": "",
                "metadata": {}
            }
        }
    ],
    "simulations": [
        {
            "name": "Simulation.1",
            "metadata": {
                "UniqueId": "820b11aa-5054-4496-9f57-c0f4519b30ed"
            },
            "simulation_guid": "218bbb39-8a89-4b4f-adc6-c40917750a5f",
            "sensor_paths": [
                "Irradiance.1"
            ],
            "source_paths": [
                "Surface.1"
            ],
            "description": "",
            "simulation": {
                "direct_mc_simulation_template": {
                    "geom_distance_tolerance": 0.01,
                    "max_impact": 100,
                    "weight": {
                        "minimum_energy_percentage": 0.005
                    },
                    "dispersion": true,
                    "colorimetric_standard": "CIE_1931",
                    "fast_transmission_gathering": false,
                    "ambient_material_uri": "",
                    "stop_condition_rays_number": "200000",
                    "automatic_save_frequency": 1800
                },
                "name": "Simulation.1",
                "description": "",
                "metadata": {},
                "scene_guid": "ed851ee2-f072-4caf-9955-725427eafe9a",
                "simulation_path": "Simulation.1",
                "job_type": "CPU"
            }
        },
        {
            "name": "Simulation.2",
            "metadata": {
                "UniqueId": "4554654d-a004-4189-959d-c436d3d89436"
            },
            "simulation_guid": "10613737-bf4e-4209-9b0e-f4dd1cd67939",
            "sensor_paths": [
                "Irradiance.1"
            ],
            "source_paths": [
                "Surface.1"
            ],
            "description": "",
            "simulation": {
                "direct_mc_simulation_template": {
                    "geom_distance_tolerance": 0.01,
                    "max_impact": 200,
                    "colorimetric_standard": "CIE_1964",
                    "ambient_material_uri": "/app/assets/AIR.material",
                    "dispersion": false,
                    "fast_transmission_gathering": false,
                    "stop_condition_rays_number": "200000",
                    "automatic_save_frequency": 1800
                },
                "name": "Simulation.2",
                "description": "",
                "metadata": {},
                "scene_guid": "ed851ee2-f072-4caf-9955-725427eafe9a",
                "simulation_path": "Simulation.2",
                "job_type": "CPU"
            }
        }
    ],
    "materials": [
        {
            "name": "Material.1",
            "metadata": {
                "UniqueId": "8cee67e2-acba-4381-bf51-a48b2bde6f24"
            },
            "vop_guid": "f4291276-6b35-4e3d-b354-77e2de162a21",
            "sop_guids": [
                "3c578be0-2d49-4e79-87c0-74726d1a43b9"
            ],
            "geometries": {
                "geo_paths": [
                    "Body.1"
                ]
            },
            "description": "",
            "vop": {
                "name": "Material.1.VOP",
                "opaque": {},
                "description": "",
                "metadata": {}
            },
            "sops": [
                {
                    "name": "Material.1.SOP",
                    "mirror": {
                        "reflectance": 100.0
                    },
                    "description": "",
                    "metadata": {}
                }
            ]
        }
    ],
    "name": "",
    "description": "",
    "metadata": {},
    "scenes": []
}

Update simulation settings#

If you are manipulating a simulation already committed, remember to commit your changes.

If you don’t, you will still only watch what is committed on the server.

[12]:
simulation1.set_ambient_material_file_uri(uri=str(assets_data_path / "AIR.material"))
simulation1.commit()
print(simulation1)
{
    "name": "Simulation.1",
    "metadata": {
        "UniqueId": "820b11aa-5054-4496-9f57-c0f4519b30ed"
    },
    "simulation_guid": "218bbb39-8a89-4b4f-adc6-c40917750a5f",
    "sensor_paths": [
        "Irradiance.1"
    ],
    "source_paths": [
        "Surface.1"
    ],
    "description": "",
    "simulation": {
        "direct_mc_simulation_template": {
            "geom_distance_tolerance": 0.01,
            "max_impact": 100,
            "weight": {
                "minimum_energy_percentage": 0.005
            },
            "dispersion": true,
            "ambient_material_uri": "/app/assets/AIR.material",
            "colorimetric_standard": "CIE_1931",
            "fast_transmission_gathering": false,
            "stop_condition_rays_number": "200000",
            "automatic_save_frequency": 1800
        },
        "name": "Simulation.1",
        "description": "",
        "metadata": {},
        "scene_guid": "ed851ee2-f072-4caf-9955-725427eafe9a",
        "simulation_path": "Simulation.1",
        "job_type": "CPU"
    }
}

Reset#

Possibility to reset local values from the one available in the server.

[13]:
simulation1.set_max_impact(1000)  # adjust max impact but no commit
simulation1.reset()  # reset -> this will apply the server value to the local value
simulation1.delete()  # delete (to display the local value with the below print)
print(simulation1)
local: {
    "name": "Simulation.1",
    "sensor_paths": [
        "Irradiance.1"
    ],
    "source_paths": [
        "Surface.1"
    ],
    "description": "",
    "metadata": {},
    "simulation_guid": "",
    "simulation": {
        "direct_mc_simulation_template": {
            "geom_distance_tolerance": 0.01,
            "max_impact": 100,
            "weight": {
                "minimum_energy_percentage": 0.005
            },
            "dispersion": true,
            "ambient_material_uri": "/app/assets/AIR.material",
            "colorimetric_standard": "CIE_1931",
            "fast_transmission_gathering": false,
            "stop_condition_rays_number": "200000",
            "automatic_save_frequency": 1800
        },
        "name": "Simulation.1",
        "description": "",
        "metadata": {},
        "scene_guid": "ed851ee2-f072-4caf-9955-725427eafe9a",
        "simulation_path": "Simulation.1",
        "job_type": "CPU"
    }
}

Other simulation examples#

Inverse simulation#

[14]:
simulation3 = p.create_simulation(name="Simulation.3", feature_type=SimulationInverse)
simulation3.set_sensor_paths(sensor_paths=["Irradiance.1"]).set_source_paths(
    source_paths=["Surface.1"]
).commit()
print(simulation3)
{
    "name": "Simulation.3",
    "metadata": {
        "UniqueId": "305710e8-4f74-40b5-8210-dc38a1772011"
    },
    "simulation_guid": "76c47825-7593-4d41-bd47-8c2cb9df8742",
    "sensor_paths": [
        "Irradiance.1"
    ],
    "source_paths": [
        "Surface.1"
    ],
    "description": "",
    "simulation": {
        "inverse_mc_simulation_template": {
            "geom_distance_tolerance": 0.01,
            "max_impact": 100,
            "weight": {
                "minimum_energy_percentage": 0.005
            },
            "number_of_gathering_rays_per_source": 1,
            "colorimetric_standard": "CIE_1931",
            "dispersion": false,
            "splitting": false,
            "maximum_gathering_error": 0,
            "maximum_gathering_error_percentage": 0.0,
            "fast_transmission_gathering": false,
            "ambient_material_uri": "",
            "optimized_propagation_none": {
                "stop_condition_passes_number": 5
            },
            "automatic_save_frequency": 1800
        },
        "name": "Simulation.3",
        "description": "",
        "metadata": {},
        "scene_guid": "ed851ee2-f072-4caf-9955-725427eafe9a",
        "simulation_path": "Simulation.3",
        "job_type": "CPU"
    }
}

Interactive simulation#

[15]:
simulation4 = p.create_simulation(name="Simulation.4", feature_type=SimulationInteractive)
simulation4.set_source_paths(source_paths=["Surface.1"]).commit()
print(simulation4)
{
    "name": "Simulation.4",
    "metadata": {
        "UniqueId": "774bdbce-419d-4bfe-8d29-a4c0872af503"
    },
    "simulation_guid": "b5d95846-0d9f-4adc-b0a2-b7bb9fdb6312",
    "source_paths": [
        "Surface.1"
    ],
    "description": "",
    "sensor_paths": [],
    "simulation": {
        "name": "Simulation.4",
        "interactive_simulation_template": {
            "geom_distance_tolerance": 0.01,
            "max_impact": 100,
            "weight": {
                "minimum_energy_percentage": 0.005
            },
            "colorimetric_standard": "CIE_1931",
            "ambient_material_uri": "",
            "rays_number_per_sources": [],
            "light_expert": false,
            "impact_report": false
        },
        "description": "",
        "metadata": {},
        "scene_guid": "ed851ee2-f072-4caf-9955-725427eafe9a",
        "simulation_path": "Simulation.4",
        "job_type": "CPU"
    }
}