-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
215 lines (198 loc) · 6.85 KB
/
main.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const char HELP_MESSAGE[] = "Help / Manual: \n"
"A purely mathematical simulator of elastic collisions that simulates the phenomenon described in this "
"3Blue1Brown YouTube Short https://www.youtube.com/shorts/P11ykXwx4-k\n"
"Description of the problem: A small block is at rest on a plane with a vertical wall on the left. "
"A block N times in mass is sliding from the right to the left with some speed, "
"the maximum number of elastic collisions that will ever happen for a scenario will be PI * sqrt(N) and "
"will thus show the digits of PI if N is 100 to some power.\n"
"Source code can be found here: https://github.com/andrei-akopian/PiCollisionsSimulator\n\n" //TODO add github link here
"Usage instructions (Warning! there is no input validation):\n"
"-n: smaller_mass * n = larger_mass\n"
"-c: generate x digits of pi\n"
"-sm: smaller_mass mass\n"
"-lm: larger_mass mass (initally negative, left is negative direction)\n"
"-sv: smaller_mass initial velocity\n"
"-lv: larger_mass initial velocity\n"
"-e: display energy (always the same)\n"
"-m: display momentum (changes)\n"
"-v: display velocity\n"
"-p: display picture/diagram (not to scale)\n"
"-h or --help: displays this unhelpful help message\n"
"Usage example `./a.out -n 10 -v -p`\n";
#define PICTURE_ARROW_MULTIPLIER 3
// Actuall Code
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
struct inputs {
float smaller_mass;
float larger_mass;
float velocity_smaller;
float velocity_larger;
int bool_display_energy;
int bool_display_momentum;
int bool_display_velocity;
int bool_display_pictures;
int bool_display_atall;
};
struct inputs parseCLI(int argc, char *argv[]){
struct inputs ins = {1,1,0,-1,0,0,0,0,0};
int i=1;
while (i<argc){
if (strcmp(argv[i],"-sm")==0){
ins.smaller_mass = atof(argv[i+1]);
i++;
} else if (strcmp(argv[i],"-lm")==0){
ins.larger_mass = atof(argv[i+1]);
i++;
} else if (strcmp(argv[i],"-sv")==0){
ins.velocity_smaller = atof(argv[i+1]);
i++;
} else if (strcmp(argv[i],"-lv")==0){
ins.velocity_larger = atof(argv[i+1]);
i++;
} else if (strcmp(argv[i],"-n")==0) {
ins.larger_mass = ins.smaller_mass*atof(argv[i+1]);
i++;
} else if (strcmp(argv[i],"-c")==0){
ins.larger_mass = ins.smaller_mass*(pow(10,atof(argv[i+1])));
i++;
} else if (strcmp(argv[i],"-e")==0){
ins.bool_display_energy = 1;
} else if (strcmp(argv[i],"-m")==0){
ins.bool_display_momentum = 1;
} else if (strcmp(argv[i],"-v")==0){
ins.bool_display_velocity = 1;
} else if (strcmp(argv[i],"-p")==0){
ins.bool_display_pictures = 1;
} else if (strcmp(argv[i],"-h")==0 || strcmp(argv[i],"--help")==0 ){
printf("%s",HELP_MESSAGE);
exit(0);
}
i++;
}
ins.bool_display_atall = ins.bool_display_energy+ins.bool_display_momentum+ins.bool_display_velocity+ins.bool_display_pictures;
return ins;
};
// Calculations
struct velocity_states {
float velocity_smaller;
float velocity_larger;
};
float calculate_center_of_mass_velocity(struct velocity_states state, struct inputs masses){
float weighted_sum = state.velocity_smaller * masses.smaller_mass + state.velocity_larger * masses.larger_mass;
float sum = masses.smaller_mass + masses.larger_mass;
return weighted_sum/sum;
}
struct velocity_states ellastic_collision (struct velocity_states state, struct inputs masses){
float vc = calculate_center_of_mass_velocity(state, masses);
struct velocity_states new_state = {2*vc-state.velocity_smaller, 2*vc-state.velocity_larger};
return new_state;
};
struct velocity_states bounce_off_wall (struct velocity_states state){
struct velocity_states new_state = {state.velocity_smaller*-1, state.velocity_larger};
return new_state;
};
// Formatting
void print_velocity_state(struct velocity_states state){
printf("%3f %3f\n",state.velocity_smaller, state.velocity_larger);
};
void calculate_energy(struct velocity_states state, struct inputs masses){
float sk = state.velocity_smaller*state.velocity_smaller*masses.smaller_mass/2;
float lk = state.velocity_larger*state.velocity_larger*masses.smaller_mass/2;
printf("Energy: Small mass: %.4f\tBig mass: %.4f\t Total: %f\n",sk,lk,sk+lk);
};
void calculate_momentum(struct velocity_states state, struct inputs masses){
float smom = state.velocity_smaller*masses.smaller_mass;
float lmom = state.velocity_larger*masses.larger_mass;
printf("Momentum: Small mass: %.4f\tBig mass: %.4f\t Total: %f\n",smom,lmom,smom+lmom);
};
void print_block(float v, float vsum){
if (v<0){
printf("<");
} else {
printf("#");
}
for (int i=0; i<(int)(fabsf(v/vsum)*PICTURE_ARROW_MULTIPLIER); i++){
printf("-");
}
if (v<0){
printf("#");
} else if (v>0){
printf(">");
}
};
void print_picture(struct velocity_states state){
float total_v = fabsf(state.velocity_smaller)+fabsf(state.velocity_larger);
printf("| ");
print_block(state.velocity_smaller,total_v);
printf("\t");
print_block(state.velocity_larger,total_v);
printf("\n");
}
void print_state(struct velocity_states state, struct inputs ins, long collision_count){
if (ins.bool_display_atall){
printf("Collision: %ld\n",collision_count);
}
if (ins.bool_display_velocity || collision_count==0){
print_velocity_state(state);
}
if (ins.bool_display_pictures){
print_picture(state);
}
if (ins.bool_display_momentum){
calculate_momentum(state,ins);
}
if (ins.bool_display_energy){
calculate_energy(state,ins);
}
};
void print_inputs(struct inputs ins){
printf("# Transcribed Input Command\n");
printf("Masses:\t%.4f \t %.4f\n",ins.smaller_mass,ins.larger_mass);
printf("Initial Velocities:\t%.4f \t %.4f\n",ins.velocity_smaller,ins.velocity_larger);
if (ins.bool_display_atall){
printf("Display options:\n");
if (ins.bool_display_energy){
printf("\tEnergy\n");
}
if (ins.bool_display_momentum){
printf("\tMomentum\n");
}
if (ins.bool_display_velocity){
printf("\tVelocity\n");
}
if (ins.bool_display_pictures){
printf("\tPictures\n");
}
}
printf("For help use -h option\n\n");
}
int main (int argc, char *argv[]){
//setup
struct inputs ins = parseCLI(argc, argv);
long c = 0; //colision count, shortcuts and bandaid stuff will break if if overflows
struct velocity_states s = {ins.velocity_smaller, ins.velocity_larger};
//initial state print
print_inputs(ins);
//simulation
printf("# Collision States:\n");
int flipper = 1;
while (!(s.velocity_smaller>=0 && s.velocity_larger>0 && s.velocity_smaller < s.velocity_larger)){
print_state(s,ins,c);
if (flipper) {
s = ellastic_collision(s,ins);
flipper =0;
} else {
s = bounce_off_wall(s);
flipper =1;
}
c++;
}
// final sate print
printf("# Final State:\n");
print_state(s,ins,0);
printf("# Total Collision Count:\n%ld\n",c);
return 0;
};