Program to plot Trajectory of an electron in a H-2 ion in c++?

May 2, 2012 at 4:47pm
Hi, I have to write a program to plot the trajectory of the electron in the Hydrogen (H2) ion. I have a list of steps that i need to follow but i absolutely suck at programming and if i fail this then i dont get my degree, which is freaking me out.

Step1: assign values to:
i) The charge of the two protons (Q1, Q2) and the electron e.
ii) the mass of e.
iii) the constant k ( in coulombs law)
iv) the distance 2d, betweeen protons in the molecule.
v) the step-size h.

Step 2:
i) assign initial values to x,y, and Vx and Vy for the e.

step 3:
i) calculate the x and y components of the acceleration Ax and Ay (this i can do because its pure maths)

step4)
find the half-step values for Vx and Vy.

step 5:
loop until a key is pressed.

step 6:
plot the point (x,y) on the Trajectory of the electron

step 7:
update the positions.

step 8:
calc the acceleration (using formula's which i havent given [i can input them mysef])

step 9:
update velocities.

step 10.
increment the time.

step11
end of loop begun in step 5 (i.e loop until key pressed)

step 12: QUIT

I have all the values that i need to assign but despite the stpes i dont even know where to begin.
ANY HELP AT ALL IS APPRECIATED!!!!

you can e-mail me if you want at: al-inglis@hotmail.com
P.s im using a program called LABWINDOWS to actually plot the trajectory.
May 2, 2012 at 9:23pm
You might be in some trouble if you suck at programming. To make a dynamic, robust program for this is not easy. But as a nuclear engineer and Geant4 developer, I like the problem and I like to see people in nuclear/physics using C++ instead of FORTRAN.

I will provide you with structure to get the info into a format you can handle all the computations with because I think thats the real pain and I already had more of the below code. I won't go over how to do all the steps because you should be able to figure that out.

1
2
3
!!!!!!  If you are unsure of how to access some of these variables, look at how I did it in other parts of the code. 

TRY THINGS, it is how you learn to code.


For the hit key thing, if you have Windows, you can use something like:
1
2
3
4
5
#include <conio.h>
while(!kbhit())
{
  // do something
}

here's a link about it:
http://forums.codeguru.com/showthread.php?t=370813

I do not think there is a equivalent for Mac because it doesn't include conio.h since it is not part of the STL library, so I don't have anything to recommend there.

otherwise the code would look something like this:
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197


#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <map> 
#include <sstream>
#include <ostream>

std::string MakeLower(std::string);

// ============================================================================
// For handling vectors in 3 dimension coordinates
struct ThreeVector
{
    double fx;   // x component
    double fy;   // y component
    double fz;   // z component
    
    // for initialization, e.g. ThreeVector(0),
    // ThreeVector(),ThreeVector(1,2,3)
    ThreeVector():fx(0),fy(0),fz(0) {} 
    ThreeVector(double val):fx(val),fy(val),fz(val) {}
    ThreeVector(double xv, double yv, double zv)
    : fx(xv),fy(yv),fz(zv) {}
    
    // return functions
    double x() { return fx; }
    double y() { return fy; }
    double z() { return fz; }
    
};

// ============================================================================
// Particle data, you can add to this as needed
struct Particle
{
    std::string type;   // proton, alpha, etc
    int charge;         // -1,0,+1, etc
    double mass;        // choose units
    ThreeVector position;
    ThreeVector velocity;
    ThreeVector acceleration;
    
    // initial data of declared particle
    Particle():type(""),charge(0),mass(0),position(0),
    velocity(0),acceleration() {}
};

// ============================================================================
// Predeclaration of functions used after main but used in main (required)
std::vector<std::string> DelimitLine(std::string,char);
void PrintThreeVector(ThreeVector,std::ostream&);
void PrintParticleData(std::vector<Particle>,std::ostream&);
double StringToDouble(std::string);
int StringToInt(std::string);

// ============================================================================

// Assign the value of k here, along with any other global variables you want
//const double k = ...;

// ============================================================================
// The main driver function
int main(int argc, char* argv[])
{
    std::ifstream in;
    // check to make sure there is an input file
    if(argc < 2) { 
        std::cout << argv[0] << " needs an input file." << std::endl;
        exit(0);
    } else {
        in.open(argv[1]);
        if(!in) {
            std::cout << "Could not open " << argv[1] << std::endl; 
            exit(0);
        }
    }
    
    // map for storing particle data
    std::map<std::string,std::vector<Particle> > particles;
    std::string line;
    while(!in.eof()) {
        getline(in,line,'\n'); // put entire line into one string
        // Below sorts line from above using spaces as delimited
        // can replace with commas by ',' and changing input file
        std::vector<std::string> delimitedLine = DelimitLine(line,' ');
        // declare particle variable
        Particle particle;
        if(delimitedLine.size() >= 3) {
            // access/assign struct particle data with . and variable
            particle.type = delimitedLine.at(0);
            particle.charge = StringToInt(delimitedLine.at(1));
            particle.mass = StringToDouble(delimitedLine.at(2));
        } else if(line.size() == 0) {
            continue;
        } else {
            std::cout << "Line: " << line 
            << " doesn't have enough parameters"
            << std::endl;
        }
        
        if(delimitedLine.size() == 7) {
            particle.position.fx = StringToDouble(delimitedLine.at(3));
            particle.position.fy = StringToDouble(delimitedLine.at(4));
            particle.velocity.fx = StringToDouble(delimitedLine.at(5));
            particle.velocity.fy = StringToDouble(delimitedLine.at(6));
        }
        particles[delimitedLine.at(0)].push_back(particle);
    }
    
    // for output
    std::map<std::string,std::vector<Particle> >::iterator ite;
    for(ite = particles.begin(); ite != particles.end(); ++ite) {
        // print to screen
        PrintParticleData(ite->second,std::cout);
    }
    
    // here is where you want to do all your computations
    // handle the data using the map iterator
    
    // for maps, you access the first part (the key) with ->first
    // -> is similar to . except the arrow is for pointers (i.e. the ite variable
    // is "pointing to" the element it is on.
    // and the second part (the data stored according to key) with ->second
    
    // for vector iterators, see PrintParticleData function
    
    
    return 0;
}

// ============================================================================

void PrintThreeVector(ThreeVector three, std::ostream& out)
{
    out << three.x() << " " << three.y() << " " << three.z() << " ";
}
// ============================================================================

void PrintParticleData(std::vector<Particle> p, std::ostream& out)
{
    std::vector<Particle>::iterator ite;
    for(ite = p.begin(); ite != p.end(); ++ite) {
        out << (*ite).type << " " << (*ite).charge << " "
        << (*ite).mass << " ";
        PrintThreeVector((*ite).position,out);  // print position data
        PrintThreeVector((*ite).velocity,out);  // print velocity data
        PrintThreeVector((*ite).acceleration,out);  // print accel data
        out << std::endl;
    }
}

// ============================================================================

std::vector<std::string> DelimitLine(std::string line, char delimiter)
{
    std::vector<std::string> delimitedLine;
    std::string token;
    std::istringstream iss(line);
    while( getline(iss,token,delimiter) ) {
        if( !token.empty() ) { delimitedLine.push_back(token); }
    }
    return delimitedLine;
}

// ============================================================================

std::string MakeLower(std::string str)
{
    for(std::string::size_type i = 0; i < str.size(); ++i) {
        str[i] = tolower(str[i]);
    }
}

// ============================================================================

int StringToInt(std::string str)
{
    std::istringstream converter(str);
    int result;
    converter >> result;
    return result;
}

// ============================================================================

double StringToDouble(std::string str)
{
    std::istringstream converter(str);
    double result;
    converter >> result;
    return result;
}



The input file would like this:
1
2
3
electron -1 0.511 5.6 4.7 22.5 44.1
proton 1 931.5 
proton 1 931.5


the output looks like this:
1
2
3
electron -1 0.511 5.6 4.7 0 22.5 44.1 0 0 0 0 
proton 1 931.5 0 0 0 0 0 0 0 0 0 
proton 1 931.5 0 0 0 0 0 0 0 0 0 
May 3, 2012 at 3:49pm
Nice one. Thank you so much for going through the trouble to do this. Im still working on it So i'll let you know how it all turns out!
Topic archived. No new replies allowed.