otsun package¶
Submodules¶
otsun.experiments module¶
Module otsun.experiments for creating experiments.
The module defines a class Experiment that deals with the setup and run of experiments.
-
class
otsun.experiments.
Experiment
(scene, light_source, number_of_rays, show_in_doc=None)¶ Bases:
object
Sets up and runs and experiment in a given scene with a given light source.
An Experiment is formed by a scene and a ligh_source. Rays are emitted from light_source and interfere with the scene until they are absorbed or scape from the scene. If they are absorbed, the energies are computed and stored.
Parameters: - scene (otsun.Scene) – Scene of the experiment
- light_source (otsun.LightSource) – Source of the rays to emit
- number_of_rays (int) – Number of rays to emit in the experiment
- show_in_doc (App.Document) – FreeCAD document where to plot the rays, or None if plotting is not desired
-
wavelengths
¶ List of wavelengths of emitted rays
Type: List of float
-
captured_energy_Th
¶ Total energy of rays that got absorbed
Type: float
-
captured_energy_PV
¶ Total energy of rays that fell in a PV
Type: float
-
Th_energy
¶
-
Th_wavelength
¶ List of wavelengths of rays that got absorbed
Type: List of float
-
PV_energy
¶ List of energies of rays that fell in a PV
Type: List of floats
-
PV_wavelength
¶ List of wavelengths of rays that fell in a PV
Type: List of float
-
PV_values
¶ List of PV_values of all emitted rays that fell in a PV
Type: List of tuples of floats
-
points_absorber_Th
¶ List with data (energy, location,…) for each ray that got absorbed
Type: List of tuple of floats
-
run
(show_in_doc=None)¶ Runs the experiment and plots the rays in the document specified (if any)
otsun.logging_unit module¶
Module otsun.logging_unit: Sets up a logger for all logs emitted from otsun
otsun.materials module¶
Module otsun.materials for treating materials
The module relies on a basic class Material with two subclasses VolumeMaterial and SurfaceMaterial, and several subclasses of them for specific materials.
-
class
otsun.materials.
NumpyEncoder
(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)¶ Bases:
json.encoder.JSONEncoder
Wrapper to dump numpy arrays as json
-
default
(obj)¶ Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
-
-
class
otsun.materials.
PVMaterial
(name, file_index_of_refraction)¶ Bases:
sphinx.ext.autodoc.importer._MockObject
Subclass of VolumeMaterial for photovoltaic materials.
-
get_PV_data
(ray, energy_before)¶ Computes the photovoltaic data stored in a ray.
Parameters: - ray (Ray) – Ray that has passed through the PV material
- energy_before (float) – Energy of the ray before passing through the PV material
Returns: Return type: float, tuple of floats
-
-
class
otsun.materials.
PolarizedThinFilm
(name, file_thin_film, file_front, file_back)¶ Bases:
sphinx.ext.autodoc.importer._MockObject
Subclass of VolumeMaterial for polarized thin film materials.
-
static
calculate_state_thin_film
(incident, normal_vector, n1, n2, polarization_vector, properties, wavelength)¶ Helper function for the computation of the optical state once the ray has passed through the film
-
change_of_optical_state
(ray, normal_vector)¶
-
static
-
class
otsun.materials.
SimpleVolumeMaterial
(name, index_of_refraction, attenuation_coefficient=None)¶ Bases:
sphinx.ext.autodoc.importer._MockObject
Subclass of VolumeMaterial for those materials with constant properties.
-
class
otsun.materials.
WavelengthVolumeMaterial
(name, file_index_of_refraction)¶ Bases:
sphinx.ext.autodoc.importer._MockObject
Subclass of VolumeMaterial for materials with tabulated index of refraction.
-
otsun.materials.
load_from_txt_or_csv
(file)¶
-
otsun.materials.
myrandom
()¶ random() -> x in the interval [0, 1).
-
otsun.materials.
vacuum_medium
¶ Subclass of VolumeMaterial for those materials with constant properties.
otsun.math module¶
Module otsun.math with mathematical helper functions
-
otsun.math.
arccos
(x)¶ Safe modification of arccos
-
otsun.math.
area_of_triangle
(vertices)¶ Compute the area of the triangle with given vertices
-
otsun.math.
cdf_from_pdf_file
(data_file)¶ Computes CDF from PDF values stored in a file
Creates a Cumulative Distribution Function from Probability Density Function data file. Each line must be a pair of numbers x y=pdf(x). It returns the CDF as two lists; first on is the list of x-values, second one is the list of corresponding CDF values.
Parameters: data_file (file or str) – file or filename where PDF values are stored Returns: x-values and y-values of CDF Return type: list of float, list of float
-
otsun.math.
constant_function
(c)¶ Create a constant function
Parameters: c (float) – constant to return Returns: Constant function equal to c Return type: function
-
otsun.math.
correct_normal
(normal, incident)¶ Corrects a vector so that is in a given half plane
Parameters: - normal (Base.Vector) –
- incident (Base.Vector) –
-
otsun.math.
myrandom
()¶ random() -> x in the interval [0, 1).
-
otsun.math.
normalize
(vector)¶ Normalizes a vector
-
otsun.math.
one_orthogonal_vector
(vector)¶ Gives one orthogonal vector of a vector
Given vector find one orthogonal vector
Parameters: vector (Base.Vector) – Returns: orthogonal Return type: Base.Vector
-
otsun.math.
parallel_orthogonal_components
(vector, incident, normal)¶ Decomposition of vector in components
Given vector (a polarization), incident (direction of a ray) and normal (vector orthogonal to a plane), decompose vector it in a component contained in the reflection (parallel) plane (det. by normal and incident): p-polarized (parallel) light a component contained in the orthogonal plane to the reflection plane: s-polarized (perpendicular) light also returns the normal vector to the reflection plane
Parameters: - vector (Base.Vector) –
- incident (Base.Vector) –
- normal (Base.Vector) –
Returns: - parallel (Base.Vector)
- orthogonal (Base.Vector)
- normal_of_parallel_plane (Base.Vector)
-
otsun.math.
pick_random_from_cdf
(cdf)¶ Pick a random value according to a given CDF.
We apply the Inverse transform sampling: https://en.wikipedia.org/wiki/Inverse_transform_sampling
Parameters: cdf (tuple of list of float) – First list is list of x-values; second one is list of values of CDF Returns: Return type: float
-
otsun.math.
polar_to_cartesian
(phi, theta)¶ Convert polar coordinates of unit vector to cartesian
Parameters: - phi (float) – phi angle (ISO 31-11) in degrees
- theta (float) – theta angle (ISO 31-11) in degrees
Returns: Return type: Base.Vector
-
otsun.math.
projection_on_orthogonal_of_vector
(u, v)¶ Compute the projection of u on the subspace orthogonal to <v>
-
otsun.math.
projection_on_vector
(u, v)¶ Compute the projection of u on <v>
-
otsun.math.
rad_to_deg
(angle)¶ Converts radians to degrees
-
otsun.math.
random_point_of_triangle
(vertices)¶ Compute a random point of the triangle with given vertices
-
otsun.math.
tabulated_function
(xvalues, yvalues)¶ Create a linear interpolating function from tabulated values
Parameters: - xvalues (list of float) – x coordinates of the tabulated values
- yvalues (list of float) – y coordinates of the tabulated values
Returns: Function that interpolates by straight line segments the input data
Return type: function
-
otsun.math.
two_orthogonal_vectors
(vector)¶ Gives two orthogonal vectors of a vector
Given vector find two orthogonal vectors
Parameters: vector (Base.Vector) – Returns: - orthogonal_1 (Base.Vector)
- orthogonal_2 (Base.Vector)
otsun.movements module¶
Module otsun.movements for computing movements
The module defines the classes Joint (with subclasses AxialJoint and CentralJoint) and MultiTracking
-
otsun.movements.
apply_movement_to_vector
(movement, vector)¶
-
otsun.movements.
axial_rotation_from_axis_and_angle
(axis_origin, axis_dir, angle)¶ Returns a rotation with given axis and angle
-
otsun.movements.
axial_rotation_from_axis_vector_and_image
(axis_origin, axis_dir, vector0, vector1)¶
-
otsun.movements.
axial_rotation_from_vector_and_image
(origin, vector0, vector1)¶ Returns a rotation that transforms the ray with origin and vector equals to vector0 to the ray with same origin and vector vector1.
-
otsun.movements.
biaxial_rotation_from_vector_and_image
(origin, vector_horizontal, vector_vertical, current_normal, desired_normal)¶
-
otsun.movements.
get_labels
(obj)¶ Gets the different labels of a given FreeCAD object
-
otsun.movements.
orientation
(u, v, w)¶ Computes the orientation (+/-1) of a basis
-
otsun.movements.
signed_angle
(axis, v1, v2)¶ Computes the (signed) angle from v1 to v2, wrt the axis vector
otsun.optics module¶
Module otsun.optics
Implementation of optical effects on rays
-
class
otsun.optics.
OpticalState
(polarization, direction, phenomenon, material=None, extra_data=None)¶ Bases:
object
Optical state of a ray.
The OpticalState class gathers together information about the optical state of a ray.
Parameters: - polarization (Base.Vector) – polarization vector of the ray
- direction (Base.Vector) – direction vector of the ray
- phenomenon (Phenomenon) – last phenomenon that the ray experimented
- solid (Shape) – solid where the ray is located
- material (Material) – Material where the ray is located
- extra_data (dict) – Dictionary where materials can put extra data
-
apply_dispersion
(properties, normal_vector)¶
-
apply_double_gaussian_dispersion
(normal, sigma_1, sigma_2, k)¶ Apply a double gaussian dispersion to the state
Parameters: - normal (Base.Vector) – normal vector of the surface at the point of incidence
- sigma_1 (float) – dispersion coefficient (first case)
- sigma_2 (float) – dispersion coefficient (second case)
- k (float) – threshold for randomly applying first or second case
-
apply_single_gaussian_dispersion
(normal, sigma_1)¶ Apply a single gaussian dispersion to the optical state
Parameters: - normal (Base.Vector) – normal vector of the surface at the point of incidence
- sigma_1 (float) – dispersion coefficient
-
class
otsun.optics.
Phenomenon
¶ Bases:
enum.Enum
Enum for optical phenomena.
This enum is used to describe which optical phenomenon affected a ray
-
ABSORPTION
= 3¶
-
ENERGY_ABSORBED
= 5¶
-
JUST_STARTED
= 0¶
-
REFLEXION
= 1¶
-
REFRACTION
= 2¶
-
TRANSMITTANCE
= 4¶
-
-
otsun.optics.
calculate_reflectance
(m_reflectance, angle, wavelength)¶ Compute perpendicular and parallel components of reflectance
Interpolates the value of the perperdicular and parallel reflectance from the matrix of values, depending on the angle and the wavelength
Parameters: - m_reflectance (float or tuple of list of floats) –
- angle (float) –
- wavelength (float) –
Returns: Return type: tuple of float or tuple of np.complex
-
otsun.optics.
dispersion_from_main_direction
(main_direction, theta, phi)¶ Computes dispersion from the main direction in terms of angles theta and phi
-
otsun.optics.
dispersion_polarization
(main_direction, polarization_vector, theta, phi)¶ Computes dispersion of polarization vector in terms of angles theta and phi
-
otsun.optics.
lambertian_reflection
(incident, normal_vector)¶ Implementation of lambertian reflection for diffusely reflecting surface
Parameters: - incident (Base.Vector) – vector incident
- normal_vector (Base.Vector) – vector normal to the surface
Returns: optical state of the reflected ray
Return type:
-
otsun.optics.
matrix_reflectance
(data_material)¶ Computes the matrix of reflectances of a material
data_material: wavelenth in nm, angle in deg., reflectance s-polarized (perpendicular), reflectance p-polarized (parallel) the values should be in the corresponding order columns with constants steps We generate a matrix for the interpolation depending on the x=angle and y=wavelength values (lambda values)
Parameters: data_material –
-
otsun.optics.
myrandom
()¶ random() -> x in the interval [0, 1).
-
otsun.optics.
random_polarization
(direction)¶ Returns a random polarization orthogonal to the given direction
-
otsun.optics.
reflection
(incident, normal_vector, polarization_vector, polarization_vector_calculated_before=False)¶ Implementation of law of reflection for incident and polarization vector.
Parameters: - incident (Base.Vector) – vector incident
- normal_vector (Base.Vector) – vector normal to the surface
- polarization_vector (Base.Vector) – Polarization vector of the ray
- polarization_vector_calculated_before (bool) – True if the polarization vector was computed before
Returns: optical state of the reflected ray
Return type:
-
otsun.optics.
refraction
(incident, normal_vector, n1, n2, polarization_vector, lambertian_surface=False)¶ Implementation of Fresnel equations of refraction
Parameters: - incident (Base.Vector) – direction vector of the incident ray
- normal_vector (Base.Vector) – normal vector of the surface at the point of incidence
- n1 (complex) – complex refractive index where ray is currently traveling
- n2 (complex) – complex refractive index of nearby material
- polarization_vector (Base.Vector) – Polarization vector of the ray
- lambertian_surface (Bool) – Indicates if the surface has lambertian reflection
Returns: Return type:
-
otsun.optics.
shure_refraction
(incident, normal_vector, n1, n2, polarization_vector, lambertian_surface=False)¶ Implementation of Snell’s law of refraction
Parameters: - incident (Base.Vector) – direction vector of the incident ray
- normal_vector (Base.Vector) – normal vector of the surface at the point of incidence
- n1 (complex) – complex refractive index where ray is currently traveling
- n2 (complex) – complex refractive index of nearby material
- polarization_vector (Base.Vector) – Polarization vector of the ray
- lambertian_surface (Bool) – Indicates if the surface has lambertian reflection
Returns: Return type:
-
otsun.optics.
simple_polarization_reflection
(incident, normal, normal_parallel_plane, polarization)¶
-
otsun.optics.
simple_polarization_refraction
(incident, normal, normal_parallel_plane, c2, polarization_vector)¶
-
otsun.optics.
simple_reflection
(incident, normal)¶
otsun.outputs module¶
Module otsun.outputs
Helper functions to format data for output
-
otsun.outputs.
integral_from_data_file
(file_in)¶
-
otsun.outputs.
make_histogram_from_experiment_results
(results_wavelength, results_energy, step_wavelength, aperture_collector, aperture_source)¶
-
otsun.outputs.
photo_current
(spectral_response, source_spectrum)¶
-
otsun.outputs.
spectral_response
(optical_absorption_wavelength, iqe)¶
-
otsun.outputs.
spectrum_to_constant_step
(file_in, wavelength_step, wavelength_min, wavelength_max)¶
-
otsun.outputs.
twoD_array_to_constant_step
(twoD_array, step, wavelength_min, wavelength_max)¶
otsun.ray module¶
Module otsun.ray for the modelization of light rays
The module defines the class Ray
otsun.scene module¶
Module otsun.scene for the modelization optical systems
The module defines the class Scene that models the elements in an optical system
-
class
otsun.scene.
Scene
(objects)¶ Bases:
object
Class used to define the Scene. It encodes all the objects that interfere with light rays.
-
face_at_point
(point)¶ Returns the face that a point is inside.
-
next_solid_at_point_in_direction
(point, normal, direction)¶ Returns the next solid found in the given direction from a given point
-
recompute_boundbox
()¶ Recomputes the boundbox, so that all objects are contained in it
-
remove_duplicate_faces
()¶ Removes redundant faces, so that the computation of next_intersection does not find duplicated points.
-
solid_at_point
(point)¶ Returns the solid that a point is inside.
-
otsun.source module¶
Module otsun.source that implements rays and its sources
-
class
otsun.source.
GeneralizedSunWindow
(scene, main_direction)¶ Bases:
object
-
add_to_document
(doc)¶
-
random_direction
()¶ Returns the main direction
Maybe in the future will return some random vector
Returns: Return type: Base.Vector
-
random_point
()¶
-
-
class
otsun.source.
LightSource
(scene, emitting_region, light_spectrum, initial_energy, direction_distribution=None, polarization_vector=None)¶ Bases:
object
Sets up a light source with a given scene, a given emitting region and a given light spectrum. The emitting region must provide the main direction. Light spectrum could be: a constant value (for a single wavelength in nanometers), or a spectrum distribution. The distribution (dispersion) for the main direction is provided in “direction_distribution”. The polarization_vector is a Base.Vector for polarized light. If is not given unpolarized light is generated.
-
emit_ray
()¶ Simulates the emission of a ray
-
-
class
otsun.source.
SunWindow
(scene, main_direction)¶ Bases:
object
Class that implements a Sun window (rectangle that emits rays)
Source of light defined by a rectangle “at infinity” in space that emits rays perpendicular to it.
Parameters: - scene (Scene) – Scene that contains the sun window
- main_direction (Base.Vector) – Vector orthogonal to the emitting region
-
origin
¶ Center of the rectangle
Type: Base.Vector
-
v1, v2
Unit vectors parallel to the sides of the rectangle
Type: Base.Vector
-
length1, length2
Lengths of the sides of the rectangle
Type: float
-
aperture
¶ Area of the rectangle
Type: float
-
add_to_document
(doc)¶ Adds the rectangle to the FreeCAD document
Parameters: doc (App.Document) –
-
static
find_min_rectangle
(points, normal)¶ Computes the minimum rectangle covering points in a direction
Given a list of points, take its projection in a normal direction, and the rectangle with minimum area that encloses this projections
Parameters: - points (list of Base.Vector) – List of points to enclose
- normal (Base.Vector) – Vector orthogonal to the rectangle to be found
Returns: - origin (Base.Vector) – Center of the rectangle
- best_v1, best_v2 (Base.Vector) – Unit vector parallel to the sides of the rectangle
- best_v2 (Base.Vector) – Other unit vector parallel to the sides of the rectangle
- length1 (float) – Length of side of the rectangle
- length2 (float) – Length of other side of the rectangle
-
random_direction
()¶ Returns the main direction
Maybe in the future will return some random vector
Returns: Return type: Base.Vector
-
random_point
()¶ Returns a random point on the rectangle
Returns: Return type: Base.Vector
-
otsun.source.
buie_distribution
(CircumSolarRatio)¶ Implementation of the Buie Distribution for Sun emission
A distribution for the direct light from the sun according to the Buie model: Buie, D., 2005. Corrigendum to “The effective size of the solar cone for solar concentrating systems” [Solar Energy 74 (2003) 417-427]. Solar Energy 79, 568-570. 5.2
Parameters: CircumSolarRatio (float) – Returns: angle distribution for random input – Function that interpolates by straight line segments the input data Return type: function
-
otsun.source.
myrandom
()¶ random() -> x in the interval [0, 1).