NPSpec  0.2.0
Solve for the spectral properties of nanoparticles easily
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
Examples for each binding

Table of Contents

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

  1. Quartz, 40%
  2. Au, 35%
  3. 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 "npspec/npspec.h"
#include "stdio.h"
int main () {
/* Input variables */
const int nlayers = 3;
const double radius[2] = { 20.0, -1.0 }; /* -1.0 in second value tells the
routine the nanoparticle is a
sphere */
const double relative_radius[nlayers][2] = { { 0.40, 0.40 }, /* 40% */
{ 0.35, 0.35 }, /* 35% */
{ 0.25, 0.25 } }; /* 25% */
const int increment = 5; /* Only calculate every 5th wavelength */
const enum SpectraType stype = Molar;
int mat_index[nlayers];
const double medium_refrac = 1.0;
const bool size_correct = false;
/* The next two are not used be required to pass to the npspec function */
const double path_length = 1.0;
const double concentration = 1e-6;
/* Result variables */
enum ErrorCode result;
double r, g, b, h, s, v;
double qext[NLAMBDA], qscat[NLAMBDA], qabs[NLAMBDA];
/* Get the material index for each layer */
index[0] = material_index("Quartz");
index[1] = material_index("Au");
index[2] = material_index("Ag");
/* Calculate the spectrum */
result = npspec(nlayers, radius, relative_radius, mat_index, medium_refrac,
size_correct, increment, path_length, concentration,
stype, qext, qscat, qabs);
if (result != NoError) {
printf("Calculation did not end correctly!");
return 1;
}
/* Get the color in HSV */
RGB(qabs, increment, false, &r, &g, &b);
RGB_to_HSV(r, g, b, &h, &s, &v);
/* Assuming you have a plotting function...
Use scattering propery, and the wavelengths array that gets imported
with the npspec.h.
*/
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
! Input variables
integer(C_INT) :: mat_index(3)
integer(C_INT), parameter :: nlayers = 3;
! -1.0 in second value tells the routine the nanoparticle is a sphere
real(C_DOUBLE), parameter :: radius(2) = (/ 20.0d0, -1.0d0 /)
! 40%, 35%, 25% relative radius
real(C_DOUBLE), parameter :: relative_radius(nlayers,2) = &
reshape((/ 0.40d0, 0.40d0, &
0.35d0, 0.35d0, &
0.25d0, 0.25d0 /), (/nlayers, 2/))
! Only calculate every 5th wavelength
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.
! The next two are not used be required to pass to the npspec function
real(C_DOUBLE), parameter :: path_length = 1.0d0
real(C_DOUBLE), parameter :: concentration = 1d-6
! Result variables
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 */
call make_c_string("Quartz", mat)
index(1) = material_index(mat);
call make_c_string("Au", mat)
index(2) = material_index(mat);
call make_c_string("Ag", mat)
index(3) = material_index(mat);
! Calculate the spectrum
result = npspec(nlayers, radius, relative_radius, mat_index, medium_refrac,
size_correct, increment, path_length, concentration,
stype, qext, qscat, qabs)
if (result != NoError) then
write(*,*) "Calculation did not end correctly!"
return
end if
! Get the color in HSV
call rgb(qabs, increment, .false., r, g, b)
call rgb_to_hsv(r, g, b, h, s, v)
! Assuming you have a plotting function...
! Use scattering propery, and the wavelengths array that gets imported
! with the NPSpecModule.
call plot(wavelengths, qscat)
end program main

C++ Example

The C++ binding is an OOP interface and uses a different paradigm.

int main (void) {
// Set the nanoparticle's physical properties
np.setNLayers(3);
np.setSphereRadius(20.0);
np.setSphereLayerMaterial(1, "Quartz");
np.setSphereLayerMaterial(2, "Au");
np.setSphereLayerMaterial(3, "Ag");
// Set how the spectrum will be calculated
np.setIncrement(5.0);
// Calculate the spectrum
try {
} catch (const std::exception& e) {
cerr << "Calculation did not end correctly!";
return 1;
}
// Get the color in HSV
double h, s, v;
np.getHSV(h, s, v);
// Assuming you have a plotting function...
double spectrum[NPSpec::NLAMBDA];
np.getSpectrum(spectrum);
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 "#"
1 \#! /usr/bin/env python
2 
3 import npspec
4 import sys
5 
6 np = npspec.Nanoparticle()
7 
8 \# Set the nanoparticle's physical properties
9 np.setNLayers(3)
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)
17 
18 \# Set how the spectrum will be calculated
19 np.setSpectraType(npspec.Molar)
20 np.setSpectraProperty(npspec.Scattering)
21 np.setIncrement(5.0)
22 
23 \# Calculate the spectrum
24 try:
25  np.calculateSpectrum()
26 except (IndexError, ValueError):
27  sys.exit("Calculation did not end correctly!")
28 
29 \# Get the color in HSV
30 h, s, v = np.getHSV()
31 
32 \# Assuming you have a plotting function...
33 spectrum = np.getSpectrum()
34 plot(npspec.wavelengths, spectrum)
35 \# -- OR --
36 plot(npspec.wavelengths, np.getSpectrum())