Skip to content

Commit

Permalink
Merge pull request #41 from rahil-makadia/dev
Browse files Browse the repository at this point in the history
logistical updates to pip manifest; pointer cleanup
  • Loading branch information
rahil-makadia authored Feb 19, 2024
2 parents 7e67d39 + c9e32bd commit db065f0
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 61 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ recursive-include tests *
include CMakeLists.txt

include grss/version.txt
include build_cpp.sh
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Build Sphinx docs)](https://github.com/rahil-makadia/grss/actions/workflows/docs.yml/badge.svg)](https://github.com/rahil-makadia/grss/actions/workflows/docs.yml)
[![Python tests)](https://github.com/rahil-makadia/grss/actions/workflows/python_tests.yml/badge.svg)](https://github.com/rahil-makadia/grss/actions/workflows/python_tests.yml)
[![C++ tests)](https://github.com/rahil-makadia/grss/actions/workflows/cpp_tests.yml/badge.svg)](https://github.com/rahil-makadia/grss/actions/workflows/cpp_tests.yml)
[![GPL](https://img.shields.io/badge/license-GPL-green.svg?style=flat)](https://github.com/rahil-makadia/grss/blob/main/LICENSE)
[![MIT](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://github.com/rahil-makadia/grss/blob/main/LICENSE)

**GRSS** (pronounced "grass"), the *Gauss-Radau Small-body Simulator* is a Python package with a C++ binding for propagating and fitting the orbits of small bodies in the solar system, such as asteroids and comets.

Expand Down
2 changes: 1 addition & 1 deletion grss/fit/fit_optical.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get_optical_data(body_id, de_kernel_path, optical_obs_file=None, t_min_tdb=N
else:
with open(optical_obs_file, 'r', encoding='utf-8') as file:
obs_raw = file.readlines()
obs_raw = [x[:-1] for x in obs_raw] # remove \n at the end of each line
obs_raw = [line.rstrip() for line in obs_raw] # remove \n at the end of each line
obs_array_optical = np.zeros((len(obs_raw), 6))
star_catalog_codes = []
observer_codes_optical = []
Expand Down
4 changes: 3 additions & 1 deletion grss/prop/prop_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,12 @@ def data_to_ellipse(x_data, y_data, n_std, plot_offset, bplane_type,
if print_ellipse_params:
sma_print = sma/n_std
smi_print = smi/n_std
print(f'{bplane_type} ellipse mean: ({x_mean}, {y_mean}) {units}')
if bplane_type == 'Impact':
# convert lat/lon to km
sma_print *= np.pi/180*6378.1367
smi_print *= np.pi/180*6378.1367
units = "km"
print(f'{bplane_type} ellipse sma: {sma_print} {units}')
print(f'{bplane_type} ellipse smi: {smi_print} {units}')
print(f'{bplane_type} ellipse theta: {theta*180/np.pi} deg')
Expand Down Expand Up @@ -643,7 +645,7 @@ def plot_earth_impact(impact_list, print_ellipse_params=False, sigma_points=None

impact_ell = data_to_ellipse(lon, lat, n_std=3.0, plot_offset=False, bplane_type='Impact',
print_ellipse_params=print_ellipse_params,
units='km', sigma_points=sigma_points)
units='deg', sigma_points=sigma_points)

plt.figure(figsize=(12, 6), dpi=150)
plt.subplot(1, 2, 1)
Expand Down
2 changes: 1 addition & 1 deletion grss/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.0
3.1.0
6 changes: 4 additions & 2 deletions include/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ struct BPlaneParameters {
real x;
real y;
real z;
std::vector<real> dx = std::vector<real>(6, 0.0L);
std::vector<real> dy = std::vector<real>(6, 0.0L);
};

class CloseApproachParameters {
Expand Down Expand Up @@ -136,6 +138,8 @@ class CloseApproachParameters {
BPlaneParameters opik;
BPlaneParameters scaled;
BPlaneParameters mtp;
std::vector<real> dTLinMinusT = std::vector<real>(6, 0.0L);
std::vector<real> dt = std::vector<real>(6, 0.0L);
void get_ca_parameters(propSimulation *propSim, const real &tMap);
void print_summary(int prec=8);
};
Expand Down Expand Up @@ -171,8 +175,6 @@ void deg_to_rad(const real &deg, real &rad);
real deg_to_rad(const real deg);

void sort_vector(std::vector<real> &v, const bool &ascending);
void sort_vector_by_another(std::vector<real> &v, const std::vector<real> &vRef,
const bool &ascending);
void sort_vector_by_another(std::vector<std::vector<real>> &v,
const std::vector<real> &vRef,
const bool &ascending);
Expand Down
216 changes: 203 additions & 13 deletions src/approach.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,10 @@ void CloseApproachParameters::get_ca_parameters(propSimulation *propSim, const r
this->scaled.z = this->kizner.z / this->gravFocusFactor;
real posDotVel;
vdot(pos, vel, 3, posDotVel);
const real F = -asinh(vInf * posDotVel / mu / e);
const real sinhF = vInf*posDotVel/mu/e;
const real F = asinh(sinhF); // or F = -log(2*r/a/e);
const real n = sqrt(-mu / a / a / a);
this->tPeri = tMap - (vInf * posDotVel / mu - F) / n;
this->tPeri = tMap + (F - e*sinhF)/n;
this->tLin = this->tPeri - log(e) / n;
// calculate B-plane parameters (Öpik xi, zeta formulation)
double xCentralBody[9];
Expand All @@ -365,25 +366,214 @@ void CloseApproachParameters::get_ca_parameters(propSimulation *propSim, const r
vunit(vCentralBodyHelioCrossSHat, 3, xiHat);
vcross(sHat, xiHat, zetaHat);
for (size_t k = 0; k < 3; k++) {
zetaHat[k] = -zetaHat[k];
zetaHat[k] *= -1;
}
vdot(bVec, xiHat, 3, this->opik.x);
vdot(bVec, zetaHat, 3, this->opik.y);
vdot(bVec, sHat, 3, this->opik.z);
pos[0] = this->xRel[0];
pos[1] = this->xRel[1];
pos[2] = this->xRel[2];
vel[0] = this->xRel[3];
vel[1] = this->xRel[4];
vel[2] = this->xRel[5];
real posCA[3], velCA[3];
posCA[0] = this->xRel[0];
posCA[1] = this->xRel[1];
posCA[2] = this->xRel[2];
velCA[0] = this->xRel[3];
velCA[1] = this->xRel[4];
velCA[2] = this->xRel[5];
real eHatX[3], eHatY[3], eHatZ[3], vVecCrosseHatZ[3];
vunit(vel, 3, eHatZ);
vunit(velCA, 3, eHatZ);
vcross(vVec, eHatZ, vVecCrosseHatZ);
vunit(vVecCrosseHatZ, 3, eHatY);
vcross(eHatY, eHatZ, eHatX);
vdot(pos, eHatX, 3, this->mtp.x);
vdot(pos, eHatY, 3, this->mtp.y);
vdot(pos, eHatZ, 3, this->mtp.z);
vdot(posCA, eHatX, 3, this->mtp.x);
vdot(posCA, eHatY, 3, this->mtp.y);
vdot(posCA, eHatZ, 3, this->mtp.z);

if (tMap == this->t && propSim->integBodies[i].propStm == true){
real **partial_r_vec = new real*[6];
real **partial_v_vec = new real*[6];
for (size_t k = 0; k < 6; k++) {
partial_r_vec[k] = new real[3];
partial_r_vec[k][0] = 0;
partial_r_vec[k][1] = 0;
partial_r_vec[k][2] = 0;
partial_v_vec[k] = new real[3];
partial_v_vec[k][0] = 0;
partial_v_vec[k][1] = 0;
partial_v_vec[k][2] = 0;
}
partial_r_vec[0][0] = 1;
partial_r_vec[1][1] = 1;
partial_r_vec[2][2] = 1;
partial_v_vec[3][0] = 1;
partial_v_vec[4][1] = 1;
partial_v_vec[5][2] = 1;

real alpha = vInf*vInf;
real *partial_alpha = new real[6];
real *partial_vInf = new real[6];
real *partial_a = new real[6];
real *partial_n = new real[6];
real temp1, temp2, rCA, rCA3, vCA;
vnorm(posCA, 3, rCA);
vnorm(velCA, 3, vCA);
rCA3 = rCA*rCA*rCA;
for (size_t k = 0; k < 6; k++) {
vdot(velCA, partial_v_vec[k], 3, temp1);
vdot(posCA, partial_r_vec[k], 3, temp2);
partial_alpha[k] = 2*(temp1 + mu*temp2/rCA3);
partial_vInf[k] = partial_alpha[k]/(2*vInf);
partial_a[k] = -a*partial_alpha[k]/alpha;
partial_n[k] = -3*n*partial_a[k]/2/a;
}

real **partial_hVec = new real*[6];
real *partial_h = new real[6];
for (size_t k = 0; k < 6; k++) {
partial_hVec[k] = new real[3];
}
real temp1Vec[3];
for (size_t k = 0; k < 6; k++) {
vcross(partial_r_vec[k], velCA, partial_hVec[k]);
vcross(posCA, partial_v_vec[k], temp1Vec);
for (size_t k2 = 0; k2 < 3; k2++) {
partial_hVec[k][k2] += temp1Vec[k2];
}
vdot(partial_hVec[k], hVec, 3, partial_h[k]);
partial_h[k] /= h;
}

real **partial_eVec = new real*[6];
real *partial_e = new real[6];
for (size_t k = 0; k < 6; k++) {
partial_eVec[k] = new real[3];
}
for (size_t k = 0; k < 6; k++) {
vcross(partial_v_vec[k], hVec, temp1Vec);
for (size_t k2 = 0; k2 < 3; k2++) {
partial_eVec[k][k2] = temp1Vec[k2] / mu;
}
vcross(velCA, partial_hVec[k], temp1Vec);
for (size_t k2 = 0; k2 < 3; k2++) {
partial_eVec[k][k2] += temp1Vec[k2] / mu;
}
for (size_t k2 = 0; k2 < 3; k2++) {
partial_eVec[k][k2] -= partial_r_vec[k][k2] / r;
}
vdot(posCA, partial_r_vec[k], 3, temp1Vec[0]);
for (size_t k2 = 0; k2 < 3; k2++) {
partial_eVec[k][k2] += temp1Vec[0] * posCA[k2] / (r * r * r);
}
vdot(partial_eVec[k], eVec, 3, partial_e[k]);
partial_e[k] /= e;
}

real *partial_F = new real[6];
for (size_t k = 0; k < 6; k++) {
vdot(posCA, partial_v_vec[k], 3, temp1);
vdot(velCA, partial_r_vec[k], 3, temp2);
partial_F[k] = (posDotVel*partial_vInf[k]/mu + vInf*(temp1+temp2)/mu - partial_e[k]*sinhF)/e/cosh(F);
}
std::vector<real> xCA = propSim->interpolate(tMap);
std::vector<real> accCA = get_state_der(tMap, xCA, propSim);
real accRel[3], accPlanet[3];
if (j < propSim->integParams.nInteg) {
accPlanet[0] = propSim->integBodies[j].acc[0];
accPlanet[1] = propSim->integBodies[j].acc[1];
accPlanet[2] = propSim->integBodies[j].acc[2];
} else {
accPlanet[0] = propSim->spiceBodies[j - propSim->integParams.nInteg].acc[0];
accPlanet[1] = propSim->spiceBodies[j - propSim->integParams.nInteg].acc[1];
accPlanet[2] = propSim->spiceBodies[j - propSim->integParams.nInteg].acc[2];
}
for (size_t k = 0; k < 3; k++) {
accRel[k] = propSim->integBodies[i].acc[k] - accPlanet[k];
}
real posDotAcc, posCADotVelCA;
vdot(posCA, accRel, 3, posDotAcc);
vdot(posCA, velCA, 3, posCADotVelCA);

for (size_t k = 0; k < 6; k++) {
this->dTLinMinusT[k] = (r*partial_F[k]/a - (e*sinhF + 1)*partial_e[k]/e - (tLin-tMap)*partial_n[k])/n;
vdot(posCA, partial_v_vec[k], 3, temp1);
vdot(velCA, partial_r_vec[k], 3, temp2);
this->dt[k] = -(temp1+temp2)/(posDotAcc + vCA*vCA);
}

real **partial_pHat = new real*[6];
real **partial_qHat = new real*[6];
real **partial_rHat = new real*[6];
real **partial_sHat = new real*[6];
real **partial_tHat = new real*[6];
real temp2Vec[3];
for (size_t k = 0; k < 6; k++) {
partial_pHat[k] = new real[3];
partial_qHat[k] = new real[3];
partial_rHat[k] = new real[3];
partial_sHat[k] = new real[3];
partial_tHat[k] = new real[3];
for (size_t k2 = 0; k2 < 3; k2++) {
partial_pHat[k][k2] = partial_eVec[k][k2]/e - partial_e[k]*eVec[k2]/e/e;
}
vcross(partial_hVec[k], pHat, temp1Vec);
vcross(hVec, partial_pHat[k], temp2Vec);
for (size_t k2 = 0; k2 < 3; k2++) {
partial_qHat[k][k2] = (temp1Vec[k2] + temp2Vec[k2] - partial_h[k]*qHat[k2])/h;
}
for (size_t k2 = 0; k2 < 3; k2++) {
partial_sHat[k][k2] = -partial_e[k]*pHat[k2]/e/e + partial_pHat[k][k2]/e + partial_e[k]*qHat[k2]/e/e/sqrt(e*e-1) + sqrt(e*e-1)*partial_qHat[k][k2]/e;
}
vcross(vVec, partial_sHat[k], temp1Vec);
vdot(tHat, temp1Vec, 3, temp1);
vcross(vVec, sHat, temp2Vec);
vnorm(temp2Vec, 3, temp2);
for (size_t k2 = 0; k2 < 3; k2++) {
partial_tHat[k][k2] = (temp1Vec[k2] - temp1*tHat[k2])/temp2;
}
vcross(partial_sHat[k], tHat, temp1Vec);
vcross(sHat, partial_tHat[k], temp2Vec);
for (size_t k2 = 0; k2 < 3; k2++) {
partial_rHat[k][k2] = temp1Vec[k2] + temp2Vec[k2];
}
}
real *partial_lambda = new real[6];
for (size_t k = 0; k < 6; k++) {
vdot(tHat, partial_hVec[k], 3, temp1);
vdot(hVec, partial_tHat[k], 3, temp2);
this->kizner.dx[k] = (temp1 + temp2 - this->kizner.x*partial_vInf[k])/vInf;
vdot(rHat, partial_hVec[k], 3, temp1);
vdot(hVec, partial_rHat[k], 3, temp2);

partial_lambda[k] = -2*mu*partial_vInf[k]/this->gravFocusFactor/radius/vInf/vInf/vInf;
this->kizner.dy[k] = -(temp1 + temp2 + this->kizner.y*partial_vInf[k])/vInf;
this->scaled.dx[k] = (this->kizner.dx[k] - this->scaled.x*partial_lambda[k])/this->gravFocusFactor;
this->scaled.dy[k] = (this->kizner.dy[k] - this->scaled.y*partial_lambda[k])/this->gravFocusFactor;
}
// clean up
for (size_t k = 0; k < 6; k++) {
delete[] partial_r_vec[k];
delete[] partial_v_vec[k];
delete[] partial_hVec[k];
delete[] partial_eVec[k];
delete[] partial_pHat[k];
delete[] partial_qHat[k];
delete[] partial_rHat[k];
delete[] partial_sHat[k];
delete[] partial_tHat[k];
}
delete[] partial_alpha;
delete[] partial_vInf;
delete[] partial_a;
delete[] partial_n;
delete[] partial_hVec;
delete[] partial_h;
delete[] partial_e;
delete[] partial_F;
delete[] partial_pHat;
delete[] partial_qHat;
delete[] partial_rHat;
delete[] partial_sHat;
delete[] partial_tHat;
delete[] partial_lambda;
}
}

void CloseApproachParameters::print_summary(int prec){
Expand Down
36 changes: 33 additions & 3 deletions src/elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,10 @@ void get_elements_partials(const real &epochMjd, const std::vector<real> &elems,
const real GM) {
real elemTol = 1e-14;
// only valid conversion is com2cart or kep2cart
real e, q, tp, om, w, i, a, nu, E, M;
real e, q, om, w, i, a, nu, E, M;
if (conversion == "com2cart") {
e = elems[0];
q = elems[1];
tp = elems[2];
om = elems[3];
w = elems[4];
i = elems[5];
Expand All @@ -261,7 +260,6 @@ void get_elements_partials(const real &epochMjd, const std::vector<real> &elems,
nu = elems[5];
E = 2 * atan(sqrt((1 - e) / (1 + e)) * tan(nu / 2));
M = E - e * sin(E);
tp = epochMjd - M * sqrt(a * a * a) / GM;
} else {
throw std::invalid_argument("get_cartesian_partials: invalid conversion "
"type, must be com2cart or kep2cart");
Expand Down Expand Up @@ -590,6 +588,38 @@ void get_elements_partials(const real &epochMjd, const std::vector<real> &elems,
partials[5][i] = partial_nu[i];
}
}

// clean up
delete[] pos;
delete[] vel;
delete[] h_vec;
delete[] n_vec;
delete[] e_vec;
for (size_t i = 0; i < 6; i++) {
delete[] partial_r_vec[i];
delete[] partial_v_vec[i];
delete[] partial_h_vec[i];
delete[] partial_n_vec[i];
delete[] partial_e_vec[i];
}
delete[] partial_r_vec;
delete[] partial_v_vec;
delete[] partial_h_vec;
delete[] partial_n_vec;
delete[] partial_e_vec;
delete[] partial_r_mag;
delete[] partial_h_mag;
delete[] partial_n_mag;
delete[] partial_e_mag;
delete[] partial_a;
delete[] partial_q;
delete[] partial_nu;
delete[] partial_E;
delete[] partial_M;
delete[] partial_tp;
delete[] partial_Omega;
delete[] partial_w;
delete[] partial_i;
}

void get_cartesian_partials(const real &epochMjd,
Expand Down
Loading

0 comments on commit db065f0

Please sign in to comment.