Skip to content

Commit

Permalink
add memoization for b and c values
Browse files Browse the repository at this point in the history
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jul 7, 2017
1 parent 1d933fa commit c41b23a
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions MechJeb2/MechJebModuleAscentPEG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,21 @@ private void SetNumStages(int n)
}
}

private void DirtyCacheForStage(int snum)
{
for(int i = 0; i <= num_stages; i++) {
stages[snum].b_dirty[i] = stages[snum].c_dirty[i] = true;
}
}

private void InitConstantCache()
{
for(int i = 0; i < num_stages; i++) {
stages[i].b = new double[num_stages+1];
stages[i].c = new double[num_stages+1];
stages[i].b_dirty = new bool[num_stages+1];
stages[i].c_dirty = new bool[num_stages+1];
for(int j = 0; i <= num_stages; j++) {
stages[i].b_dirty[j] = stages[i].c_dirty[j] = true;
}
DirtyCacheForStage(i);
}
}

Expand Down Expand Up @@ -299,6 +304,8 @@ void UpdateStageStats()
stages.RemoveAt(i);
}
}
// dirty the constant cache for the bottom stage because v_e and such is live and may be changing (even exoatmospheric due to Agathorn)
DirtyCacheForStage(0);
}

private double smaT() {
Expand Down Expand Up @@ -328,24 +335,44 @@ void UpdateStageStats()
stages[0].h = Vector3.Cross(mainBody.position, vessel.obt_velocity).magnitude;
}

/* FIXME: some memoization */
private double b(int n, int snum)
{
StageInfo stage = stages[snum];

if (!stage.b_dirty[n])
return stage.b[n];

double ret;

if (n == 0)
return stage.dV;
ret = stage.dV;
else
ret = b(n-1, snum) * stage.tau - stage.v_e * MuUtils.IntPow(stage.T, n) / n;

stage.b[n] = ret;
stage.b_dirty[n] = false;

return b(n-1, snum) * stage.tau - stage.v_e * MuUtils.IntPow(stage.T, n) / n;
return ret;
}

/* FIXME: some memoization */
private double c(int n, int snum)
{
StageInfo stage = stages[snum];

if (!stage.c_dirty[n])
return stage.c[n];

double ret;

if (n == 0)
return b(0, snum) * stage.T - b(1, snum);
ret = b(0, snum) * stage.T - b(1, snum);
else
ret = c(n-1, snum) * stage.tau - stage.v_e * MuUtils.IntPow(stage.T, n+1) / ( n * (n + 1 ) );

return c(n-1, snum) * stage.tau - stage.v_e * MuUtils.IntPow(stage.T, n+1) / ( n * (n + 1 ) );
stage.c[n] = ret;
stage.c_dirty[n] = false;

return ret;
}

private double Alpha(int snum)
Expand Down Expand Up @@ -497,6 +524,9 @@ private void peg_estimate(int snum)
/* updated estimate of T */
T = stage.T = tau * ( 1 - Math.Exp( - stage.dV / v_e ) );

/* now that we have the updated T and dV values, dirty the upper stages constant cache */
DirtyCacheForStage(snum);

total_T = 0.0;
for(int i = 0; i < num_stages; i++)
{
Expand Down

0 comments on commit c41b23a

Please sign in to comment.