Prism example#

This tutorial demonstrates how to run a prism use case.

[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

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

Load a project from .speos file.

[3]:
p = Project(speos=speos, path=str(assets_data_path / "Prism.speos" / "Prism.speos"))
print(p)
{
    "name": "Prism",
    "description": "From /app/assets/Prism.speos/Prism.speos",
    "part_guid": "1df1c2ff-e897-4855-b13b-62df975e2d34",
    "sources": [
        {
            "name": "Surface.1:7758",
            "metadata": {
                "UniqueId": "39f303f0-49d1-4024-9902-df5012cc6415"
            },
            "source_guid": "59915513-8e62-493e-a867-4562e3a02c3a",
            "description": "",
            "source": {
                "name": "Surface.1:7758",
                "surface": {
                    "radiant_flux": {
                        "radiant_value": 8.346636625511586
                    },
                    "intensity_guid": "a93f1c14-0820-45b4-860e-568cf76697bd",
                    "exitance_constant": {
                        "geo_paths": [
                            {
                                "geo_path": "SourceBody:1801986974/face.1:544289899",
                                "reverse_normal": false
                            }
                        ]
                    },
                    "spectrum_guid": "20323fe8-7b1a-47a6-90e7-3bfb747d9769",
                    "intensity": {
                        "cos": {
                            "N": 1.0,
                            "total_angle": 0.1
                        },
                        "name": "",
                        "description": "",
                        "metadata": {}
                    },
                    "spectrum": {
                        "blackbody": {
                            "temperature": 5000.0
                        },
                        "name": "",
                        "description": "",
                        "metadata": {}
                    }
                },
                "description": "",
                "metadata": {}
            }
        }
    ],
    "sensors": [
        {
            "name": "Irradiance.1:564",
            "metadata": {
                "UniqueId": "8f28470e-d627-48c3-9aed-bfc84f534dfd"
            },
            "sensor_guid": "f4825772-85cb-4360-96f2-74d7b51e23d5",
            "result_file_name": "Prism.Irradiance.1",
            "description": "",
            "sensor": {
                "irradiance_sensor_template": {
                    "sensor_type_spectral": {
                        "wavelengths_range": {
                            "w_start": 380.0,
                            "w_end": 780.0,
                            "w_sampling": 41
                        }
                    },
                    "illuminance_type_planar": {},
                    "dimensions": {
                        "x_start": -20.0,
                        "x_end": 20.0,
                        "x_sampling": 400,
                        "y_start": -5.0,
                        "y_end": 5.0,
                        "y_sampling": 100
                    },
                    "axis_system": [
                        921.3551052231927,
                        0.0,
                        388.72197015239516,
                        -0.3887219701523952,
                        -0.0,
                        0.9213551052231927,
                        0.0,
                        1.0000000000000002,
                        0.0,
                        -0.9213551052231927,
                        0.0,
                        -0.3887219701523952
                    ],
                    "layer_type_none": {},
                    "ray_file_type": "RayFileNone",
                    "integration_direction": []
                },
                "name": "Irradiance.1:564",
                "description": "",
                "metadata": {}
            }
        }
    ],
    "simulations": [
        {
            "name": "Prism",
            "metadata": {
                "UniqueId": "a93bab36-5141-4108-8fc5-d92e4a72c807"
            },
            "simulation_guid": "67e9cdae-ad19-4498-a0eb-9eeb34eb975b",
            "sensor_paths": [
                "Irradiance.1:564"
            ],
            "source_paths": [
                "Surface.1:7758"
            ],
            "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": "Prism",
                "metadata": {},
                "description": "",
                "scene_guid": "5b3ca5b3-b9fd-45cd-886f-433a84234650",
                "simulation_path": "Prism",
                "job_type": "CPU"
            }
        }
    ],
    "materials": [
        {
            "name": "Material.1",
            "metadata": {
                "UniqueId": "efc6b78a-613e-4154-8ce9-21abb05740dc"
            },
            "sop_guids": [
                "4e7c3223-0180-40de-ad67-28d1d9741b5e"
            ],
            "geometries": {
                "geo_paths": [
                    "SourceBody:1801986974/face.1:544289899",
                    "SourceBody:1801986974/face.1:2282175023",
                    "SourceBody:1801986974/face.1:2503270562",
                    "SourceBody:1801986974/face.1:2272456582",
                    "SourceBody:1801986974/face.1:1596341914",
                    "SourceBody:1801986974/face.1:387288686"
                ]
            },
            "description": "",
            "sops": [
                {
                    "mirror": {
                        "reflectance": 0.0
                    },
                    "name": "",
                    "description": "",
                    "metadata": {}
                }
            ]
        },
        {
            "name": "Material.2",
            "metadata": {
                "UniqueId": "768a4b9e-fe84-4ab1-8cda-f1b358234d11"
            },
            "vop_guid": "a6e3cbdf-c6e8-488d-a83d-a450abd57457",
            "sop_guids": [
                "ee994285-7fb2-4c99-b1e0-0eb7f78d27bd"
            ],
            "geometries": {
                "geo_paths": [
                    "PrismBody:1130610277"
                ]
            },
            "description": "",
            "vop": {
                "optic": {
                    "index": 1.5,
                    "absorption": 0.001,
                    "constringence": 60.0
                },
                "name": "",
                "description": "",
                "metadata": {}
            },
            "sops": [
                {
                    "optical_polished": {},
                    "name": "",
                    "description": "",
                    "metadata": {}
                }
            ]
        },
        {
            "name": "Material.3",
            "metadata": {
                "UniqueId": "3aa28c01-7969-4b33-82c5-ff0904ce3ed6"
            },
            "sop_guids": [
                "d390b9d6-96a1-4770-af2d-b4f72cf61877"
            ],
            "geometries": {
                "geo_paths": [
                    "PrismBody:1130610277/face.1:242396665"
                ]
            },
            "description": "",
            "sops": [
                {
                    "library": {
                        "sop_file_uri": "/app/assets/Prism.speos/transparent_ad06-07cb-a92e-7815..scattering"
                    },
                    "name": "",
                    "description": "",
                    "metadata": {}
                }
            ]
        },
        {
            "name": "Material.4",
            "metadata": {
                "UniqueId": "21b4acb9-e929-48a6-9aa8-477916857be9"
            },
            "vop_guid": "78750607-f011-44e2-80e1-7bd8263c8467",
            "description": "",
            "sop_guids": [],
            "vop": {
                "optic": {
                    "index": 1.0,
                    "absorption": 0.0
                },
                "name": "",
                "description": "",
                "metadata": {}
            }
        }
    ],
    "metadata": {},
    "scenes": []
}

