Dear all,
I am newbie in programming language, especially in C++.
My problem is the following:
i would like to read an external .txt file which contains the x,y,z position of each atom in my crystal, and, once defined a range of tolerance, e.g. 10 Angstrom, calculate all the distances between each pair of atoms in the file up to 10 Angstrom.
The problem is not for sure the formula for the distance, but how to say to my small script to iteratively calculate the distances.
Thanks in advance for every suggestions you can provide to me!!
Tommy
P.S.
This is a very rough scripting tentative that I attach here:
but how to say to my small script to iteratively calculate the distances.
I suppose the simplest approach is to store your points in a std::vector. Then I think you need to calculate the distance of each point from each other point.
You might do that with a nested for loop.
Outer loop, simply loop through all of the points.
Inner loop. Loop through all of the points. If the two points are the same item in the vector, do nothing (continue). Calculate the distance between the two points.
You might want to define a simple class to contain the (x, y, z) values. that would simplify much of the other code.
The outline of that part of the code might look something like this :
1 2 3 4 5 6 7 8 9 10 11
for (unsignedint i=0; i<points.size(); i++)
{
for (unsignedint j=0; j<points.size(); j++)
{
if (i == j)
continue;
// calculate distance from points[i] to points[j] here
}
}
Note, I've used the term "point". You might prefer the name "atom", since it relates to your subject matter.
Edit: My suggestion is slightly flawed. The nested loop will generate duplicates because a,b will be considered different from b,a where a and b are points (or atoms).
You have "atoms" (of a molecule?) and you calculate (intermolecular) distances. That naturally produces a symmetric N*N 2D-matrix from set of N atoms (as already noted) and it is sufficient to fill the upper triangle to avoid duplication (and calculating the diagonal -- atom to self distance is always 0).
You mention a limit (and script has two). Do you just want to the distance values that are within limit?
How about the other data?
How many of the distances are within limits?
Which pairs of atoms have interesting distances? Do you want to know which atoms produce those distance?
If you would not need the actual distance, then you would not need to calculate sqrt(), because
-You mention a limit (and script has two). Do you just want to the distance values that are within limit?
Yep, in principle I would like to be able to say to my script: just take into account the distances that are in the range between A and B;
-How about the other data?
-How many of the distances are within limits?
-Which pairs of atoms have interesting distances? Do you want to know which -atoms produce those distance?
My input file is like this one:
x y z
-20.76177 11.85862 3.04062
-35.15550 35.54668 3.07355
It doesn't matter which kind of species I have, is just to calculate all the possible available distances between all the points provided. The problem is that I have some difficulties in defining to my script the general coordinates for each of the two vectors. Is is possible that the general coordinates are defined (in my case) as x.x, x.y, x.z and y.x, y.y. and y.z?
Regards,
T
Thanks everyone for giving the correction to my flawed code - I had some sort of blind-spot and didn't see the solution for a while.
@tommy venice
I don't know how familiar you are with class and structs.
if you define a struct like this:
1 2 3 4 5
struct Atom {
double x;
double y;
double z;
};
Then you can declare objects of that type, such as
1 2
Atom a;
Atom b;
or in your case probably a vector of such objects std::vector<Atom> atoms;
and refer to their members as
a.x, a.y, a.z or b.x, b.y, b.z and so on. An element of the vector would be accessed as atoms[n] and its member variables atoms[n].x, atoms[n].y, atoms[z] etc.
Though such repetitive code can be simplified by means of member functions, constructors etc.
Dear Chervil,
thanks for clarifying me this aspect of the problem. I am not very familiar with classes etc. simply because I am very new on this language code. But I am determined to understand this program language.
By the way now it is more clear to me how it works.
Thanks again.
P.S.
in case I will paste here the piece of code and if you all will correct my mistakes I will appreciate.
Best
T
Are you confined in Windows platform with no access to GNU tools? Your input has one (first) line different from the others. Skipping it outside of the program could be nice.
In your first code sample you do mix C-style I/O and C++-style I/O. That is double the trouble.
Asking questions interactively during runtime is inconvenient for a "number crunching operation", like yours. A program can take and process command-line arguments.