The Units System

Most of these functions can be acccessed directly from Solcore:

>>> from solcore import eV

>>> print(eV(1e-19))
0.624 eV
exception solcore.units_system.units_system.UnitError(msg)[source]
exception solcore.units_system.units_system.WrongDimensionError(msg)[source]
solcore.units_system.units_system.generateConversionDictForSISuffix(suffix, centi=False, deci=False, non_base_si_factor=1)[source]
class solcore.units_system.units_system.UnitsSystem(sources=None)[source]

Contains all the functions related with the conversion of units. While defined inside this class, most of these functions are available outside it, being decorated with ‘breakout’ (see ‘Singleton’)

read(name=None)[source]

Reads the units file and creates a database with all units and conversion factors.

safe_eval(string_expression)[source]
siUnits(value, unit)[source]

Convert value from unit to equivalent si-unit

>>> print(siUnits(1,"mm")) # yields meters
0.001
>>> print(siUnits(1,"um")) # yields meters
1e-06
Parameters:
  • value – the value to convert
  • unit – the units of the value
Returns:

the value expresed in SI units

asUnit(value, unit)[source]

Converts from si unit to other unit. It is the reversed of siUnits function

>>> print(asUnit(1, "mA")) # print 1A in mA.
1000.0
Parameters:
  • value – the value to convert, assumed in SI units
  • unit – the new units
  • dimension – the value expressed in the new units.
Returns:

si(*args)[source]

Utility function that forwards to either siUnit or siUnitFromString

siUnitFromString(string)[source]

Converts a string of a number with units into si units of that quantity

>>> print(si("5 mm s-1")) # output in m/s
0.005
>>> print(si("5e-0mm-2")) # output in m2
5000000.0
>>> print(si("5"))
5.0
Parameters:string – the string to convert
Returns:the value in SI units
convert(value, from_unit, to_unit)[source]

Converts between comparable units, does NOT check if units are comparable.

>>> print(convert(1, "nm", "mm"))
1e-06
>>> print(convert(1, "um", "nm"))
1000.0
>>> print(convert(1, "cm s-1", "km h-1"))
0.036
Parameters:
  • value – the value ot convert
  • from_unit – the original unit
  • to_unit – the final unit
Returns:

the value expressed in the final unit

eVnm(value)[source]

Bi-directional conversion between nm and eV.

>>> print('%.3f'%eVnm(1000))
1.240
>>> print('%i'%round(eVnm(1)))
1240
Parameters:value – a number with units [nm] or [eV].
Returns:either the conversion [nm] –> [eV], or [eV] –> [nm]
nmJ(value)[source]

Bi-directional conversion between nm and J.

>>> print(nmJ(1000))
1.9864452126e-19
>>> print(nmJ(2e-18))
99.3222606298
Parameters:value – a number with units [nm] or [J].
Returns:either the conversion [nm] –> [J], or [J] –> [nm]
mJ(value)[source]

Bi-directional conversion between m and J.

>>> print(mJ(1000))
1.986445212595144e-25
>>> print(mJ(2e-18))
9.93222606297572e-08
Parameters:value – a number with units [m] or [J].
Returns:either the conversion [m] –> [J], or [J] –> [m]
nmHz(value)[source]

Bi-directional conversion between nm and Hz.

Parameters:value – a number with units [nm] or [Hz].
Returns:Either a number which is the conversion [nm] –> [Hz] or [Hz] –> [nm]
spectral_conversion_nm_ev(x, y)[source]

Bi-directional conversion between a spectrum per nanometer and a spectrum per electronvolt.

Example: 1) nm –> eV conversion wavelength_nm photon_flux_per_nm energy_ev, photon_flux_per_ev = spectral_conversion_nm_ev(wavelength_nm, photon_flux_per_nm)

2) eV –> nm conversion energy_ev photon_flux_per_ev wavelength_nm, photon_flux_per_nm = spectral_conversion_nm_ev(energy_ev, photon_flux_per_ev)

Discussion: A physical quantities such as total number of photon in a spectrum or total energy of a spectrum should remain invariant after a transformation to different units. This is called a spectral conversion. This function is bi-directional because the mathematics of the conversion processes is symmetrical.