Preview.#

[4]:
p.preview()

Retrieve the simulation feature and open result#

Run the simulation

[5]:
sim_features = p.find(name="Prism", feature_type=SimulationDirect)
sim = sim_features[0]
sim.compute_CPU()
[5]:
[upload_response {
  info {
    uri: "2e8124cb-70d2-46a9-905e-68fb8a7a837b"
    file_name: "Prism.Irradiance.1.xmp"
    file_size: 1568125
  }
  upload_duration {
    nanos: 3388880
  }
}
, upload_response {
  info {
    uri: "64a3e1c9-f0ab-4253-8f68-c03f62ac7886"
    file_name: "Prism.html"
    file_size: 65780
  }
  upload_duration {
    nanos: 280182
  }
}
]

Use the open_result_image method to review the result

[6]:
if os.name == "nt":
    from ansys.speos.core.workflow.open_result import open_result_image

    open_result_image(simulation_feature=sim, result_name="Prism.Irradiance.1.xmp")

Work with sensor#

Retrieve the sensor feature.

Modify the sensor setting, e.g. set the spectral type, etc.

[7]:
irr_features = p.find(name=".*", name_regex=True, feature_type=SensorIrradiance)
irr = irr_features[0]
irr.set_type_spectral().set_wavelengths_range().set_start(500).set_end(600).set_sampling(11)
irr.commit()
[7]:
<ansys.speos.core.sensor.SensorIrradiance at 0x7f3c690805e0>

Re-run the simulation with new sensor definition#

[8]:
sim.compute_CPU()
if os.name == "nt":
    open_result_image(simulation_feature=sim, result_name="Prism.Irradiance.1.xmp")