-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework of 2D arrays, implemented vector math library
Vector math library now converts from perifocal frame to ECI frame for propagation
- Loading branch information
Showing
9 changed files
with
354 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# A Note on Dynamic 2D Array Formatting | ||
|
||
Source: https://stackoverflow.com/questions/40847172/best-way-to-allocate-memory-to-a-two-dimensional-array-in-c | ||
|
||
2D arrays are most efficiently stored as a contiguous block, i.e. as a single linear array. | ||
|
||
Therefore, the array | ||
```c | ||
[1, 2, 3] | ||
[4, 5, 6] | ||
[7, 8, 9] | ||
``` | ||
has memory allocated like the following: | ||
`[1, 2, 3, 4, 5, 6, 7, 8, 9]` | ||
|
||
## Array Declaration | ||
Arrays of doubles should be declared like this: | ||
```c | ||
double (*name)[COLUMN_SIZE]; | ||
``` | ||
Wherein `COLUMN_SIZE` is some constant denoting the column length of the array. For Supernova, we use a column length of 6 (x/y/z/vx/vy/vz) | ||
|
||
This way of formatting the array makes it so that the first index of the array yields chunks spaced out by `COLUMN_SIZE`. | ||
|
||
## Array Memory Allocation | ||
Memory allocation can be done like the following: | ||
```c | ||
name = malloc(n * sizeof(double[COLUMN_SIZE])); | ||
``` | ||
Wherein `n` is the number of rows in the array. Similar procedures are done for reallocating memory, if you need to make the array bigger. | ||
|
||
## Accessing Array indices | ||
When the array is initialized like this, you can access it just as you would expect with Python. | ||
```c | ||
printf("%f", name[1][2]); | ||
>>> 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "vecmath.h" | ||
#include <math.h> | ||
|
||
int main() { | ||
// Validation from Mathworks (MATLAB documentation) | ||
|
||
// 1. CROSS PRODUCT | ||
double A1[3] = {4, -2, 1}; | ||
double B1[3] = {1, -1, 3}; | ||
double C1[3]; | ||
cross(A1, B1, C1); | ||
printVec(C1); | ||
// Result: [-5, -11, -2] | ||
|
||
// 2. DOT PRODUCT | ||
printf("%f\n", dot(A1, C1)); | ||
// Result: 0 | ||
printf("%f\n", dot(B1, C1)); | ||
// Result: 0 | ||
|
||
double A2[3] = {4, -1, 2}; | ||
double B2[3] = {2, -2, -1}; | ||
printf("%f\n", dot(A2, B2)); | ||
// Result: 8 | ||
|
||
// 3. Matrices | ||
double A3[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; | ||
double B3[3][3] = {{0, 1, 0}, {1, 0, 0}, {0, 0, 1}}; | ||
double (*C3)[3] = malloc(3 * sizeof(double[3])); | ||
|
||
matXmat(A3, B3, C3); | ||
printMatrix(C3); | ||
|
||
double (*q)[3] = QpX(0, 97.49588373 * PI/180.0, 227.05566156 * PI/180.0); | ||
printMatrix(q); | ||
|
||
// 4. Matrix times Vec | ||
double A4[3][3] = {{-0.681288, -0.095495, -0.725760}, {-0.732016, 0.088877, 0.675465}, {0.000000, 0.991454, -0.130455}}; | ||
double B4[3] = {6877999.998558, 0.000000, 0.000000}; | ||
double C4[3]; | ||
matXvec(A4, B4, C4); | ||
printVec(C4); | ||
|
||
// 5. Orbit alteration | ||
double* Perifocal_pos_vel = PFE(6903e3, 0.003621614, 97.49588373 * PI/180.0, 0, 0); | ||
// Perifocal coordinates | ||
|
||
double ECI_position[3]; | ||
double ECI_vel[3]; | ||
matXvec(q, Perifocal_pos_vel, ECI_position); | ||
matXvec(q, Perifocal_pos_vel+3, ECI_vel); | ||
|
||
printVec(ECI_position); | ||
printVec(ECI_vel); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.