Convert CPU Parallel.For to GPU Gpu.For - c#

I created a CPU Parallel.For loop in my code as follow:
Vector2[] p;
Vector2[] pnew;
Vector2[] v;
float[] m;
Parallel.For(0, N, i =>
{
double dx;
double dy;
double invr;
double invr3;
double f;
double ax;
double ay;
double eps = 0.02;
ax = 0;
ay = 0;
for (int j = 0; j < N; j++)
{
dx = p[j].X - p[i].X;
dy = p[j].Y - p[i].Y;
invr =1.0 / Math.Sqrt(dx * dx + dy * dy + eps);
invr3 = invr * invr * invr;
f = m[j] * m[i] * invr3;
ax += f * dx;
ay += f * dy;
}
pnew[i].X = (float)(p[i].X + dt * v[i].X + 0.5 * dt * dt * ax); /* save new position of particle "i" */
pnew[i].Y = (float)(p[i].Y + dt * v[i].Y + 0.5 * dt * dt * ay);
v[i].X += dt * (float)ax; /* update velocity of particle "i" */
v[i].Y += dt * (float)ay;
});
The above code works fine and run on my CPU cores.
Want I want to do it to convert this code to a GPU For loop using "Alea GPU" library. So i tried the following:
Vector2[] p;
Vector2[] pnew;
Vector2[] v;
float[] m;
[GpuManaged]
public void calculForceGPU()
{
Gpu.Default.For(0, N + 1, i =>
{
double dx;
double dy;
double invr;
double invr3;
double f;
double ax;
double ay;
double eps = 0.02;
ax = 0;
ay = 0;
for (int j = 0; j < N; j++)
{
dx = p[j].X - p[i].X;
dy = p[j].Y - p[i].Y;
invr = 1.0 / Math.Sqrt(dx * dx + dy * dy + eps);
invr3 = invr * invr * invr;
f = m[j] * m[i] * invr3;
ax += f * dx;
ay += f * dy;
}
pnew[i].X = (float)(p[i].X + dt * v[i].X + 0.5 * dt * dt * ax); /* save new position of particle "i" */
pnew[i].Y = (float)(p[i].Y + dt * v[i].Y + 0.5 * dt * dt * ay);
v[i].X += dt * (float)ax; /* update velocity of particle "i" */
v[i].Y += dt * (float)ay;
});
}
You can see that it is the exact same code as above but with Parallel.For changed for Gpu.Default.For. But when I run it I get the following error:
i32 is not struct type.
Source location stack:
-> in C:\Users\...\Simulation.cs(628,21-628,42)
-> at ....Simulation.[Void <calculForceGPU>b__36_0(Int32)]
-> at Alea.Parallel.Device.DeviceFor.[Void Kernel(Int32, Int32,
System.Action`1[System.Int32])]
-> at defining runtime32 (sm52,32bit)
Loading method as kernel:
-> Method: Alea.Parallel.Device.DeviceFor.[Void Kernel(Int32, Int32,
System.Action`1[System.Int32])]
-> InstanceOpt: <None>
-> Argument.#0: 0
-> Argument.#1: 1025
-> Argument.#2: System.Action`1[System.Int32]
Getting or loading method as kernel:
-> Method: Alea.Parallel.Device.DeviceFor.[Void Kernel(Int32, Int32,
System.Action`1[System.Int32])]
-> InstanceOpt: <None>
-> Argument.#0: 0
-> Argument.#1: 1025
-> Argument.#2: System.Action`1[System.Int32]
I am not sure how to solve this error. Any help would be appreciated.
Update on what I tried after comments by NineBerry:
So it turns out the problem might be the Vector2 type because it might use properties. So I created my own struct which use Fields like so:
struct Vector2Struct
{
public float X;
public float Y;
public Vector2Struct(float x, float y)
{
X = x;
Y = y;
}
}
Vector2Struct[] p;
Vector2Struct[] pnew;
Vector2Struct[] v;
float[] m;
The rest of the code is pretty much the same as before. But I still get the same "i32 is not struct type." error.
Same error if I ditch all struct and use arrays of float instead:
float[] m;
float[] pX;
float[] pY;
float[] pnewX;
float[] pnewY;
float[] vX;
float[] vY;
Code dump as per comment. Creating a new instance of the class should make it run. You will need to install nuget ALEA and ALEA.FODY . Also I think you need FSharp.Core to run Alea
using System;
using Alea;
using Alea.Parallel;
namespace GalaxyTest
{
public class Simulation
{
// param
object[] param;
// masses
float[] m;
float[] pX;
float[] pY;
float[] pnewX;
float[] pnewY;
float[] vX;
float[] vY;
// data
static int N;
double ratioPM;
double ratioMasse;
int Np;
int Nm;
int distribution;
int simulType;
float dt = 0.01F;
double eps = 0.02;
float Rrp = 1;
float Rrm = 10;
public Simulation() //Constructor
{
Initializer();
updatePointGPUAlea();
}
private void Initializer()
{
// Settings
N = 1024;
ratioPM = 0.25;
ratioMasse = 1;
Np = 256;
Nm = 769;
distribution = 0;
simulType = 1;
// vector Initialisation
pX = new float[N];
pY = new float[N];
pnewX = new float[N];
pnewY = new float[N];
vX = new float[N];
vY = new float[N];
m = new float[N];
// compute masses
for (int i = 0; i < Np; i++)
{
m[i] = 1;
}
for (int i = Np; i < Nm; i++)
{
m[i] = -1 * (float)ratioMasse;
}
Random r = new Random();
double R;
double teta;
double Rp;
double Rn;
float signe1, signe2, signe3, signe4;
// Init pos = random shell
for (int i = 0; i < N; i++)
{
Rp = 2.61;
Rn = 45;
teta = r.NextDouble() * 2 * Math.PI;
signe1 = Math.Sign(r.NextDouble() - 0.5);
signe2 = Math.Sign(r.NextDouble() - 0.5);
signe3 = Math.Sign(r.NextDouble() - 0.5);
signe4 = Math.Sign(r.NextDouble() - 0.5);
if (m[i] > 0)
{
pX[i] = (float)(Rp * Math.Cos(teta)) + 400 / 2;
pY[i] = (float)(Rp * Math.Sin(teta)) + 400 / 2;
vX[i] = (float)(r.NextDouble() * Rrp * signe1 + Math.Sqrt(Np) / 12 * 3 * Math.Sin(teta) * (0.4 - 1 / Math.Sqrt(10 + Rp)) * 3);
vY[i] = (float)(r.NextDouble() * Rrp * signe2 - Math.Sqrt(Np) / 12 * 3 * Math.Cos(teta) * (0.4 - 1 / Math.Sqrt(10 + Rp)) * 3);
}
else
{
pX[i] = (float)(Rn * Math.Cos(teta)) + 400 / 2;
pY[i] = (float)(Rn * Math.Sin(teta)) + 400 / 2;
vX[i] = (float)r.NextDouble() * Rrm * signe3;
vY[i] = (float)r.NextDouble() * Rrm * signe4;
}
}
}
public void updatePointGPUAlea()
{
calculForceGPU();
for (int i = 0; i < N; i++)
{
// Update de la position
pX[i] = pnewX[i];
pY[i] = pnewY[i];
}
}
[GpuManaged]
public void calculForceGPU()
{
Gpu.Default.For(0, N + 1, i =>
{
double dx;
double dy;
double invr;
double invr3;
double f;
double ax;
double ay;
ax = 0;
ay = 0;
for (int j = 0; j < N; j++)
{
dx = pX[j] - pX[i];
dy = pY[j] - pY[i];
invr = 1.0 / Math.Sqrt(dx * dx + dy * dy + eps);
invr3 = invr * invr * invr;
f = m[j] * m[i] * invr3;
ax += f * dx;
ay += f * dy;
}
pnewX[i] = (float)(pX[i] + dt * vX[i] + 0.5 * dt * dt * ax); /* save new position of particle "i" */
pnewY[i] = (float)(pY[i] + dt * vY[i] + 0.5 * dt * dt * ay);
vX[i] += dt * (float)ax; /* update velocity of particle "i" */
vY[i] += dt * (float)ay;
});
}
}
}

This is probably because you use the X and Y properties of the Vector2 struct type. Alea does not support properties according to the documentation:
Note that the current version of Alea GPU only supports fields in structs but not properties
See also Alea: "i32 is not struct type

Related

LAB to XYZ and XYZ to RGB color space conversion algorithm

I tried to convert CIE-LAB color space to RGB color space. But there is a mistake.
input LAB values = (46.41,-39.24,33.51)
received result XYZ values =(-2,641482,15,57358,-5,368798)
received result RGB vaues = (-791,4557,135,8615,-271,5485)
XYZ values should be (9.22,15.58,5.54)
RGB values should be (50,125,50)
I checked these values from http://colorizer.org/
Where did I make a mistake?
If you check the following code and answer me. I will be glad. Thanks.
I convert RGB to XYZ and XYZ to LAB color space conversion. You can check
using the following link.
RGB / XYZ and XYZ-LAB color space conversion algorithm
public static Vector4 LabToXYZ(Vector4 color)
{
float[] xyz = new float[3];
float[] col = new float[] { color[0], color[1], color[2], color[3]};
xyz[1] = (col[0] + 16.0f) / 116.0f;
xyz[0] = (col[1] / 500.0f) + xyz[0];
xyz[2] = xyz[0] - (col[2] / 200.0f);
for (int i = 0; i < 3; i++)
{
float pow = xyz[i] * xyz[i] * xyz[i];
if (pow > .008856f)
{
xyz[i] = pow;
}
else
{
xyz[i] = (xyz[i]- 16.0f / 116.0f) / 7.787f;
}
}
xyz[0] = xyz[0] * (95.047f);
xyz[1] = xyz[1] * (100.0f);
xyz[2] = xyz[2] * (108.883f);
return new Vector4(xyz[0], xyz[1], xyz[2], color[3]);
}
public static Vector4 XYZToRGB(Vector4 color)
{
float[] rgb = new float[3];
float[] xyz = new float[3];
float[] col = new float[] { color[0], color[1], color[2] };
for (int i = 0; i < 3; i++)
{
xyz[i] = col[i] / 100.0f;
}
rgb[0] = (xyz[0] * 3.240479f) + (xyz[1] * -1.537150f) + (xyz[2] * -.498535f);
rgb[1] = (xyz[0] * -.969256f) + (xyz[1] * 1.875992f) + (xyz[2] * .041556f);
rgb[2] = (xyz[0] * .055648f) + (xyz[1] * -.204043f) + (xyz[2] * 1.057311f);
for (int i = 0; i < 3; i++)
{
if (rgb[i] > .0031308f)
{
rgb[i] = (1.055f * (float)Math.Pow(rgb[i], (1.0f / 2.4f))) - .055f;
}
else
{
rgb[i] = rgb[i] * 12.92f;
}
}
rgb[0] = rgb[0] * 255.0f;
rgb[1] = rgb[1] * 255.0f;
rgb[2] = rgb[2] * 255.0f;
return new Vector4(rgb[0], rgb[1], rgb[2], color[3]);
}
public static Vector4 LabToRGB(Vector4 color)
{
Vector4 xyz = LabToXYZ(color);
Vector4 rgb = XYZToRGB(xyz);
Debug.Log("R: " + rgb[0]);
Debug.Log("G: " + rgb[1]);
Debug.Log("B: " + rgb[2]);
Debug.Log("A: " + color[3]);
return new Vector4 (rgb[0],rgb[1],rgb[2]);
}
I changed only XYZ computations in the LabToXYZ function and I received correct values.
There is a little mistake.
xyz[1] = (col[0] + 16.0f) / 116.0f;
xyz[0] = (col[1] / 500.0f) + xyz[0];
xyz[2] = xyz[0] - (col[2] / 200.0f);
Is not correct. This should be like below
xyz[1] = (col[0] + 16.0f) / 116.0f;
xyz[0] = (col[1] / 500.0f) + xyz[1];
xyz[2] = xyz[1] - (col[2] / 200.0f);
Also, you can change to LabToXYZ function like the following function.
public static Vector4 LabToXYZ(Vector4 color)
{
float[] xyz = new float[3];
float[] col = new float[] { color[0], color[1], color[2], color[3]};
xyz[1] = (col[0] + 16.0f) / 116.0f;
xyz[0] = (col[1] / 500.0f) + xyz[1];
xyz[2] = xyz[1] - (col[2] / 200.0f);
for (int i = 0; i < 3; i++)
{
float pow = xyz[i] * xyz[i] * xyz[i];
float ratio = (6.0f / 29.0f);
if (xyz[i] > ratio)
{
xyz[i] = pow;
}
else
{
xyz[i] = (3.0f * (6.0f / 29.0f) * (6.0f / 29.0f) * (xyz[i] - (4.0f / 29.0f)));
}
}
xyz[0] = xyz[0] * 95.047f;
xyz[1] = xyz[1] * 100.0f;
xyz[2] = xyz[2] * 108.883f;
return new Vector4(xyz[0], xyz[1], xyz[2], color[3]);
}
see: https://en.wikipedia.org/wiki/CIELAB_color_space#RGB_and_CMYK_conversions for other computations

Problem with different outcome with DCT transformation on image while converting C++ to C#

I tried to write APP that would make DCT-2 transformation on image fragment and then transform back with inverse DCT-2. I have found code in c++/opencv that I tried to convert to C#, but somehow, I have different outcome at some point. Here is the code i tried to convert:
for (unsigned v = 0; v < BLOCK_SIZE; ++v)
{
for (unsigned u = 0; u < BLOCK_SIZE; ++u)
{
const double cu = (u == 0) ? 1.0 / sqrt(2) : 1.0;
const double cv = (v == 0) ? 1.0 / sqrt(2) : 1.0;
double dctCoeff = 0;
for (unsigned y = 0; y < BLOCK_SIZE; ++y)
{
for (unsigned x = 0; x < BLOCK_SIZE; ++x)
{
double uCosFactor = cos((double)(2 * x + 1) * M_PI * (double)u / (2 * (double) BLOCK_SIZE));
double vCosFactor = cos((double)(2 * y + 1) * M_PI * (double)v / (2 * (double) BLOCK_SIZE));
double pixel = (double)(lenaNoseGrey.at<unsigned char>(cv::Point(x,y)));
dctCoeff += pixel * uCosFactor * vCosFactor;
}
}
dctCoeff *= (2 / (double) BLOCK_SIZE) * cu * cv;
lenaNoseDct.at<double>(cv::Point(u,v)) = dctCoeff;
}
}
And here is mine:
for (int v = 0; v < BLOCK_SIZE; ++v)
{
for (int u = 0; u < BLOCK_SIZE; ++u)
{
double cu = (u == 0) ? 1.0 / Math.Sqrt(2) : 1.0;
double cv = (v == 0) ? 1.0 / Math.Sqrt(2) : 1.0;
double dctCoeff = 0;
double dctCoeffAlpha = 0;
for (int y1 = 0; y1 < BLOCK_SIZE; ++y1)
{
for (int x1 = 0; x1 < BLOCK_SIZE; ++x1)
{
double uCosFactor = Math.Cos((2 * x1 + 1) * Math.PI * u / (2 * (double)BLOCK_SIZE));
double vCosFactor = Math.Cos((2 * y1 + 1) * Math.PI * v / (2 * (double)BLOCK_SIZE));
double pixel = (double)bitmapaWy1.GetPixel((x1 + 284), (y1 + 313)).R;
double pixelalpha = (double)bitmapaWy1.GetPixel((x1 + 284), (y1 + 313)).A;
dctCoeff += pixel * uCosFactor * vCosFactor;
//dctCoeffAlpha += pixelalpha * uCosFactor * vCosFactor;
dctCoeffAlpha = pixelalpha;
}
}
dctCoeffAlpha *= (2 / (double)BLOCK_SIZE) * cu * cv;
dctCoeff *= (2 / (double)BLOCK_SIZE) * cu * cv;
macierz[u, v] = dctCoeff;
}
}
I have different outcome in my matrix, but when i convert matrix from the C++ code above with my inverse code, it works well.
Can you find what have I done wrong? One difference I can spot is with getpixel method, but it is performed on exact same grayscaled image and image fragment that C++ code was performed.
When i inverse my matrix to image again, I can see image, but it has lot of random pixels that are too white, or too black.
Problem is solved, the issue was too much code, actual code i posted works, problem was code I had after it, which was changing "dctcoeff" to byte and it was inside loop, so value of dct didnt reset to 0;
int BLOCK_SIZE = 16;
double[,] macierz = new double[16, 16];
for (int v = 0; v < BLOCK_SIZE; ++v)
{
for (int u = 0; u < BLOCK_SIZE; ++u)
{
double cu = (u == 0) ? 1.0 / Math.Sqrt(2) : 1.0;
double cv = (v == 0) ? 1.0 / Math.Sqrt(2) : 1.0;
double dctCoeff = 0;
for (int y1 = 0; y1 < BLOCK_SIZE; ++y1)
{
for (int x1 = 0; x1 < BLOCK_SIZE; ++x1)
{
double uCosFactor = Math.Cos((2 * x1 + 1) * Math.PI * u / (2 * (double)BLOCK_SIZE));
double vCosFactor = Math.Cos((2 * y1 + 1) * Math.PI * v / (2 * (double)BLOCK_SIZE));
double pixel = bitmapaWy1.GetPixel((x1 + 284), (y1 + 313)).R;
dctCoeff += pixel * uCosFactor * vCosFactor;
//dctCoeffAlpha += pixelalpha * uCosFactor * vCosFactor;
}
}
dctCoeff *= (2 / (double)BLOCK_SIZE) * cu * cv;
macierz[u, v] = dctCoeff;
}
}
This code works like a charm. I hope some good soul will use it, becouse i spent about 20h into making it work.
This is clearly a precision issue.
Most likely the problem is at the following line:
macierz[u, v] = dctCoeff;
I also have doubt in:
double pixel = (double)bitmapaWy1.GetPixel((x1 + 284), (y1 + 313)).R;
double pixelalpha = (double)bitmapaWy1.GetPixel((x1 + 284), (y1 + 313)).A;
dctCoeff += pixel * uCosFactor * vCosFactor;
//dctCoeffAlpha += pixelalpha * uCosFactor * vCosFactor;
dctCoeffAlpha = pixelalpha;

I converted a CSV file to shapefile, but can't create its projection

I tried to convert a csv file to shapefile with its projection. Conversion works but I can't create the .prj file.
The error says that
"there is no source code available for current location".
My code is as follows:
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
AxMap axMap1 = new AxMap();
Shapefile sf = new Shapefile();
bool result = sf.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON);
if (!result) {
MessageBox.Show(sf.get_ErrorMsg(sf.LastErrorCode));
} else {
double xMin = 0.0;
double yMin = 0.0;
double xMax = 1000.0;
double yMax = 1000.0;
Random rnd = new Random(DateTime.Now.Millisecond);
int fldX = sf.EditAddField("x", FieldType.DOUBLE_FIELD, 9, 12);
int fldY = sf.EditAddField("y", FieldType.DOUBLE_FIELD, 9, 12);
int fldArea = sf.EditAddField("area", FieldType.DOUBLE_FIELD, 9, 12);
// In a loop we are creating 100 different points using the box established above.
for (int i = 0; i < 100; i++) {
if (i % 10 == 0) {
Shape shp1 = new Shape();
shp1.Create(ShpfileType.SHP_POLYGON);
sf.EditInsertShape(shp1, ref i);
} else {
double xCenter = xMin + (xMax - xMin) * rnd.NextDouble();
double yCenter = yMin + (yMax - yMin) * rnd.NextDouble();
// random radius from 10 to 100
double radius = 10 + rnd.NextDouble() * 90;
// polygons must be clockwise
Shape shp = new Shape();
shp.Create(ShpfileType.SHP_POLYGON);
for (int j = 0; j < 37; j++) {
Point pnt = new Point();
pnt.x = xCenter + radius * Math.Cos(j * Math.PI / 18);
pnt.y = yCenter - radius * Math.Sin(j * Math.PI / 18);
shp.InsertPoint(pnt, ref j);
}
sf.EditInsertShape(shp, ref i);
sf.EditCellValue(fldX, i, xCenter.ToString());
sf.EditCellValue(fldY, i, yCenter.ToString());
sf.EditCellValue(fldArea, i, Math.PI * radius * radius);
}
}
axMap1.CreateControl();
axMap1.AddLayer(sf, true);
axMap1.ZoomToLayer(0);
sf.Categories.Generate(fldArea, tkClassificationType.ctNaturalBreaks, 7);
ColorScheme scheme = new ColorScheme();
scheme.SetColors2(tkMapColor.Wheat, tkMapColor.Salmon); sf.Categories.ApplyColorScheme(tkColorSchemeType.ctSchemeGraduated, scheme);
axMap1.Redraw();
sf.SaveAs(#"D:\shp1\polygons.shp", null);
sf.Open("D:\\shp1\\polygons.shp");
sf.Projection = (DotSpatial.Projections.KnownCoordinateSystems.Projected.UtmWgs1984.WGS1984UTMZone32N).ToString();
sf.SaveAs(#"D:\shp1\polygons.prj");
}
}

Simple Mandelbrot renderer runs out of memory

Ive been trying to make an simple mandelbrot renderer just to get myself into c# and forms, but when i render the image the program runs out of memory sometimes.
The memory builds up to 2GB and then crashes.
But sometimes it builds up i a jigsaw pattern like this and dont crash:
http://puu.sh/l2ri9/2fcd47e6d7.png
==============Code to render================
Renderer.CreateGraphics().Clear(Color.White);
double minR = System.Convert.ToDouble(MinR.Value);
double maxR = System.Convert.ToDouble(MaxR.Value);
double minI = System.Convert.ToDouble(MaxI.Value);
double maxI = System.Convert.ToDouble(MinI.Value);
int maxN = System.Convert.ToInt32(Iterations.Value);
SolidBrush MandelColor = new SolidBrush(Color.Red);
for (int y = 0; y < Renderer.Height; y++)
{
for (int x = 0; x < Renderer.Width; x++)
{
double cr = fitInRRange(x, Renderer.Width, minR, maxR);
double ci = fitInIRange(y, Renderer.Height, minI, maxI);
int n = findMandelbrot(cr, ci, maxN);
double t = ((n + 0.0) / (maxN + 0.0));
MandelColor.Color = Color.FromArgb(System.Convert.ToInt32(9 * (1 - t) * t * t * t * 255), System.Convert.ToInt32(15 * (1 - t) * (1 - t) * t * t * 255), System.Convert.ToInt32(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255));
Renderer.CreateGraphics().FillRectangle(MandelColor, x, y, 1, 1);
}
}
===Link To Github page===
https://github.com/JimAlexBerger/MandelbrotProject
Why not move Renderer.CreateGraphics() outside of the loop? The memory leak is probably caused by redundantly calling Renderer.CreateGraphics() without calling IDisposable.Dispose() on the Graphics object.
double minR = System.Convert.ToDouble(MinR.Value);
double maxR = System.Convert.ToDouble(MaxR.Value);
double minI = System.Convert.ToDouble(MaxI.Value);
double maxI = System.Convert.ToDouble(MinI.Value);
int maxN = System.Convert.ToInt32(Iterations.Value);
SolidBrush MandelColor = new SolidBrush(Color.Red);
using(var graphics = Renderer.CreateGraphics())
{
graphics.Clear(Color.White);
for (int y = 0; y < Renderer.Height; y++)
{
for (int x = 0; x < Renderer.Width; x++)
{
double cr = fitInRRange(x, Renderer.Width, minR, maxR);
double ci = fitInIRange(y, Renderer.Height, minI, maxI);
int n = findMandelbrot(cr, ci, maxN);
double t = ((n + 0.0) / (maxN + 0.0));
MandelColor.Color = Color.FromArgb(System.Convert.ToInt32(9 * (1 - t) * t * t * t * 255), System.Convert.ToInt32(15 * (1 - t) * (1 - t) * t * t * 255), System.Convert.ToInt32(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255));
gfx.FillRectangle(MandelColor, x, y, 1, 1);
}
}
}

N body simulation in C#

I'm trying to implement an N body simulation in C# using either Runge Kutta 4 or Velocity Verlet integration algorithms.
Before I move to a bigger number of particles, I wanted to test the simulation by modeling the earth's orbit around the sun, however, instead of the elliptical orbit, I get a weird spiral for some reason.
I can't figure out the problem since I made a simpler simulation of the solar system using the same algorithms where the sun was fixed in position and everything worked perfectly. The integrators work perfectly because it doesn't matter which one I use, I get the spiral with both.
Any help would be appreciated.
Here's the code:
class NBODY
{
public static double G = 4 * Math.PI * Math.PI;
class Particle
{
public double[] r; // position vector
public double[] v; // velocity vector
public double mass;
//constructor
public Particle() {}
public Particle(double x, double y, double z, double vx, double vy, double vz, double m)
{
this.r = new double[3];
this.v = new double[3];
this.r[0] = x;
this.r[1] = y;
this.r[2] = z;
this.v[0] = vx;
this.v[1] = vy;
this.v[2] = vz;
this.mass = m;
}
public void Update(Particle[] particles, double t, double h, int particleNumber)
{
RungeKutta4(particles, t, h, particleNumber);
}
private double acc(double r, Particle[] particles, int particleNumber, double[] r_temp, int l)
{
// dv/dt = f(x) = -G * m_i * (x - x_i) / [(x - x_i)^2 + (y - y_i)^2 + (z - z_i)^2]^(3/2)
double sum = 0;
switch (l)
{
case 0:
for (int i = 0; i < particles.Length; i++)
if (i != particleNumber)
sum += particles[i].mass * (r - particles[i].r[l]) / Math.Pow( Math.Pow(r - particles[i].r[l], 2)
+ Math.Pow(r_temp[1] - particles[i].r[1], 2) + Math.Pow(r_temp[2] - particles[i].r[2], 2), 1.5);
break;
case 1:
for (int i = 0; i < particles.Length; i++)
if (i != particleNumber)
sum += particles[i].mass * (r - particles[i].r[l]) / Math.Pow(Math.Pow(r - particles[i].r[l], 2)
+ Math.Pow(r_temp[0] - particles[i].r[0], 2) + Math.Pow(r_temp[2] - particles[i].r[2], 2), 1.5);
break;
case 2:
for (int i = 0; i < particles.Length; i++)
if (i != particleNumber)
sum += particles[i].mass * (r - particles[i].r[l]) / Math.Pow(Math.Pow(r - particles[i].r[l], 2)
+ Math.Pow(r_temp[0] - particles[i].r[0], 2) + Math.Pow(r_temp[1] - particles[i].r[1], 2), 1.5);
break;
}
return -G * sum;
}
private void RungeKutta4(Particle[] particles, double t, double h, int particleNumber)
{
//current position of the particle is saved in a vector
double[] r_temp = new double[3];
for (int j = 0; j < 3; j++)
r_temp[j] = this.r[j];
//loop going over all the coordinates and updating each using RK4 algorithm
for (int l = 0; l < 3; l++)
{
double[,] k = new double[4, 2];
k[0, 0] = this.v[l]; //k1_r
k[0, 1] = acc(this.r[l], particles, particleNumber, r_temp, l); //k1_v
k[1, 0] = this.v[l] + k[0, 1] * 0.5 * h; //k2_r
k[1, 1] = acc(this.r[l] + k[0, 0] * 0.5 * h, particles, particleNumber, r_temp, l); //k2_v
k[2, 0] = this.v[l] + k[1, 1] * 0.5 * h; //k3_r
k[2, 1] = acc(this.r[l] + k[1, 0] * 0.5 * h, particles, particleNumber, r_temp, l); //k3_v
k[3, 0] = this.v[l] + k[2, 1] * h; //k4_r
k[3, 1] = acc(this.r[l] + k[2, 0] * h, particles, particleNumber, r_temp, l); //k4_v
this.r[l] += (h / 6.0) * (k[0, 0] + 2 * k[1, 0] + 2 * k[2, 0] + k[3, 0]);
this.v[l] += (h / 6.0) * (k[0, 1] + 2 * k[1, 1] + 2 * k[2, 1] + k[3, 1]);
}
}
/*
Velocity Verlet algorithm:
1. Calculate y(t+h) = y(t) + v(t)h + 0.5a(t)h*h
2. Derive a(t+h) from dv/dt = -y using y(t+h)
3. Calculate v(t+h) = v(t) + 0.5*(a(t) + a(t+h))*h
*/
private void VelocityVerlet(Particle[] particles, double t, double h, int particleNumber)
{
double[] r_temp = new double[3];
for (int j = 0; j < 3; j++)
r_temp[j] = this.r[j];
//loop going over all the coordinates and updating each using RK4 algorithm
for (int l = 0; l < 3; l++)
{
//position
this.r[l] += h * this.v[l] + 0.5 * h * h * acc(this.r[l], particles, particleNumber, r_temp, l);
//velocity
this.v[l] += 0.5 * h * (acc(r_temp[l], particles, particleNumber, r_temp,l)
+ acc(this.r[l], particles, particleNumber, r_temp,l));
}
}
}
static void Main(string[] args)
{
//output file
TextWriter output = new StreamWriter("ispis.txt");
// declarations of variables
Particle[] particles = new Particle[2];
particles[0] = new Particle(0, 0, 0, 0, 0, 0, 1); //sun
particles[1] = new Particle(1, 0, 0, 0, 6.28, 0, 3.003467E-06); //earth
int N = 200;
double h, t, tmax;
double[,,] x = new double[particles.Length, N, 3]; //output
// setting initial values, step size and max time tmax
h = 0.01; // the step size in years
tmax = h * N;
// initial time
t = 0;
int i = 0;
while (t <= tmax) {
//updates position of all particles
for (int z = 1; z < particles.Length; z++)
particles[z].Update(particles, t, h, z);
//saves the position for output
for (int j = 1; j < particles.Length ; j++)
for (int z = 0; z < 3; z++ )
x[j,i,z] = particles[j].r[z];
t += h;
i++;
}
//output to file
for (int k = 0; k < particles.Length; k++ )
{
for (int f = 0; f < 3; f++)
{
for (int l = 0; l < N; l++)
output.Write(string.Format("{0,-15:0.########},", x[k,l,f]));
output.Write(string.Format("\n\n"));
}
output.Write(string.Format("\n\n\n\n"));
}
output.Close();
}
}
And here's the plot of the output data for earth's orbit:
Your model calculates the gravity force between two particles twice: for the first particle the force is based on their original coordinates, and for the second particle it is based on an updated position of the first one. This is a clear violation of the Newton's 3rd law. You must precompute all the forces before any update.
Your problem with the orbital of Earth is because the Center of Gravity of the System Earth-Sun, if you want to see the Orbital stay in loops you need to set center Of Gravity In (x,y,z)=(0,0,0) ;
I have a C# code based on your code Above so :
public partial class Form1 : Form
{
static
int
x1,y1,x2,y2, x3, y3;//for the 3th particule
private void timer1_Tick_1(object sender, EventArgs e)
{Moveu();
Invalidate();
}
private void button1_Click_1(object sender, EventArgs e)
{
timer1.Enabled = !timer1.Enabled;
}
public Form1()
{
InitializeComponent();
Paint += new PaintEventHandler(paint);
MouseDown += new MouseEventHandler(mouse_Click);
MouseUp += new MouseEventHandler(mouse_up);
MouseMove += new MouseEventHandler(mouse_move);
// x y z vx vy vz m
particles[0] = new Particle( 0, 0, 0, 0, 0, 0, 1 ) ; //sun
particles[1] = new Particle( 1, 0, 0, 0, 6, 0, 0.03 ); //earth
// particles[2] = new Particle( 0, 2, 0, 0, 0, 0, 1 ); //planet
x1 = (int)(100 * particles[0].r[0] + 300);
y1 = (int)(100 * particles[0].r[1] + 300);
x2 = (int)(100 * particles[1].r[0] + 300);
y2 = (int)(100 * particles[1].r[1] + 300);
}
Particle[] particles = new Particle[2];
void Moveu()
{
double h, t;
// setting initial values, step size and max time tmax
h = 0.005; // the step size in years
// initial time
t = 0;
//updates position of --all-- particles ( z=0 not z=1 )
for (int z = 0; z < particles.Length; z++)
particles[z].RungeKutta4(particles, t, h, z);
x1 = (int)(100 * particles[0].r[0] + 300); // +300 just for render it in centre
y1 = (int)(100 * particles[0].r[1] + 300);
x2 = (int)(100 * particles[1].r[0] + 300);
y2 = (int)(100 * particles[1].r[1] + 300);
// x3 = (int)(100 * particles[2].r[0] + 300);
// y3 = (int)(100 * particles[2].r[1] + 300);
}
void paint(object s, PaintEventArgs e)
{
Graphics graf;
graf = CreateGraphics();
graf.FillEllipse(new SolidBrush(Color.AntiqueWhite), x1 + move.X, y1 + move.Y, 50, 50);
graf.FillEllipse(new SolidBrush(Color.Blue), x2 + move.X, y2 + move.Y, 10, 10);
// graf.FillEllipse(new SolidBrush(Color.Yellow), x3, y3, 20, 20);
}
class Particle
{
public double[] r; // position vector
public double[] v; // velocity vector
public double mass;
//constructor
public Particle() { }
public Particle(double x, double y, double z, double vx, double vy, double vz, double m)
{
this.r = new double[3];
this.v = new double[3];
this.r[0] = x;
this.r[1] = y;
this.r[2] = z;
this.v[0] = vx;
this.v[1] = vy;
this.v[2] = vz;
this.mass = m;
}
private double acc(double r, Particle[] particles, int particleNumber, double[] r_temp, int l)
{
// dv/dt = f(x) = -G * m_i * (x - x_i) / [(x - x_i)^2 + (y - y_i)^2 + (z - z_i)^2]^(3/2)
double sum = 0;
switch (l)
{
case 0:
for (int i = 0; i < particles.Length; i++)
if (i != particleNumber)
sum += particles[i].mass * (r - particles[i].r[l]) / Math.Pow(Math.Pow(r - particles[i].r[l], 2)
+ Math.Pow(r_temp[1] - particles[i].r[1], 2) + Math.Pow(r_temp[2] - particles[i].r[2], 2), 1.5);
break;
case 1:
for (int i = 0; i < particles.Length; i++)
if (i != particleNumber)
sum += particles[i].mass * (r - particles[i].r[l]) / Math.Pow(Math.Pow(r - particles[i].r[l], 2)
+ Math.Pow(r_temp[0] - particles[i].r[0], 2) + Math.Pow(r_temp[2] - particles[i].r[2], 2), 1.5);
break;
case 2:
for (int i = 0; i < particles.Length; i++)
if (i != particleNumber)
sum += particles[i].mass * (r - particles[i].r[l]) / Math.Pow(Math.Pow(r - particles[i].r[l], 2)
+ Math.Pow(r_temp[0] - particles[i].r[0], 2) + Math.Pow(r_temp[1] - particles[i].r[1], 2), 1.5);
break;
}
return -G * sum;
}
public void RungeKutta4(Particle[] particles, double t, double h, int particleNumber)
{
//current position of the particle is saved in a vector
double[] r_temp = new double[3];
for (int j = 0; j < 3; j++)
r_temp[j] = this.r[j];
//loop going over all the coordinates and updating each using RK4 algorithm
for (int l = 0; l < 3; l++)
{
double[,] k = new double[4, 2];
k[0, 0] = this.v[l]; //k1_r
k[0, 1] = acc(this.r[l], particles, particleNumber, r_temp, l); //k1_v
k[1, 0] = this.v[l] + k[0, 1] * 0.5 * h; //k2_r
k[1, 1] = acc(this.r[l] + k[0, 0] * 0.5 * h, particles, particleNumber, r_temp, l); //k2_v
k[2, 0] = this.v[l] + k[1, 1] * 0.5 * h; //k3_r
k[2, 1] = acc(this.r[l] + k[1, 0] * 0.5 * h, particles, particleNumber, r_temp, l); //k3_v
k[3, 0] = this.v[l] + k[2, 1] * h; //k4_r
k[3, 1] = acc(this.r[l] + k[2, 0] * h, particles, particleNumber, r_temp, l); //k4_v
this.r[l] += (h / 6.0) * (k[0, 0] + 2 * k[1, 0] + 2 * k[2, 0] + k[3, 0]);
this.v[l] += (h / 6.0) * (k[0, 1] + 2 * k[1, 1] + 2 * k[2, 1] + k[3, 1]);
}
}
}
public static double G = 4 * Math.PI * Math.PI; //then time unite in years and length unite = distance between Earth and Sun and masse is the sun masse unite
void mouse_Click(object o, MouseEventArgs e)
{
dwn = new Point(e.X, e.Y);
if ("" + e.Button == "Left")
{
pos = move;
clicked = true;
}
}
void mouse_move(object o, MouseEventArgs e)
{
if (clicked)
{
move = new Point(e.X + pos.X - dwn.X, e.Y + pos.Y - dwn.Y);
Invalidate();
}
}
void mouse_up(object o, MouseEventArgs e)
{
clicked = false;
}
Point dwn, pos,move;
bool clicked;
}`you need to create a Timer and a button [![enter image description here][1]][1]

Categories