#C++
#include <cmath>
#include <vector>
#include <iostream>
// Define an ion type to hold the
// coordinates of the ion
struct Ion
{
()
Ion{
= 0;
x = 0;
y = 0;
z }
(double _x, double _y, double _z)
Ion{
= _x;
x = _y;
y = _z;
z }
double x;
double y;
double z;
};
// The reference ion
;
Ion reference_ion
// Return the square of a number
inline double square(const double x)
{
return x*x;
}
// Energy function to be mapped
double calc_energy(const Ion &ion)
{
double r = std::sqrt( square( reference_ion.x - ion.x ) +
( reference_ion.y - ion.y ) +
square( reference_ion.z - ion.z ) );
square
// The energy is simply 1 / r
return 1.0 / r;
}
// You would need to fill in this function to read
// in an array of ions and return the array
std::vector<Ion> readArrayOfIons()
{
std::vector<Ion> ions;
for (int i=0; i<10; ++i)
{
.push_back( Ion(0,0,i) );
ions}
return ions;
}
int main(int argc, const char **argv)
{
//put the reference ion at (1,2,3)
.x = 1.0;
reference_ion.y = 2.0;
reference_ion.z = 3.0;
reference_ion
//read in the array of ions
std::vector<Ion> ions_array = readArrayOfIons();
double total_energy = 0.0;
#pragma omp parallel reduction( + : total_energy )
{
double mapped_energy = 0.0;
const int n_ions = ions_array.size();
#pragma omp for
for (int i=0; i < n_ions; ++i)
{
//map this ion against the function
+= calc_energy( ions_array[i] );
mapped_energy }
//reduce to get the result
+= mapped_energy;
total_energy }
std::cout << "The total energy is " << total_energy << std::endl;
return 0;
}