Integrating external programs
In this tutorial, we show how to call an external executable from SAMSON, in order to perform calculations based on selected atoms in the document. Setting up the external executable Implement the following program, and compile it to produce an executable called Barycenter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <sstream> #include <fstream> #include <string> #include <iostream> int main() { unsigned int numberOfAtoms; // open the file std::ifstream input("input.txt"); // read the number of atoms std::string line; std::getline(input, line); std::stringstream firstLineParser(line); firstLineParser >> numberOfAtoms; if (!numberOfAtoms) { std::ofstream output("output.txt"); output << "No atoms" << std::endl; output.close(); return 0; } // read the coordinates of the atoms and update the barycenter double x = 0.0; double y = 0.0; double z = 0.0; while (std::getline(input, line)) { std::stringstream lineParser(line); double currentX = 0.0; double currentY = 0.0; double currentZ = 0.0; lineParser >> currentX >> currentY >> currentZ; x += currentX; y += currentY; z += currentZ; } x /= (double)numberOfAtoms; y /= (double)numberOfAtoms; z /= (double)numberOfAtoms; std::ofstream output("output.txt"); output << x << "A " << y << "A " << z << "A" << std::endl; return 0; } |
This program opens a text file named input.txt containing atoms coordinates and creates a file named output.txt that contains the coordinates of the barycenter of the atoms (or the string “No atoms” if the initial fileā¦