Turn up the speed with Monte C++arlo
Data Structures
Stepping deeper into the grounds of c++. Today's material covered Arrays and Vectors, as well as Pointers and Memory concepts. Some comparison points between std::array and vectors are:
| * | Array | Vector | |-------------------|-----------------------------------|-------------------------------| | Length | Constant - known at compile time | Dynamic | | Type | Homogeneous | Homogeneous | | Memory | Contigous | Contiguos | | Mem Management | Allocation needs to be dumped | Automatically handled | | Access | Constant | By position - time-consuming | | Indexing | Index-based | Non-Index Based | | Resizing | Expensive - requires new allocation | Dynamic by nature |
Converting MCSIM to C++
We have been running Monte Carlo Simulation in Python. Now that we are learning c++ we are adventuring into creating a package in c++. Today's assignment is to rewrite our code to produce a c++ mcsim package.
Pointers and Reference
Pointers can be thought of as school lockers. Let's say you get assigned locker #2 and you store your chem280 book inside. Well, a pointer would be like some other student also is given a key to access locker #2. So, that other student opens the locker and reads the chem280 book you stored inside. Now what would happen if after they read the chem280 book then they decide to store instead a book on ML? when you open that locker, what book would you find inside? Yes! now instead of finding your, originally stored, chem280 book you find the ML book inside. Nice, ha?
References are another similar concept. However, key differences are:
- Pointers in-memory store the address of the variable they are pointing to.
- Whereas references are like aliases, they have the same address. Whether in a variable or as arguments, they are referencing the same object.
- It would be like someone knocking on your door looking for you but instead of using your name, they somehow got a hold of your secret super hero identity. Oh boy! Same address, same you.
Caveats
A few things to consider while rewriting code from Python to c++.
- Remember that brackets matter
- While a clause can allow the lack of curly bracket for a single line of code, the following lines will be out of scope regardless of indentation.
- Example the code below would throw an error, why?
if(rij < cutoff)
double pairEnergy = culculatePE();
totalEnergy += pairEnergy;
- In c++ you need to declare the type of the variables, and the return type of the functions, if there is no return then we use void.
void printAtom(AtomCoord atomLocation)
{
double atomSize = atomLocation.size();
std::cout<< "Atom location: [";
for (int i=0; i<atomSize;i++)
{
std::cout<<atomLocation.at(i);
if(i < atomSize-1) {
std::cout<<", ";
}
else {
std::cout<<"]"<< std::endl;
}
}
return 0;
}
- Make sure to always add a decimal to double types to avoid any strange type conversion.
double r = pow(2.0, 1/6);
- Leverage the power of typedef. We can make typing more maneageable by defining a type such as a vector or array you will be working with a descriptive name.
typedef std::array<int, 8> Particles;
typedef std::vector<Particles> Coords;
Performance
It needs to be said, we will have more details tomorrow when we finish the mcsim package, but what became very evident is that c++ is oh so fast.