public Post(Vector start, Vector axis, double wireDiameter, PseudoDES pdes) { Start = start; WireRadius = wireDiameter / 2.0; pd = pdes; Vector[] perps = axis.GetPerpendiculars(); Axis = axis; perpX = perps[0]; perpY = perps[1]; }
public Ring(Vector center, Vector axis, double radius, PseudoDES pdes) { Center = center; Axis = axis; Radius = radius; pd = pdes; Vector[] perps = Axis.GetPerpendiculars(); perpX = perps[0]; perpY = perps[1]; }
public Torus(Vector center, Vector axis, double radius, double wireDiameter, PseudoDES pdes) { Center = center; Radius = radius; WireRadius = wireDiameter / 2.0; pd = pdes; Vector[] perps = axis.GetPerpendiculars(); Axis = axis.Normalize(); perpX = perps[0]; perpY = perps[1]; }
// Return a random point on the triangle given a random generator. public Vector RandomPoint(PseudoDES rand) { double r1 = rand.RandomDouble(); double r2 = rand.RandomDouble(); if (r1 + r2 > 1.0) { r1 = 1.0 - r1; r2 = 1.0 - r2; } return(A + r1 * AtoB + r2 * AtoC); }
double cos1, cos2; // Extents of cos(theta) where theta is measured from the north pole. public Sphere(Vector center, Vector axis, double radius, double north, double south, PseudoDES pdes) { Center = center; Radius = radius; pd = pdes; Vector[] perps = axis.GetPerpendiculars(); Axis = axis.Normalize(); perpX = perps[0]; perpY = perps[1]; if (north > 90.0 || south < -90.0 || south >= north) { throw new ArgumentOutOfRangeException("Invalid latitude cutoff"); } cos1 = (south == -90.0) ? -1.0 : Math.Sin(south * Math.PI / 180.0); cos2 = (north == 90.0) ? 1.0 : Math.Sin(north * Math.PI / 180.0); }
static public Shape FromDictionary(Dictionary <string, string> dict, PseudoDES pd) { Shape shape = null; switch (dict["type"]) { case "TorusSegment": shape = new TorusSegment(dict); break; default: throw new Exception(String.Format("Unknown shape type {0}", dict["type"])); } shape.pd = pd; return(shape); }
public Surface(StlFile stl, PseudoDES rand) { this.rand = rand; List <SurfTriangle> triangleList = new List <SurfTriangle>(); foreach (Face f in stl) { triangleList.Add(new SurfTriangle(f)); } triangles = triangleList.ToArray(); double sum = 0.0; List <double> areas = new List <double>(); foreach (SurfTriangle t in triangles) { sum += t.Area; areas.Add(sum); } cummulativeAreas = areas.ToArray(); TotalArea = sum; }
static void Main(string[] args) { if (!ReadArgs(args)) { return; } if (xmlFileName == null) { Console.WriteLine("No XML configuration was provided."); return; } if (!xmlFileName.EndsWith(".xml", ignoreCase: true, culture: System.Globalization.CultureInfo.CurrentCulture)) { xmlFileName = xmlFileName + ".xml"; } if (outFile != null) { tw = new StreamWriter(outFile); } WriteLine("Command line: efield {0}", String.Join(" ", args)); DateTime startTime = DateTime.Now; WriteLine(startTime.ToString()); if (!haveSeed) { seed = (uint)startTime.Ticks; } pdes = new PseudoDES(0, seed); WriteLine("Seed = {0}", seed); try { SetupGeometryFromXml(xmlFileName); } catch (Exception e) { Console.WriteLine("ERROR: {0}", e.Message); return; } WriteLine("Cutoff c = {0:0.0000}", c); // Fill the geometry elements with particles. posParticles = new Particle[nParticles]; negParticles = new Particle[nParticles]; for (int i = 0; i < nParticles; i++) { posParticles[i] = PlaceParticleRandomly(posElements); negParticles[i] = PlaceParticleRandomly(negElements); } // Trap ^C Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress); // Do the requested number of repetitions. for (int iRep = 0; iRep < nReps && !cancelWasReceived; iRep++) { WriteLine("Rep: {0}", iRep + 1); // Loop over all particles for (int i = 0; i < nParticles; i++) { // A positive particle double phi = Update(i, posParticles, negParticles, posElements, "+"); // A negative particle phi = Update(i, negParticles, posParticles, negElements, "-"); if (cancelWasReceived) { break; } } } DateTime endTime = DateTime.Now; TimeSpan runTime = endTime - startTime; if (cancelWasReceived) { WriteLine("Received ^C, terminating early!"); } WriteLine("Run time = {0:0.000} minutes", runTime.TotalMinutes); // Dump all the particles WriteLine("\nPositive particles"); double sumPos = 0.0; for (int i = 0; i < nParticles; i++) { Vector pos = posParticles[i].Position; double phi = ComputePotential(pos, posParticles, i) - ComputePotential(pos, negParticles, -1); sumPos += phi; WriteLine("{0,10:0.00000}{1,10:0.00000}{2,10:0.00000} {3,11:0.000e+00}", pos.x, pos.y, pos.z, phi); } WriteLine("\nNegative particles"); double sumNeg = 0.0; for (int i = 0; i < nParticles; i++) { Vector pos = negParticles[i].Position; double phi = ComputePotential(pos, posParticles, -1) - ComputePotential(pos, negParticles, i); sumNeg += phi; WriteLine("{0,10:0.00000}{1,10:0.00000}{2,10:0.00000} {3,11:0.000e+00}", pos.x, pos.y, pos.z, phi); } WriteLine("{0,11:0.000e+00} {1,11:0.000e+00}", sumPos / nParticles, sumNeg / nParticles); if (tw != null) { tw.Close(); } }
static void Main(string[] args) { if (!ReadArgs(args)) { return; } try { if (outFile != null) { tw = new StreamWriter(outFile); } //WriteLine("Command line: efield {0}", String.Join(" ", args)); WriteLine("Command line: {0}", Environment.CommandLine); WriteLine("Run by: {0}\\{1} on {2}", Environment.UserDomainName, Environment.UserName, Environment.MachineName); DateTime startTime = DateTime.Now; WriteLine(startTime.ToString()); if (!haveSeed) { seed = (uint)startTime.Ticks; } PseudoDES rand = new PseudoDES(0, seed); WriteLine("Seed = {0}", seed); if (!File.Exists(posFileName)) { throw new Exception(string.Format("Anode description file {0} not found", posFileName)); } if (!File.Exists(negFileName)) { throw new Exception(string.Format("Cathode description file {0} not found", negFileName)); } Surface anode = new Surface(new StlFile(posFileName), rand); Surface cathode = new Surface(new StlFile(negFileName), rand); WriteLine("Anode: {0} Triangles: {1} Area: {2:0.0000} m^2", posFileName, anode.TriangleCount, anode.TotalArea); WriteLine("Cathode: {0} Triangles: {1} Area: {2:0.0000} m^2", negFileName, cathode.TriangleCount, cathode.TotalArea); // Trap ^C Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress); // Create the Balancer. Balancer baal = new Balancer(nParticles, anode, cathode); // Let's see the starting potentials. double[] pots = baal.GetAveragePotentials(); WriteLine("Steps: {0,5} Potentials: {1:0.00e00} {2:0.00e00} {3:0.00e00} {4:0.00e00}", 0, pots[0], pots[1], pots[2], pots[3]); int batch = 10; for (int nDone = 0; nDone < nReps; nDone += batch) { // Run some steps. baal.DoSteps(Math.Min(batch, nReps - nDone)); // And report. pots = baal.GetAveragePotentials(); WriteLine("Steps: {0,5} Potentials: {1:0.00e00} {2:0.00e00} {3:0.00e00} {4:0.00e00}", Math.Min(nDone + batch, nReps), pots[0], pots[1], pots[2], pots[3]); if (cancelWasReceived) { break; } } DateTime endTime = DateTime.Now; TimeSpan runTime = endTime - startTime; if (cancelWasReceived) { WriteLine("Received ^C, terminating early!"); } WriteLine("Run time = {0:0.000} minutes", runTime.TotalMinutes); double potentialDifference = pots[0] - pots[2]; double posSdev = 100.0 * pots[1] / Math.Abs(pots[0]); double negSdev = 100.0 * pots[3] / Math.Abs(pots[2]); double capacitance = 1e12 / potentialDifference; // in picoFarads WriteLine("Final potentials: {0:0.00e00} ({1:0.0}%) {2:0.00e00} ({3:0.0}%)", pots[0], posSdev, pots[2], negSdev); WriteLine("Final potential difference: {0:0.00e00}", potentialDifference); WriteLine("Capacitance: {0:0.0} pF", capacitance); // If writing to a file, dump the final particle positions if (tw != null) { if (xN > 0) { double step = (x1 - x0) / xN; double charge = 1.0 / baal.posParticles.Length; tw.WriteLine("Scan along X-axis"); for (int i = 0; i <= xN; i++) { double x = x0 + i * step; // x in mm double phi = EField.GetPotential(new Vector(x * 0.001, 0.0, 0.0), baal.posParticles, baal.posParticles, charge); tw.WriteLine("{0,10:0.00} {1:0.000e00}", x, phi); } } int n = baal.posParticles.Length; tw.WriteLine("particles {0} {0}", n); foreach (Vector x in baal.posParticles) { tw.WriteLine("{0,10:0.0000}{1,10:0.0000}{2,10:0.0000}", x.x, x.y, x.z); } foreach (Vector x in baal.negParticles) { tw.WriteLine("{0,10:0.0000}{1,10:0.0000}{2,10:0.0000}", x.x, x.y, x.z); } } } catch (Exception e) { Console.WriteLine("Exception: {0}", e.Message); } if (tw != null) { tw.Close(); } }
static void Main(string[] args) { bool allPassed = true; if (!ReadArgs(args)) { return; } if (!haveSeed) { seed = (uint)DateTime.UtcNow.Ticks; } PseudoDES pdes = new PseudoDES(0, seed); Console.WriteLine("Random seed = {0}", seed); if (nVectors < 11) { nVectors = 11; } Console.WriteLine("Vectors = {0}", nVectors); // Run the PseudoDES test if (pdes.Test()) { Console.WriteLine("PseudoDES test passed"); } else { Console.WriteLine("PseudoDES test FAILED"); allPassed = false; } Vector[] vecs = new Vector[nVectors]; vecs[0] = new Vector(1.0, 0.0, 0.0); vecs[1] = new Vector(0.0, 1.0, 0.0); vecs[2] = new Vector(0.0, 0.0, 1.0); vecs[3] = new Vector(1.0, 0.0, 0.0); vecs[4] = new Vector(0.0, 1.0, 0.0); vecs[5] = new Vector(0.0, 0.0, 1.0); vecs[6] = new Vector(1e-15, 1e-11, 1.0); vecs[7] = new Vector(1e-11, 1e-15, -1.0); vecs[8] = new Vector(1e-15, 1e-11, 1.01); vecs[9] = new Vector(1e-11, 1e-15, -1.01); // Fill the rest with random points. for (int i = 10; i < vecs.Length; i++) { vecs[i] = new Vector(pdes.RandomDouble(), pdes.RandomDouble(), pdes.RandomDouble()); } // Test the vector operations. bool vectorsPassed = true; for (int i = 0; i < vecs.Length; i++) { Vector v = vecs[i]; bool passed = true; Vector[] perps = v.GetPerpendiculars(); // All three vectors are perpendicular. passed = IsSmall(Vector.Dot(perps[0], perps[1]), 1e-13, "p0.p1"); passed = passed && IsSmall(Vector.Dot(perps[0], v), 1e-13, "p0.v"); passed = passed && IsSmall(Vector.Dot(v, perps[1]), 1e-13, "v.p1"); // The computed vectors have length 1. passed = passed && IsSmall(Vector.Dot(perps[0], perps[0]) - 1.0, 1e-13, "p0.p0 - 1"); passed = passed && IsSmall(Vector.Dot(perps[1], perps[1]) - 1.0, 1e-13, "p1.p1 - 1"); // The vectors form a right-handed system. double len = Math.Sqrt(Vector.Dot(v, v)); Vector cross = Vector.Cross(perps[0], perps[1]); passed = passed && IsSmall(Vector.Dot(cross, v) / len - 1.0, 1e-13, "(p0xp1.v)/sqrt(v.v) - 1"); if (!passed) { Console.WriteLine("Vector test FAILED for i={0}", i); vectorsPassed = false; Console.WriteLine(" v = {0}", v); Console.WriteLine(" p0 = {0}", perps[0]); Console.WriteLine(" p1 = {0}", perps[1]); } } if (vectorsPassed) { Console.WriteLine("Vector test passed"); } else { Console.WriteLine("Vector test FAILED"); allPassed = false; } if (allPassed) { Console.WriteLine("All unit tests passed"); } else { Console.WriteLine("Some unit tests FAILED"); } }