Skip to content

Commit 1caa717

Browse files
committed
small additions
1 parent 76d3866 commit 1caa717

File tree

3 files changed

+67
-31
lines changed

3 files changed

+67
-31
lines changed

RK/GravitationalMomentEquations.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ public GravitationalMomentEquations(double epsilon, double delta) : base(7)
1111
_delta = delta;
1212
}
1313

14-
public override void Equations(double t, double[] y, ref double[] fy)
14+
public override void Equations(double t, double[] y, ref double[] yDot)
1515
{
1616
var p = y[0] * y[0] + y[1] * y[1] + y[2] * y[2] + y[3] * y[3] - 1;
17-
fy[0] = -0.5 * y[1] * y[4] - 0.5 * y[2] * y[5] - 0.5 * y[3] * y[6] + 0.5 * y[2] - 0.5 * y[0] * p;
18-
fy[1] = 0.5 * y[0] * y[4] + 0.5 * y[2] * y[6] - 0.5 * y[3] * y[5] - 0.5 * y[3] - 0.5 * y[1] * p;
19-
fy[2] = 0.5 * y[0] * y[5] + 0.5 * y[3] * y[4] - 0.5 * y[1] * y[6] - 0.5 * y[0] - 0.5 * y[2] * p;
20-
fy[3] = 0.5 * y[0] * y[6] + 0.5 * y[1] * y[5] - 0.5 * y[2] * y[4] + 0.5 * y[1] - 0.5 * y[3] * p;
21-
fy[4] = (_epsilon - _delta) * (-y[5] * y[6] + 3 * (2 * y[2] * y[3] + 2 * y[0] * y[1])
17+
yDot[0] = -0.5 * y[1] * y[4] - 0.5 * y[2] * y[5] - 0.5 * y[3] * y[6] + 0.5 * y[2] - 0.5 * y[0] * p;
18+
yDot[1] = 0.5 * y[0] * y[4] + 0.5 * y[2] * y[6] - 0.5 * y[3] * y[5] - 0.5 * y[3] - 0.5 * y[1] * p;
19+
yDot[2] = 0.5 * y[0] * y[5] + 0.5 * y[3] * y[4] - 0.5 * y[1] * y[6] - 0.5 * y[0] - 0.5 * y[2] * p;
20+
yDot[3] = 0.5 * y[0] * y[6] + 0.5 * y[1] * y[5] - 0.5 * y[2] * y[4] + 0.5 * y[1] - 0.5 * y[3] * p;
21+
yDot[4] = (_epsilon - _delta) * (-y[5] * y[6] + 3 * (2 * y[2] * y[3] + 2 * y[0] * y[1])
2222
* (y[0] * y[0] - y[1] * y[1] - y[2] * y[2] + y[3] * y[3]));
23-
fy[5] = (1 - _epsilon) / _delta * (-y[6] * y[4] + 3 * (2 * y[1] * y[3] - 2 * y[0] * y[2])
23+
yDot[5] = (1 - _epsilon) / _delta * (-y[6] * y[4] + 3 * (2 * y[1] * y[3] - 2 * y[0] * y[2])
2424
* (y[0] * y[0] - y[1] * y[1] - y[2] * y[2] + y[3] * y[3]));
25-
fy[6] = (_delta - 1) / _epsilon * (-y[4] * y[5] + 3 * (2 * y[1] * y[3] - 2 * y[0] * y[2])
25+
yDot[6] = (_delta - 1) / _epsilon * (-y[4] * y[5] + 3 * (2 * y[1] * y[3] - 2 * y[0] * y[2])
2626
* (2 * y[2] * y[3] + 2 * y[0] * y[1]));
2727
}
2828
}

RK/RungeKuttaBase.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ namespace RK
55
public class RungeKuttaBase
66
{
77
public int N;
8-
double _t; // текущее время
9-
public double[] Y; // искомое решение Y[0] - само решение, Y[i] - i-тая производная решения
8+
double _t; // current time
9+
public double[] Y; //solutions
1010

11-
readonly double[] _yy;
12-
double[] _y1, _y2, _y3, _y4;
11+
private double[] _y1, _y2, _y3, _y4, _yy;
1312

14-
public RungeKuttaBase(int aN) // aN - размерность системы
13+
private void BaseSetup(int aN)
1514
{
1615
N = aN; // сохранить размерность системы
1716

@@ -29,6 +28,17 @@ public RungeKuttaBase(int aN) // aN - размерность системы
2928
_y4 = new double[N];
3029
}
3130

31+
public RungeKuttaBase(int aN) // aN - размерность системы
32+
{
33+
BaseSetup(aN);
34+
}
35+
36+
public RungeKuttaBase(int aN, double t0, double[] y0)
37+
{
38+
BaseSetup(aN);
39+
SetInit(t0, y0);
40+
}
41+
3242
public void SetInit(double t0, double[] y0) // установить начальные условия.
3343
{ // t0 - начальное время, Y0 - начальное условие
3444
_t = t0;
@@ -39,12 +49,12 @@ public void SetInit(double t0, double[] y0) // установить началь
3949
}
4050
}
4151

42-
public double GetTime() // вернуть текущее время
52+
public double GetTime()
4353
{
4454
return _t;
4555
}
4656

47-
public virtual void Equations(double t, double[] y, ref double[] fy)
57+
public virtual void Equations(double t, double[] y, ref double[] yDot)
4858
{
4959
throw new NotImplementedException();
5060
}

RK/Solver.cs

+42-16
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,57 @@ namespace RK
66
{
77
public static class Solver
88
{
9-
static double[] SolveOnePoint(double tInit, double[] yInit,
9+
public const double Pi = Math.PI;
10+
11+
public static double[] InitialConditions(double phi0, double theta0, double psi0)
12+
{
13+
var sPh0 = Math.Sin(phi0 * Pi / 2);
14+
var sPs0 = Math.Sin(psi0 * Pi / 2);
15+
var sTh0 = Math.Sin(theta0 * Pi / 2);
16+
var cPh0 = Math.Cos(phi0 * Pi / 2);
17+
var cPs0 = Math.Cos(psi0 * Pi / 2);
18+
var cTh0 = Math.Cos(theta0 * Pi / 2);
19+
20+
var lambda0 = cPh0*cPs0*cTh0 + sPh0*sPs0*sTh0;
21+
var lambda1 = sPh0*cPs0*cTh0 - cPh0*sPs0*sTh0;
22+
var lambda2 = cPh0*cPs0*sTh0 + sPh0*sPs0*cTh0;
23+
var lambda3 = cPh0*sPs0*cTh0 - sPh0*cPs0*sTh0;
24+
return new[] { lambda0, lambda1, lambda2, lambda3, 0, 1, 0 };
25+
26+
}
27+
28+
private static double Psi(this double[] y)
29+
{
30+
var alpha1 = y[0] * y[0] + y[1] * y[1] - y[2] * y[2] - y[3] * y[3];
31+
var beta1 = 2 * (y[1] * y[2] + y[0] * y[3]);
32+
return Math.Abs(Math.Atan(beta1 / alpha1));
33+
}
34+
35+
private static double Phi(this double[] y)
36+
{
37+
var gamma2 = 2 * (y[2] * y[3] + y[0] * y[1]);
38+
var gamma3 = y[0] * y[0] - y[1] * y[1] - y[2] * y[2] + y[3] * y[3];
39+
return Math.Abs(Math.Atan(gamma2 / gamma3));
40+
}
41+
42+
private static double Theta(this double[] y)
43+
{
44+
var gamma1 = 2 * (y[1] * y[3] - y[0] * y[2]);
45+
return Math.Abs(-Math.Asin(gamma1));
46+
}
47+
48+
static void SolveOnePoint(double tInit, double[] yInit,
1049
double epsilon, double delta,
1150
double timePeriod, double timeStep)
1251
{
1352
var rk4 = new GravitationalMomentEquations(epsilon, delta);
1453
rk4.SetInit(tInit, yInit);
15-
var write = new StreamWriter("Test.txt");
16-
54+
1755
while (rk4.GetTime() < timePeriod)
1856
{
19-
write.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}",
20-
Convert.ToString(rk4.GetTime(), CultureInfo.InvariantCulture),
21-
Convert.ToString(rk4.Y[0], CultureInfo.InvariantCulture),
22-
Convert.ToString(rk4.Y[1], CultureInfo.InvariantCulture),
23-
Convert.ToString(rk4.Y[2], CultureInfo.InvariantCulture),
24-
Convert.ToString(rk4.Y[3], CultureInfo.InvariantCulture),
25-
Convert.ToString(rk4.Y[4], CultureInfo.InvariantCulture),
26-
Convert.ToString(rk4.Y[5], CultureInfo.InvariantCulture),
27-
Convert.ToString(rk4.Y[6], CultureInfo.InvariantCulture));
28-
57+
//rk4.Y
2958
rk4.NextStep(timeStep);
3059
}
31-
32-
write.Close();
33-
return rk4.Y;
3460
}
3561
}
3662
}

0 commit comments

Comments
 (0)