I have a code (provided below) which simulates the motion of an ensemble of charged particles which are subjected to a complex static electric field which passes through some aperture in a metallic surface (N spheres, whose number is defined as numbodies
in my code).
Now, I would like to run the simulation for a single particle many, many times, and plot each of their final resting places on the surface (z=-partRad
) of my metalized aperture at (x(tfin), y(tfin), z= -partRad)
together, in 2D. These coordinates can be displayed as simple dots in the x-y plane.
It would also be cool to superimpose the surface of my aperture onto the x-y graph of the final particle locations, to see exactly where they most frequently land. I would also like to find the average distance these particles center from some user specified point(s), after everything is said and done. I would also appreciate any cool ideas for statistical analysis regarding the outcome of the Monte Carlo simulation!!!
Each of the particles should be given a random starting position and velocity given by
vel0 = Table[
Partition[{RandomReal[{-0.0001, 0.0001}],
RandomReal[{-0.0001, 0.0001}], RandomReal[{-0.0001, 0.0001}]}, 1],
numbodies]
pos0 = Table[
Partition[{RandomReal[{0.5, 1}], RandomReal[{0.5, 1}],
RandomReal[{-0.4, -0.04}]}, 1], numbodies];
in my code.
The first part of the code, above the (***... MONTE CARLO CAN START...***)
line in my code, doesn't need to be included in the Monte Carlo simulation (unless you find some reason I do not see).
Here is the code. Happy hunting!
Clear["Global`*"];
Needs["NDSolve`FEM`"]
q = 1.617733*10^-18;(*Net ion Charge*)
R = Import[
"https://www.dropbox.com/s/dds8rm3odg2m7gu/largeAp.obj?dl=1"];
RegionDimension[R];
M = BoundaryMeshRegion[MeshCoordinates[R], MeshCells[R, 2]];
RegionDimension[M];
Volume[M];
r = RegionDifference[
RegionDifference[
RegionDifference[Cuboid[{0, 0, -6.5}, {2, 2, 0.5}], M],
Cuboid[{0, 0, 0.4}, {2, 2, 0.5}]],
Cuboid[{0, 0, -6.5}, {2, 2, -6.4}]];
ToElementMesh[r]["Wireframe"];
pol = -1;
V0 = 1000;
sol = NDSolveValue[{Laplacian[V[x, y, z], {x, y, z}] == 0,
DirichletCondition[
V[x, y, z] == -pol*
V0/2, (0.4 <= z <= 0.5) && (0 <= y <= 2) && (0 <= x <= 2)],
DirichletCondition[
V[x, y, z] ==
pol*V0/2, (0.0071 <= z <= 0.0072) && (0 <= y <= 2) && (0 <= x <=
2)], DirichletCondition[
V[x, y, z] ==
0, (0 <= z <= 0.0070) && (0 <= y <= 2) && (0 <= x <= 2)],
DirichletCondition[
V[x, y, z] ==
0, (-6.5 <= z <= -6.4) && (0 <= y <= 2) && (0 <= x <= 2)]},
V, {x, y, z} \[Element] r];
electricField[x_, y_, z_] = -Grad[sol[x, y, z], {x, y, z}];
v = Show[VectorPlot3D[
electricField[x, y, z], {x, 0.5, 1}, {y, 0.5, 1}, {z, -0.5, 0.1},
PlotTheme -> "Detailed", ColorFunction -> "Rainbow",
PerformanceGoal -> "Quality", VectorScale -> 0.05,
VectorPoints -> 7, PlotLegends -> Automatic], M];
mass = 6.52*10^-11;(*particle mass in kg/m^3*)
eforceX[x_, y_, z_] = q*Grad[sol[x, y, z], {x, y, z}][[1]];
eforceY[x_, y_, z_] = q*Grad[sol[x, y, z], {x, y, z}][[2]];
eforceZ[x_, y_, z_] = q*Grad[sol[x, y, z], {x, y, z}][[3]];
(*********************** MONTE CARLO CAN START ******************************)
numbodies = 1;
vel0 = Table[
Partition[{RandomReal[{-0.0001, 0.0001}],
RandomReal[{-0.0001, 0.0001}], RandomReal[{-0.0001, 0.0001}]}, 1],
numbodies]
pos0 = Table[
Partition[{RandomReal[{0.5, 1}], RandomReal[{0.5, 1}],
RandomReal[{-0.4, -0.04}]}, 1], numbodies];
tfin = 1000;
eqs = Table[{x1[j]''[t] == -a[t]*(1/mass)*eforceX[x, y, z] /. {x ->
x1[j][t], y -> y1[j][t], z -> z1[j][t]},
y1[j]''[t] == -a[t]*(1/mass)*eforceY[x, y, z] /. {x -> x1[j][t],
y -> y1[j][t], z -> z1[j][t]},
z1[j]''[t] == -a[t]*(1/mass)*eforceZ[x, y, z] /. {x -> x1[j][t],
y -> y1[j][t], z -> z1[j][t]}, x1[j][0] == pos0[[j, 1, 1]],
y1[j][0] == pos0[[j, 2, 1]], z1[j][0] == pos0[[j, 3, 1]],
x1[j]'[0] == vel0[[j, 1, 1]], y1[j]'[0] == vel0[[j, 2, 1]],
z1[j]'[0] == vel0[[j, 3, 1]], a[0] == 1}, {j, numbodies}];
partRad = 0.0125;
vars = Flatten[Table[{x1[j][t], y1[j][t], z1[j][t]}, {j, numbodies}]];
event = Table[
WhenEvent[
z1[j][t] >= -partRad, {a[t] -> 0, x1[j]'[t] -> 0, y1[j]'[t] -> 0,
z1[j]'[t] -> 0}] /. {j -> i}, {i, numbodies}];
sol1 = NDSolve[{eqs, event}, Head /@ vars, {t, 0, tfin},
DiscreteVariables -> a]
frames = Table[
Show[v, ParametricPlot3D[
Table[{x1[j][t], y1[j][t], z1[j][t]} /. sol1, {j,
numbodies}], {t, 0, tf}, PlotRange -> All, Axes -> Off],
Graphics3D[
Table[{Hue[.35],
Sphere[{x1[j][tf], y1[j][tf], z1[j][tf]} /. sol1, 0.025]}, {j,
numbodies}]]], {tf, .015*tfin, tfin, .015*tfin}];
ListAnimate[Rasterize[#, "Image"] & /@ frames]