# # Prism example
#
# This tutorial demonstrates how to open an existing Speos file, run
# the simulation and open the result. After that it explains how to
# edit the File and adjust Parameters of the sensor.
#
# ## Prerequisites
#
# ### Perform imports

# +
import os
from pathlib import Path

from ansys.speos.core import Body, Project, Speos
from ansys.speos.core.kernel.client import (
    default_docker_channel,
)
from ansys.speos.core.launcher import launch_local_speos_rpc_server
from ansys.speos.core.sensor import Sensor3DIrradiance, SensorIrradiance
from ansys.speos.core.simulation import SimulationDirect

# -

# ### Define constants
#
# The constants help ensure consistency and avoid repetition throughout the example.

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.

# ## Model Setup
#
# ### Load assets
# The assets used to run this example are available in the
# [PySpeos repository](https://github.com/ansys/pyspeos/) on GitHub.
#
# > **Note:** Make sure you
# > have downloaded simulation assets and set ``assets_data_path``
# > to point to the assets folder.

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


# ### Start/Connect to Speos 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. The launch_local_speos_rpc_method can
# be used to start a local instance of the service.

if USE_DOCKER:
    speos = Speos(channel=default_docker_channel())
else:
    speos = launch_local_speos_rpc_server(port=GRPC_PORT)


# ## Create project
#
# Load a project from .speos file.

p = Project(speos=speos, path=str(assets_data_path / "Prism.speos" / "Prism.speos"))
print(p)

# ## Preview
# This preview method allows you to preview the content of the Speos solver
# file.

p.preview()


# ## Retrieve the simulation feature and open result
#
# Run the simulation

sim_features = p.find(name="Prism", feature_type=SimulationDirect)
sim = sim_features[0]
sim.compute_CPU()

# Use the open_result_image method to review the result


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.

irr_features = p.find(name=".*", name_regex=True, feature_type=SensorIrradiance)
irr = irr_features[0]
wl = irr.set_type_spectral().set_wavelengths_range()
wl.start = 500
wl.end = 600
wl.sampling = 11
irr.commit()

# Create and add a new sensor, e.g. 3d irradiance sensor

body = p.find(name="PrismBody", name_regex=True, feature_type=Body)[0]
sensor_3d = p.create_sensor(name="3d_irradiance", feature_type=Sensor3DIrradiance)
sensor_3d.geometries = [body.geo_path]
sensor_3d.commit()
sim.sensor_paths == ["Irradiance.1:564", "3d_irradiance"]
sim.commit()
p.preview()

# ## Re-run the simulation with new sensor definition.

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

speos.close()
