-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (127 loc) · 5.84 KB
/
Copy pathmain.cpp
File metadata and controls
170 lines (127 loc) · 5.84 KB
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
// This is a code to simulate dynamics of one-particle systems
// Vikram Jadhao and Vijay G. Jadhao
// June 19, 2026
#include<iostream>
#include<fstream>
#include<vector>
#include"vector3d.h"
#include"particle.h"
const double g = 9.8;
const int planewidth = 8;
const int downshift = -4;
using namespace std;
// make movie
void make_movie(int num, PARTICLE block, PARTICLE& plane, ofstream& outdump)
{
int L = 24;
int a, b;
a = planewidth;
b = planewidth;
double d = cos(M_PI/4);
outdump << "ITEM: TIMESTEP" << endl;
outdump << num << endl; // the time at which movie frame is made
outdump << "ITEM: NUMBER OF ATOMS" << endl;
outdump << 7 << endl; // hard coded
outdump << "ITEM: BOX BOUNDS" << endl;
outdump << -L/2 << "\t" << L/2 << endl;
outdump << -L/2 << "\t" << L/2 << endl;
outdump << -L/2 << "\t" << L/2 << endl;
outdump << "ITEM: ATOMS index type x y" << endl;
PARTICLE P = block;
PARTICLE Q = block;
Q.position = block.position + VECTOR3D(d,-d,0);
PARTICLE R = block;
R.position = block.position + VECTOR3D(d,d,0);
PARTICLE S = block;
S.position = block.position + VECTOR3D(2*d,0,0);
//outdump << 1 << " " << block.ty << " " << block.position.x << " " << block.position.y << endl;
outdump << 1 << " " << P.ty << " " << P.position.x << " " << P.position.y << endl;
outdump << 2 << " " << Q.ty << " " << Q.position.x << " " << Q.position.y << endl;
outdump << 3 << " " << R.ty << " " << R.position.x << " " << R.position.y << endl;
outdump << 4 << " " << S.ty << " " << S.position.x << " " << S.position.y << endl;
PARTICLE A = plane;
PARTICLE B = plane;
B.position = plane.position + VECTOR3D(0,b,0);
PARTICLE C = plane;
C.position = plane.position + VECTOR3D(a,0,0);
//outdump << 2 << " " << plane.ty << " " << plane.position.x << " " << plane.position.y << endl;
outdump << 5 << " " << A.ty << " " << A.position.x << " " << A.position.y << endl;
outdump << 6 << " " << B.ty << " " << B.position.x << " " << B.position.y << endl;
outdump << 7 << " " << C.ty << " " << C.position.x << " " << C.position.y << endl;
}
void compute_energy(PARTICLE& block, PARTICLE& plane, double theta)
{
// pe of the block
block.pe = block.m * g * block.position.y;
plane.pe = 0;
}
void compute_force(PARTICLE& block, PARTICLE& plane, double theta)
{
double r = block.m/plane.m; // ratio of block mass and plane mass
r = 0;
// force for the block
block.force.x = block.m * g * cos(theta) * sin(theta) / (1 + r * sin(theta) * sin(theta));
block.force.y = -block.m * g * (1 + r) * sin(theta) * sin(theta) / (1 + r * sin(theta) * sin(theta));
// force on the plane
plane.force.x = -plane.m * r * g * cos(theta) * sin(theta) / (1 + r * sin(theta) * sin(theta));
}
int main(int argc, char* argv[])
{
cout << "Simulating block plane dynamics" << endl;
cout << "Set up initial conditions" << endl;
double m = 1;
double M = 1;
//double k = 1; // spring constant
//cout << "enter theta" << endl;
double theta = M_PI / 4;
//cin >> theta;
// simulating the block-spring system
PARTICLE block = PARTICLE(m,1,VECTOR3D(1,planewidth-1+downshift,0),VECTOR3D(0,0,0));
PARTICLE plane = PARTICLE(M,2,VECTOR3D(0,downshift,0),VECTOR3D(0,0,0));
// initial force on the block
compute_force(block,plane,theta);
cout << "mass of the block is " << block.m << endl;
cout << "initial position of the block is " << block.position.x << endl;
cout << "initial velocity of the block is " << block.velocity.x << endl;
cout << "initial force on the block is " << block.force.x << endl;
cout << "inclined plane angle " << theta * 180 / M_PI << endl;
block.kinetic_energy();
plane.kinetic_energy();
compute_energy(block,plane,theta);
// writing files to store data
char filename_block[200];
sprintf(filename_block, "energyMinf.out");
ofstream block_result(filename_block, ios::out);
ofstream list_particles("movieMinf.out", ios::out); // create a file to store and visualize 3D data
// start by recording the initial condition (state) of the system
block_result << 0 << " " << block.ke << " " << block.pe << " " << block.ke + block.pe << " " << plane.ke << " " << plane.pe << " " << plane.ke + plane.pe << " " << block.ke + block.pe + plane.ke + plane.pe << endl;
make_movie(0,block,plane,list_particles);
double t = 2; // total duration of time for which dynamics is desired
int S = 2000;
double dt = t/S; // dt = 20/20000 = 0.001
int freq = 10;
// simulation
for (int num = 1; num < S; num++)
{
// update velocity (half step)
block.update_velocity(dt/2);
plane.update_velocity(dt/2);
// update position
block.update_position(dt);
plane.update_position(dt);
// recalculate force
compute_force(block,plane,theta);
// update velocity again
block.update_velocity(dt/2);
plane.update_velocity(dt/2);
// useful to collect energy information
block.kinetic_energy();
plane.kinetic_energy();
compute_energy(block,plane,theta);
// file the simulation updates
block_result << num*dt << " " << block.ke << " " << block.pe << " " << block.ke + block.pe << " " << plane.ke << " " << plane.pe << " " << plane.ke + plane.pe << " " << block.ke + block.pe + plane.ke + plane.pe << endl;
if (num%freq == 0)
make_movie(num,block,plane,list_particles);
}
return 0;
}