Repository navigation
Expand file tree
/
Copy pathproc.c
More file actions
executable file
·85 lines (61 loc) · 2.12 KB
/
Copy pathproc.c
File metadata and controls
executable file
·85 lines (61 loc) · 2.12 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
#include "proc.h"
#include "psim.h"
#include "util.h"
#include "init.h"
#include <math.h>
#include <stdio.h>
#define GRAV_CONST 1.0
#define CHRG_CONST 1.0
struct vector coll_forces(struct particle p) {
struct vector res = {0.0, 0.0};
for(int i = 0; i < MAX_FORCES; i++) {
struct vector force = p.forces[i];
res = vsum(res, force);
}
return res;
}
typedef double (*force_coeff_fn)(struct particle, struct particle);
struct vector calc_func_force(struct particle p, struct universe* uverse, force_coeff_fn coeff) {
struct vector res = vc_zero;
for(int i = 0; i < uverse->pcount; i++) {
struct particle t = uverse->particles[i];
// F = coeff / d^2
struct vector ppos = p.cstate.pos;
struct vector tpos = t.cstate.pos;
double d = dist(ppos, tpos);
if(d == 0) continue;
double lforce = coeff(p, t) / pow(d, 2);
double angc = angcoeff(ppos, tpos);
double ang = atan(angc);
struct vector force;
force.x = (dist_x(ppos, tpos) > 0 ? 1 : -1) * lforce * cos(ang);
force.y = (dist_y(ppos, tpos) > 0 ? 1 : -1) * lforce * sin(ang);
res = vsum(res, force);
}
return res;
}
double c_gravity(struct particle p, struct particle t) {
return GRAV_CONST * p.mass * t.mass;
}
double c_electr(struct particle p, struct particle t) {
return CHRG_CONST * p.charge * t.charge;
}
void advance_state(struct universe* uverse) {
// struct universe before = *uverse;
for(int i = 0; i < uverse->pcount; i++) {
struct particle* p = &uverse->particles[i];
set_force(8, p, calc_func_force(*p, uverse, &c_electr));
set_force(9, p, calc_func_force(*p, uverse, &c_gravity));
struct vector force = coll_forces(*p);
struct state nstate = p->cstate;
nstate.acc = vdiv(force, p->mass); // F = ma => a = F/m
// a(t) = a0
// v(t) = v0 + a0t
// s(t) = s0 + v0t + 1/2a0t^2
nstate.vel = vsum(p->cstate.vel, vmul(nstate.acc, TIME_DELTA));
nstate.pos = vsum3(p->cstate.pos, vmul(p->cstate.vel, TIME_DELTA),
vdiv(vmul(nstate.acc, pow(TIME_DELTA, 2)), 2));
p->cstate = nstate;
}
uverse->instant++;
}