Skip to content

Commit

Permalink
Convert the ArrayWrapper class to a struct
Browse files Browse the repository at this point in the history
- causes more copying but removes the need to memorypool
- weirdly this seems like it fixed a very tiny bug in the Titan2Tests
- split the class up into input/output wrappers
- they're no longer wrappers so i need a better name, TBD

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jun 2, 2023
1 parent 1f961fe commit 26de45d
Show file tree
Hide file tree
Showing 23 changed files with 324 additions and 351 deletions.
3 changes: 2 additions & 1 deletion MechJeb2/MechJeb2.csproj
Expand Up @@ -121,7 +121,8 @@
<Compile Include="MechJebLib\Maneuvers\ReturnFromMoon.cs" />
<Compile Include="MechJebLib\Primitives\Vn.cs" />
<Compile Include="MechJebLib\Primitives\Scale.cs" />
<Compile Include="MechJebLib\PVG\ArrayWrapper.cs" />
<Compile Include="MechJebLib\PVG\InputWrapper.cs" />
<Compile Include="MechJebLib\PVG\OutputWrapper.cs" />
<Compile Include="MechJebLib\PVG\AscentBuilder.cs" />
<Compile Include="MechJebLib\PVG\Integrators\IPVGIntegrator.cs" />
<Compile Include="MechJebLib\PVG\Integrators\VacuumCoastAnalytic.cs" />
Expand Down
218 changes: 0 additions & 218 deletions MechJeb2/MechJebLib/PVG/ArrayWrapper.cs

This file was deleted.

74 changes: 74 additions & 0 deletions MechJeb2/MechJebLib/PVG/InputWrapper.cs
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using MechJebLib.Primitives;

namespace MechJebLib.PVG
{
public struct InputWrapper
{
public const int INPUT_WRAPPER_LEN = 15;

public V3 R;

public V3 V;

public V3 PV;

public V3 PR;

public double CostateMagnitude => Math.Sqrt(PR.sqrMagnitude + PV.sqrMagnitude);

public double M;

public double Pm;

public double H0
{
get
{
double rm = R.magnitude;
return V3.Dot(PR, V) -
V3.Dot(PV, R) / (rm * rm * rm); // FIXME: this changes for linear gravity model??!?!?
}
}

public double Bt;

public void CopyTo(IList<double> other)
{
R.CopyTo(other);
V.CopyTo(other, 3);
PV.CopyTo(other, 6);
PR.CopyTo(other, 9);
other[12] = M;
other[13] = Pm;
other[14] = Bt;
}

public void CopyFrom(IList<double> other)
{
R.CopyFrom(other, 0);
V.CopyFrom(other, 3);
PV.CopyFrom(other, 6);
PR.CopyFrom(other, 9);
M = other[12];
Pm = other[13];
Bt = other[14];
}

public static InputWrapper CreateFrom(IList<double> other)
{
var a = new InputWrapper();

a.R.CopyFrom(other);
a.V.CopyFrom(other, 3);
a.PV.CopyFrom(other, 6);
a.PR.CopyFrom(other, 9);
a.M = other[12];
a.Pm = other[13];
a.Bt = other[14];

return a;
}
}
}
8 changes: 5 additions & 3 deletions MechJeb2/MechJebLib/PVG/Integrators/VacuumCoastAnalytic.cs
Expand Up @@ -14,8 +14,8 @@ public class VacuumCoastAnalytic : IPVGIntegrator
{
public void Integrate(Vn yin, Vn yfout, Phase phase, double t0, double tf)
{
using var y0 = ArrayWrapper.Rent(yin);
using var yf = ArrayWrapper.Rent(yfout);
var y0 = OutputWrapper.CreateFrom(yin);
var yf = new OutputWrapper();

(V3 rf, V3 vf, M3 stm00, M3 stm01, M3 stm10, M3 stm11) = Shepperd.Solve2(1.0, tf - t0, y0.R, y0.V);

Expand All @@ -29,11 +29,13 @@ public void Integrate(Vn yin, Vn yfout, Phase phase, double t0, double tf)
yf.Pm = y0.Pm;

yf.DV = y0.DV;

yf.CopyTo(yfout);
}

public void Integrate(Vn yin, Vn yfout, Phase phase, double t0, double tf, Solution solution)
{
var interpolant = Hn.Get(ArrayWrapper.ARRAY_WRAPPER_LEN);
var interpolant = Hn.Get(OutputWrapper.OUTPUT_WRAPPER_LEN);
interpolant.Add(t0, yin);
for (int i = 1; i < 21; i++)
{
Expand Down
9 changes: 5 additions & 4 deletions MechJeb2/MechJebLib/PVG/Integrators/VacuumThrustAnalytic.cs
Expand Up @@ -18,8 +18,8 @@ public void Integrate(Vn yin, Vn yfout, Phase phase, double t0, double tf)
{
Check.True(phase.Normalized);

using var y0 = ArrayWrapper.Rent(yin);
using var yf = ArrayWrapper.Rent(yfout);
var y0 = OutputWrapper.CreateFrom(yin);
var yf = new OutputWrapper();

double rm = y0.R.magnitude;

Expand Down Expand Up @@ -106,15 +106,16 @@ public void Integrate(Vn yin, Vn yfout, Phase phase, double t0, double tf)
yf.DV = y0.DV + phase.thrust / phase.m0 * dt;

yf.Pm = y0.Pm; // FIXME: this is certainly wrong

yf.CopyTo(yfout);
}

public void Integrate(Vn y0in, Vn yfout, Phase phase, double t0, double tf, Solution solution)
{
// kinda janky way to compute interpolants with intermediate points
var interpolant = Hn.Get(ArrayWrapper.ARRAY_WRAPPER_LEN);
var interpolant = Hn.Get(OutputWrapper.OUTPUT_WRAPPER_LEN);
interpolant.Add(t0, y0in);
const int SEGMENTS = 20;
using var y0 = ArrayWrapper.Rent(y0in);

for (int i = 1; i < 21; i++)
{
Expand Down
8 changes: 5 additions & 3 deletions MechJeb2/MechJebLib/PVG/Integrators/VacuumThrustIntegrator.cs
Expand Up @@ -18,16 +18,16 @@ public class VacuumThrustIntegrator : IPVGIntegrator
{
private class VacuumThrustKernel
{
public static int N => ArrayWrapper.ARRAY_WRAPPER_LEN;
public static int N => OutputWrapper.OUTPUT_WRAPPER_LEN;

public Phase Phase = null!;

public void dydt(IList<double> yin, double x, IList<double> dyout)
{
Check.True(Phase.Normalized);

using var y = ArrayWrapper.Rent(yin);
using var dy = ArrayWrapper.Rent(dyout);
var y = OutputWrapper.CreateFrom(yin);
var dy = new OutputWrapper();

double at = Phase.thrust / y.M;
if (Phase.Infinite)
Expand All @@ -50,6 +50,8 @@ public void dydt(IList<double> yin, double x, IList<double> dyout)
? _phase.ThrustBar * V3.Dot(y.PV, u) / (y.M * y.M)
: 0; */
dy.DV = at;

dy.CopyTo(dyout);
}
}

Expand Down

0 comments on commit 26de45d

Please sign in to comment.