Explanation of examples
Below, a code example for each language binding of NPSpec is shown. Each example will do the same thing: Define a 3 layer spherical 20 nm nanoparticle, where the three layers have a material and relative radius of
- Quartz, 40%
- Au, 35%
- Ag, 25%
The scattering will be calculated and represented by the molar absorbtivitty; every 5th wavelength will be calculated. No size correction will be used and it is in a medium of refractive index 1.0.
After calculation, the color will be calculated in HSV color space.
C Example
#include "stdio.h"
int main () {
const int nlayers = 3;
const double radius[2] = { 20.0, -1.0 };
const double relative_radius[nlayers][2] = { { 0.40, 0.40 },
{ 0.35, 0.35 },
{ 0.25, 0.25 } };
const int increment = 5;
int mat_index[nlayers];
const double medium_refrac = 1.0;
const bool size_correct = false;
const double path_length = 1.0;
const double concentration = 1e-6;
double r, g, b, h, s, v;
index[0] = material_index("Quartz");
index[1] = material_index("Au");
index[2] = material_index("Ag");
result = npspec(nlayers, radius, relative_radius, mat_index, medium_refrac,
size_correct, increment, path_length, concentration,
stype, qext, qscat, qabs);
printf("Calculation did not end correctly!");
return 1;
}
RGB(qabs, increment, false, &r, &g, &b);
RGB_to_HSV(r, g, b, &h, &s, &v);
plot(wavelengths, qscat);
return 0;
}
Fortran Example
You would write Fortran code using the same paradigm as C code.
program main
use, intrinsic :: iso_c_binding
implicit none
integer(C_INT) :: mat_index(3)
integer(C_INT), parameter :: nlayers = 3;
real(C_DOUBLE), parameter :: radius(2) = (/ 20.0d0, -1.0d0 /)
real(C_DOUBLE), parameter :: relative_radius(nlayers,2) = &
reshape((/ 0.40d0, 0.40d0, &
0.35d0, 0.35d0, &
0.25d0, 0.25d0 /), (/nlayers, 2/))
integer(C_INT), parameter :: increment = 5
integer(C_INT), parameter :: stype = molar
real(C_DOUBLE), parameter :: medium_refrac = 1.0d0
real(C_DOUBLE), parameter :: size_correct = .false.
real(C_DOUBLE), parameter :: path_length = 1.0d0
real(C_DOUBLE), parameter :: concentration = 1d-6
integer(C_INT) :: result
real(C_DOUBLE) :: r, g, b, h, s, v
real(C_DOUBLE) :: qext(nlambda), qscat(nlambda), qabs(nlambda)
character(kind=C_CHAR, len=14) :: mat
/* get the material index for each layer */
index(1) = material_index(mat);
index(2) = material_index(mat);
index(3) = material_index(mat);
result = npspec(nlayers, radius, relative_radius, mat_index, medium_refrac,
size_correct, increment, path_length, concentration,
stype, qext, qscat, qabs)
if (result
write(*,*) "Calculation did not end correctly!"
return
end if
call rgb(qabs, increment, .false., r, g, b)
call rgb_to_hsv(r, g, b, h, s, v)
call plot(wavelengths, qscat)
end program main
C++ Example
The C++ binding is an OOP interface and uses a different paradigm.
int main (void) {
np.setSphereLayerMaterial(1, "Quartz");
np.setSphereLayerMaterial(2, "Au");
np.setSphereLayerMaterial(3, "Ag");
try {
} catch (const std::exception& e) {
cerr << "Calculation did not end correctly!";
return 1;
}
double h, s, v;
plot(NPSpec::wavelengths, spectrum);
return 0;
}
Python Example
The Python API is nearly identical to the C++ API.
- Note
- Due to a bug in Doxygen, I had to put "\#" for the comments instead of just "#"
6 np = npspec.Nanoparticle()
10 np.setSphereRadius(20.0)
11 np.setSphereLayerMaterial(1,
"Quartz")
12 np.setSphereLayerMaterial(2,
"Au")
13 np.setSphereLayerMaterial(3,
"Ag")
14 np.setSphereLayerRelativeRadius(1, 0.40)
15 np.setSphereLayerRelativeRadius(2, 0.35)
16 np.setSphereLayerRelativeRadius(3, 0.25)
19 np.setSpectraType(npspec.Molar)
20 np.setSpectraProperty(npspec.Scattering)
25 np.calculateSpectrum()
26 except (IndexError, ValueError):
27 sys.exit(
"Calculation did not end correctly!")
33 spectrum = np.getSpectrum()
34 plot(npspec.wavelengths, spectrum)
36 plot(npspec.wavelengths, np.getSpectrum())