@@ -168,15 +168,75 @@ Next, we move the code that we wrote to do the calculation, that up to now was i
stays inside main, since this code should be called once.
Second. before we declared the `distance` variable. You have to remove that line, because the distance is now passed as an argument to the function and it is not fixed.
Finally, we need to return the value of the energy that we calculated so it can be seen by the code that calls our function, this is done using the `return` statement. So at the end of the function we add
Finally, we need to return the value of the energy that we calculated so it can be seen by the code that calls our function, this is done using the `return` statement. So at the end of the function we add `return result.energy.total();`.
Before, we were printing the value of the energy to the terminal. You can remove the line that did that or if you prefer modify it so that it prints also the distance in the same line.
Okay, so we have defined or new and shiny `nitrogen_energy` function. As you might have already realized, this function *is the potential energy surface* for nitrogen.
We can do many things with this function. For example, we are going to print it to a file so we can plot it.
To access the file input/output capabilities of C++ we need to load the proper include. In this case `fstream`. This is done with
```c++
returnresult.energy.total();
#include<fstream>
```
at the beginning of the file, where the other includes are.
Before, we were printing the value of the energy to the terminal. You can remove the line that did that or if you prefer modify it so that it prints also the distance in the same line.
Now we can use the `ofstream` object that opens a file for writing. As argument we have to pass the name of the file we want to open. For example:
Okay, so we have defined or new and shiny `nitrogen_energy` function. As you might have already realized, this function *is the potential energy surface* for nitrogen.
```c++
std::ofstreamofs{"nitrogen_e_vs_d.dat"};
```
This you have to do in the `main` function.
Now that we have the file opened, let's iterate over some distances and store the resulting energy to the file. This is the code:
We first define the values that determine how we are going to scan the distances. We use 20 values starting from 1.5 atomic units and with a uniform distance of 0.05 atomic units. Note that this is the simplest approach but we can use any distances we want, and we could even use some adaptive scheme, using the previous results to refine the energy surface in the areas near the minimum, for example.
Then we do the loop, iterating over the distances. For each iteration of the loop we print the distance and the energy of the nitrogen molecule for that distance. And that's it.
Now compile the code and run it. This time we are doing 20 calculations so it will take longer than before.
Once the calculation is done, you should have the file `nitrogen_e_vs_d.dat` in your directory. Have a look at the file, you should have two columns data. Now use your favorite plotting program (not MS Paint) to plot the file and look at the curve. Does it look reasonable. Is it what you expected?
**SPOILER ALERT**, it should look something like this: