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.

[1]:
from pathlib import Path

from ansys.speos.core import GeoRef, Project, Speos
from ansys.speos.core.source import (
    SourceLuminaire,
    SourceRayFile,
    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)

Create a new project#

The only way to create a source, is to create it from a project.

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

Create#

Create locally

The mention “local: “ is added when printing the source.

[4]:
intensity_file_path = str(assets_data_path / "IES_C_DETECTOR.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.#

Now that it is committed to the server, the mention “local: “ is no more present when printing the source.

[5]:
source1.commit()
print(source1)
{
    "name": "Luminaire.1",
    "metadata": {
        "UniqueId": "f9ed5c94-6a51-4ca5-97bf-adb343dfbf45"
    },
    "source_guid": "c9169d01-b6ef-4fed-878e-4d402019a943",
    "description": "",
    "source": {
        "name": "Luminaire.1",
        "luminaire": {
            "flux_from_intensity_file": {},
            "intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
            "spectrum_guid": "3abe8a6f-5fa3-4421-86d2-bf5971f4e04b",
            "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": {}
    }
}

Another example#

Setting several more characteristics.

[6]:
intensity_file_path = str(assets_data_path / "IES_C_DETECTOR.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 blacbody with default value for the source spectrum
source2.commit()  # Push to the server
print(source2)
{
    "name": "Luminaire.2",
    "metadata": {
        "UniqueId": "9ea97b59-3ee7-4a55-a8f6-80aafc6b424c"
    },
    "source_guid": "87794efd-19bf-4361-b6ce-401acfe48247",
    "description": "",
    "source": {
        "name": "Luminaire.2",
        "luminaire": {
            "radiant_flux": {
                "radiant_value": 1.0
            },
            "intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
            "spectrum_guid": "cf95407f-bf7d-45a4-bee4-f40b3455e4d9",
            "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": {}
    }
}

Read#

Source Instance#

As mention “local: “ is added if it is not yet committed to the server.

[7]:
print(source1)
{
    "name": "Luminaire.1",
    "metadata": {
        "UniqueId": "f9ed5c94-6a51-4ca5-97bf-adb343dfbf45"
    },
    "source_guid": "c9169d01-b6ef-4fed-878e-4d402019a943",
    "description": "",
    "source": {
        "name": "Luminaire.1",
        "luminaire": {
            "flux_from_intensity_file": {},
            "intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
            "spectrum_guid": "3abe8a6f-5fa3-4421-86d2-bf5971f4e04b",
            "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.

[8]:
print(p)
{
    "sources": [
        {
            "name": "Luminaire.1",
            "metadata": {
                "UniqueId": "f9ed5c94-6a51-4ca5-97bf-adb343dfbf45"
            },
            "source_guid": "c9169d01-b6ef-4fed-878e-4d402019a943",
            "description": "",
            "source": {
                "name": "Luminaire.1",
                "luminaire": {
                    "flux_from_intensity_file": {},
                    "intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
                    "spectrum_guid": "3abe8a6f-5fa3-4421-86d2-bf5971f4e04b",
                    "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": "9ea97b59-3ee7-4a55-a8f6-80aafc6b424c"
            },
            "source_guid": "87794efd-19bf-4361-b6ce-401acfe48247",
            "description": "",
            "source": {
                "name": "Luminaire.2",
                "luminaire": {
                    "radiant_flux": {
                        "radiant_value": 1.0
                    },
                    "intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
                    "spectrum_guid": "cf95407f-bf7d-45a4-bee4-f40b3455e4d9",
                    "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#

Tipp: 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.

[9]:
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": "f9ed5c94-6a51-4ca5-97bf-adb343dfbf45"
    },
    "source_guid": "c9169d01-b6ef-4fed-878e-4d402019a943",
    "description": "",
    "source": {
        "name": "Luminaire.1",
        "luminaire": {
            "radiant_flux": {
                "radiant_value": 1.2
            },
            "intensity_file_uri": "/app/assets/IES_C_DETECTOR.ies",
            "spectrum_guid": "3abe8a6f-5fa3-4421-86d2-bf5971f4e04b",
            "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.

[10]:
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": "3abe8a6f-5fa3-4421-86d2-bf5971f4e04b",
            "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.

[11]:
source2.delete()
print(source2)
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": "cf95407f-bf7d-45a4-bee4-f40b3455e4d9",
            "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": {}
    }
}
[12]:
source1.delete()
[12]:
<ansys.speos.core.source.SourceLuminaire at 0x7f4e9dd5f6a0>

Other Sources Examples#

ray-file source#

[13]:
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": "6b24dac9-5b08-45de-81f4-bcca73d2359a"
    },
    "source_guid": "80b3fd78-0227-4ad0-a581-0531ef633498",
    "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": {}
    }
}
[14]:
source3.set_flux_luminous()
source3.commit()
print(source3)
{
    "name": "Ray-file.1",
    "metadata": {
        "UniqueId": "6b24dac9-5b08-45de-81f4-bcca73d2359a"
    },
    "source_guid": "80b3fd78-0227-4ad0-a581-0531ef633498",
    "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": {}
    }
}
[15]:
source3.delete()
[15]:
<ansys.speos.core.source.SourceRayFile at 0x7f4e9dd9db70>

surface source#

[16]:
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": "db8526b8-fbde-41ff-a1c7-61735a3e297d"
    },
    "source_guid": "3c7a9e2f-07be-45a1-a593-9fcbc5801da4",
    "description": "",
    "source": {
        "name": "Surface.1",
        "surface": {
            "luminous_flux": {
                "luminous_value": 683.0
            },
            "intensity_guid": "b43ad23f-d4f7-40b3-a785-bd4e8deb6852",
            "exitance_constant": {
                "geo_paths": [
                    {
                        "geo_path": "TheBodyB/TheFaceF",
                        "reverse_normal": false
                    },
                    {
                        "geo_path": "TheBodyB/TheFaceG",
                        "reverse_normal": true
                    }
                ]
            },
            "spectrum_guid": "d81d75e1-d633-4de7-812b-87123e3ac7f8",
            "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": {}
    }
}
[17]:
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": "db8526b8-fbde-41ff-a1c7-61735a3e297d"
    },
    "source_guid": "3c7a9e2f-07be-45a1-a593-9fcbc5801da4",
    "description": "",
    "source": {
        "name": "Surface.1",
        "surface": {
            "luminous_intensity_flux": {
                "luminous_intensity_value": 5.0
            },
            "intensity_guid": "b43ad23f-d4f7-40b3-a785-bd4e8deb6852",
            "exitance_constant": {
                "geo_paths": [
                    {
                        "geo_path": "TheBodyB/TheFaceF",
                        "reverse_normal": false
                    },
                    {
                        "geo_path": "TheBodyB/TheFaceG",
                        "reverse_normal": true
                    }
                ]
            },
            "spectrum_guid": "d81d75e1-d633-4de7-812b-87123e3ac7f8",
            "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": {}
    }
}
[18]:
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": "b43ad23f-d4f7-40b3-a785-bd4e8deb6852",
            "exitance_constant": {
                "geo_paths": [
                    {
                        "geo_path": "TheBodyB/TheFaceF",
                        "reverse_normal": false
                    },
                    {
                        "geo_path": "TheBodyB/TheFaceG",
                        "reverse_normal": true
                    }
                ]
            },
            "spectrum_guid": "d81d75e1-d633-4de7-812b-87123e3ac7f8",
            "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

[19]:
for item in speos.client.intensity_templates().list() + speos.client.spectrums().list():
    item.delete()