diff --git a/src/main/java/hep/dataforge/trapping/ElectronTrappingSimulator.java b/src/main/java/hep/dataforge/trapping/ElectronTrappingSimulator.java index 4aab6fd..38f4ef5 100644 --- a/src/main/java/hep/dataforge/trapping/ElectronTrappingSimulator.java +++ b/src/main/java/hep/dataforge/trapping/ElectronTrappingSimulator.java @@ -4,7 +4,10 @@ */ package hep.dataforge.trapping; -import java.util.ArrayList; +import java.io.PrintStream; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.math3.geometry.euclidean.threed.Rotation; import org.apache.commons.math3.geometry.euclidean.threed.SphericalCoordinates; import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; @@ -52,15 +55,14 @@ public class ElectronTrappingSimulator { if (initTheta < this.thetaPinch) { if (generator.heads()) { - return new SimulaionResult(EndState.PASS, initEnergy, initTheta, 0); + return new SimulaionResult(EndState.PASS, initEnergy, initTheta, initTheta, 0); } else { - return new SimulaionResult(EndState.REJECTED, initEnergy, initTheta, 0); + return new SimulaionResult(EndState.REJECTED, initEnergy, initTheta, initTheta, 0); } - } else if(initTheta simulateAll(double E, int num) { - ArrayList res = new ArrayList(); - double theta; - + public List simulateAll(double E, int num) { System.out.printf("%nStarting sumulation with initial energy %g and %d electrons.%n%n", E, num); - for (int i = 0; i < num; i++) { -// System.out.printf("Running electron number %d", i); - theta = this.getRandomTheta(); - res.add(this.simulateOne(E, theta)); - } - return res; + + return Stream.generate(() -> getRandomTheta()).limit(num).parallel() + .map(theta -> simulateOne(E, theta)) + .collect(Collectors.toList()); + } + + public static void printOne(PrintStream out, SimulaionResult res) { + out.printf("%g\t%g\t%g\t%d\t%s%n", res.E, res.theta * 180 / Math.PI, res.initTheta * 180 / Math.PI, res.collisionNumber, res.state.toString()); } private double normalizeTheta(double theta) { @@ -201,14 +204,16 @@ public class ElectronTrappingSimulator { public class SimulaionResult { - public SimulaionResult(EndState state, double E, double theta, int collisionNumber) { + public SimulaionResult(EndState state, double E, double theta, double initTheta, int collisionNumber) { this.state = state; this.E = E; this.theta = theta; + this.initTheta = initTheta; this.collisionNumber = collisionNumber; } public EndState state; public double E; + public double initTheta; public double theta; public int collisionNumber; } diff --git a/src/main/java/hep/dataforge/trapping/Trapping.java b/src/main/java/hep/dataforge/trapping/Trapping.java index dea0a16..70cdc9b 100644 --- a/src/main/java/hep/dataforge/trapping/Trapping.java +++ b/src/main/java/hep/dataforge/trapping/Trapping.java @@ -2,9 +2,8 @@ package hep.dataforge.trapping; import java.io.File; import java.io.FileNotFoundException; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.Iterator; +import java.io.PrintStream; +import java.util.List; /** * Hello world! @@ -13,10 +12,10 @@ import java.util.Iterator; public class Trapping { public static void main(String[] args) throws FileNotFoundException { - PrintWriter out = null; + PrintStream out = null; if ((args.length > 0) && (args[0] != null)) { File file = new File(args[0]); - out = new PrintWriter(file); + out = new PrintStream(file); } else { } @@ -33,18 +32,15 @@ public class Trapping { int rejected = 0; int lowE = 0; - ArrayList results = simulator.simulateAll(E, 50000); - - for (Iterator it = results.iterator(); it.hasNext();) { - ElectronTrappingSimulator.SimulaionResult res = it.next(); + List results = simulator.simulateAll(E, 500000); + for (ElectronTrappingSimulator.SimulaionResult res : results) { if (out != null) { - out.printf("%g\t%g\t%d\t%s%n", res.E, res.theta * 180 / Math.PI, res.collisionNumber, res.state.toString()); + ElectronTrappingSimulator.printOne(System.out, res); } switch (res.state) { - case ACCEPTED: - System.out.printf("%g\t%g\t%d\t%s%n", res.E, res.theta * 180 / Math.PI, res.collisionNumber, res.state.toString()); +// ElectronTrappingSimulator.printOne(System.out, res); accepted++; break; case LOWENERGY: diff --git a/src/main/java/hep/dataforge/trapping/TrappingRandomGenerator.java b/src/main/java/hep/dataforge/trapping/TrappingRandomGenerator.java index 761a1dc..f7786ed 100644 --- a/src/main/java/hep/dataforge/trapping/TrappingRandomGenerator.java +++ b/src/main/java/hep/dataforge/trapping/TrappingRandomGenerator.java @@ -4,8 +4,9 @@ */ package hep.dataforge.trapping; -import org.apache.commons.math3.random.JDKRandomGenerator; +import org.apache.commons.math3.random.MersenneTwister; import org.apache.commons.math3.random.RandomGenerator; +import org.apache.commons.math3.random.SynchronizedRandomGenerator; /** * @@ -15,7 +16,7 @@ public class TrappingRandomGenerator { RandomGenerator generator; public TrappingRandomGenerator() { - this.generator = new JDKRandomGenerator(); + this.generator = new SynchronizedRandomGenerator(new MersenneTwister()); } public TrappingRandomGenerator(RandomGenerator generator) {