>>> import numpy as np
>>> x = np.array([1,2,3])
>>> y = np.array([1,1,1])
>>> area_before = np.trapz(y, x=x)
>>> x_new, y_new = spectral_conversion_nm_ev(x, y)
>>> area_after = np.trapz(y_new, x=x_new)
>>> compare_floats(area_before, area_after, relative_precision=0.2)
True
Parameters:
  • x – abscissa of the spectrum in units of [nm] or [eV]
  • y – ordinate of the spectrum in units of [something/nm] or [something/eV]
Returns:

A tuple (x, y) which has units either [eV, something/eV] or [nm. something/nm].

spectral_conversion_nm_hz(x, y)[source]

Bi-directional conversion between a spectrum per nanometer and a spectrum per Hertz.

Example: 1) nm –> Hz conversion wavelength_nm photon_flux_per_nm energy_hz, photon_flux_per_hz = spectral_conversion_nm_hz(wavelength_nm, photon_flux_per_nm)

2) Hz –> nm conversion energy_hz photon_flux_per_hz wavelength_nm, photon_flux_per_nm = spectral_conversion_nm_ev(energy_hz, photon_flux_per_hz)

Discussion: A physical quantities such as total number of photon in a spectrum or total energy of a spectrum should remain invariant after a transformation to different units. This is called a spectral conversion. This function is bi-directional because the mathematics of the conversion processes is symmetrical.

>>> import numpy as np
>>> x = np.array([1,2,3])
>>> y = np.array([1,1,1])
>>> area_before = np.trapz(y, x=x)
>>> x_new, y_new = spectral_conversion_nm_hz(x, y)
>>> area_after = np.trapz(y_new, x=x_new)
>>> compare_floats(area_before, area_after, relative_precision=0.2)
True
Parameters:
  • x – abscissa of the spectrum in units of [nm] or [Hz]
  • y – ordinate of the spectrum in units of [something/nm] or [something/Hz]
Returns:

A tuple (x, y) which has units either [eV, something/nm] or [nm. something/Hz].

sensibleUnits(value, dimension, precision=2)[source]

Attempt to convert a physical quantity of a particular dimension to the most sensible units

>>> print(sensibleUnits(0.001,"length",0))
1 mm
>>> print(sensibleUnits(1000,"length",0))
1 km
>>> print(sensibleUnits(si("0.141 days"),"time", 5))
3.38400 h
Parameters:
  • value – The value to re-calculate in SI units
  • dimension – The dimension of the value. Possible values are: ‘luminous intensity’, ‘pressure’, ‘time’, ‘angle’, ‘temperature’, ‘current’, ‘force’, ‘charge’, ‘power’, ‘voltage’, ‘resistance’, ‘mass’, ‘length’, ‘energy’
  • precision – Precission of the converted value.
Returns:

A string with the value in the more ‘sensible’ units and the units.

eV(e)[source]

Transform an energy value in SI units in a string expresing the value in eV.

>>> print(eV(1e-19))
0.624 eV
Parameters:e – Energy value in SI units
Returns:A string with the energy converted in eV and its units.
guess_dimension(unit)[source]

Guess the dimension of a unit.

>>> print guess_dimension("nm")
length
Parameters:unit – the unit.
Returns:None
list_dimensions()[source]
solcore.units_system.units_system.compare_floats(a, b, absoulte_precision=1e-12, relative_precision=None)[source]

Returns true if the absolute difference between the numbers a and b is less than the precision.

Arguments: a – a float b – a float

Keyword Arguments (optional): absolute_precision – the absolute precision, abs(a-b) of the comparison. relative_precision – the relative precision, max(a,b)/min(a,b) - 1. of the comparison.

Returns: True if the numbers are the same within the limits of the precision. False if the number are not the same within the limits of the precision.

solcore.units_system.units_system.independent_nm_ev(x, y)[source]
solcore.units_system.units_system.independent_nm_J(x, y)[source]
solcore.units_system.units_system.independent_m_J(x, y)[source]
solcore.units_system.units_system.reverse(x)[source]