-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn-body.c
94 lines (83 loc) · 3.28 KB
/
n-body.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
/*
* Copyright (c) 2014 - 2016 W.A. Garrett Weaver
*
* This file is part of moon-earth-sun.
*
* moon-earth-sun is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* moon-earth-sun is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along moon-earth-sun. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "n-body.h"
NBodyError CalculateNewPositionAndVelocity(PARTICLE * output, PARTICLE * pointList, UnsignedType elements, UnsignedType pointOfInterest, FloatingType timeStep) {
THREE_VECTOR_FLOAT distanceComponent;
FloatingType distance, acceleration;
UnsignedType i;
//Error checking
if(output == NULL) {
return OUTPUT_NULL;
}
if(pointList == NULL) {
return POINTLIST_NULL;
}
if(pointOfInterest >= elements) {
return POI_NOT_IN_RANGE;
}
if(timeStep <= 0.0f) {
return TIME_STEP_NOT_VALID;
}
*output = pointList[pointOfInterest];
for(i = 0; i < elements; i++) {
distanceComponent.x = pointList[i].position.x - pointList[pointOfInterest].position.x;
distanceComponent.y = pointList[i].position.y - pointList[pointOfInterest].position.y;
distanceComponent.z = pointList[i].position.z - pointList[pointOfInterest].position.z;
distance = sqrt(pow(distanceComponent.x, 2) + pow(distanceComponent.y, 2) + pow(distanceComponent.z, 2));
acceleration = (distance != 0.0f) ? (G * pointList[i].mass) / (pow(distanceComponent.x, 2) + pow(distanceComponent.y, 2) + pow(distanceComponent.z, 2)) : 0;
output->speed.x += (distance != 0.0f) ? ((acceleration) * timeStep) * (distanceComponent.x / distance) : 0;
output->speed.y += (distance != 0.0f) ? ((acceleration) * timeStep) * (distanceComponent.y / distance) : 0;
output->speed.z += (distance != 0.0f) ? ((acceleration) * timeStep) * (distanceComponent.z / distance) : 0;
}
output->position.x += output->speed.x * timeStep;
output->position.y += output->speed.y * timeStep;
output->position.z += output->speed.z * timeStep;
return SUCCESS;
}
const char * ErrorParser(NBodyError errorCode) {
switch (errorCode) {
case SUCCESS:
return "no errors";
break;
case OUTPUT_NULL:
return "destination pointer is null";
break;
case POINTLIST_NULL:
return "particle array is null";
break;
case POI_NOT_IN_RANGE:
return "point of interest is not in range";
break;
case TIME_STEP_NOT_VALID:
return "time step is less than or equal to zero";
break;
default:
return "unknown error";
break;
}
}
const char * PointToString(PARTICLE * input) {
const size_t retStrLen = 1024;
char * outputVal = (char *) malloc(sizeof(char) * retStrLen);
snprintf(outputVal, retStrLen, "position.x: %f, position.y: %f, position.z: %f, speed.x: %f, speed.y %f, speed.z: %f, mass: %f\n", input->position.x, input->position.y, input->position.z, input->speed.x, input->speed.y, input->speed.z, input->mass);
return outputVal;
}