Performance optimization

This commit is contained in:
Alexander Nozik 2018-12-03 13:40:35 +03:00
parent 38eb54873f
commit c2ad326a77
3 changed files with 61 additions and 37 deletions

View File

@ -4,6 +4,8 @@
\.rej$ \.rej$
\.conflict\~$ \.conflict\~$
.gradle/ .gradle/
output/
notebooks/.ipynb_checkpoints
.ipynb_checkpoints .ipynb_checkpoints
build/ build/
.idea/* .idea/*

View File

@ -1,30 +1,25 @@
package inr.numass.trapping package inr.numass.trapping
import org.apache.commons.math3.analysis.UnivariateFunction
import org.apache.commons.math3.analysis.interpolation.LinearInterpolator import org.apache.commons.math3.analysis.interpolation.LinearInterpolator
import org.apache.commons.math3.random.JDKRandomGenerator
import org.apache.commons.math3.random.RandomGenerator
import org.apache.commons.rng.UniformRandomProvider import org.apache.commons.rng.UniformRandomProvider
import org.apache.commons.rng.simple.RandomSource import org.apache.commons.rng.simple.RandomSource
import java.io.File
import java.io.* import java.io.PrintStream
import java.nio.file.Files
import java.nio.file.StandardOpenOption
import java.util.stream.Stream import java.util.stream.Stream
/** /**
* Created by darksnake on 04-Jun-16. * Created by darksnake on 04-Jun-16.
*/ */
class SimulationManager { class SimulationManager() {
var outputDirectory: File = File("./output")
var fileName = "trap[test]"
var comment = ""
var generator: UniformRandomProvider = RandomSource.create(RandomSource.SPLIT_MIX_64) var generator: UniformRandomProvider = RandomSource.create(RandomSource.SPLIT_MIX_64)
/**
* output for accepted events
*/
var output: PrintStream = System.out
/**
* output for statistics
*/
var statisticOutput = System.out
var reportFilter: (Simulator.SimulationResult) -> Boolean = { it.state == Simulator.EndState.ACCEPTED } var reportFilter: (Simulator.SimulationResult) -> Boolean = { it.state == Simulator.EndState.ACCEPTED }
var initialE = 18000.0 var initialE = 18000.0
@ -51,14 +46,14 @@ class SimulationManager {
return Math.acos(1 - 2 * x) return Math.acos(1 - 2 * x)
} }
fun setOutputFile(fileName: String) { // fun setOutputFile(fileName: String) {
val outputFile = File(fileName) // val outputFile = File(fileName)
if (!outputFile.exists()) { // if (!outputFile.exists()) {
outputFile.parentFile.mkdirs() // outputFile.parentFile.mkdirs()
outputFile.createNewFile() // outputFile.createNewFile()
} // }
this.output = PrintStream(FileOutputStream(outputFile)) // this.output = PrintStream(FileOutputStream(outputFile))
} // }
fun withReportFilter(filter: (Simulator.SimulationResult) -> Boolean): SimulationManager { fun withReportFilter(filter: (Simulator.SimulationResult) -> Boolean): SimulationManager {
@ -91,10 +86,27 @@ class SimulationManager {
*/ */
@Synchronized @Synchronized
fun simulateAll(num: Number): Counter { fun simulateAll(num: Number): Counter {
if (!outputDirectory.exists()) {
outputDirectory.mkdirs()
}
val outputPath = outputDirectory.toPath().resolve("$fileName.out")
val output = PrintStream(Files.newOutputStream(outputPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE))
val counter = Counter() val counter = Counter()
val simulator = Simulator(eLow, thetaTransport, thetaPinch, gasDensity, bSource, magneticField, RandomGeneratorBridge(generator)) val simulator = Simulator(eLow, thetaTransport, thetaPinch, gasDensity, bSource, magneticField, RandomGeneratorBridge(generator))
System.out.printf("%nStarting sumulation with initial energy %g and %d electrons.%n%n", initialE, num.toLong()) val header = """
E_init = $initialE;
E_low = $eLow;
theta_pinch = $thetaPinch;
theta_transport = $thetaTransport;
density = $gasDensity;
""".trimIndent() + comment
output.println(header.replace("\n", "\n# "))//adding comment symbols
System.out.printf("%nStarting simulation with initial energy %g and %d electrons.%n%n", initialE, num.toLong())
output.printf("%s\t%s\t%s\t%s\t%s\t%s%n", "E", "theta", "theta_start", "colNum", "L", "state") output.printf("%s\t%s\t%s\t%s\t%s\t%s%n", "E", "theta", "theta_start", "colNum", "L", "state")
Stream.generate { getRandomTheta() }.limit(num.toLong()).parallel() Stream.generate { getRandomTheta() }.limit(num.toLong()).parallel()
.forEach { theta -> .forEach { theta ->
@ -105,6 +117,10 @@ class SimulationManager {
} }
counter.count(res) counter.count(res)
} }
val statisticsPath = outputDirectory.toPath().resolve("$fileName.stat")
val statisticOutput = PrintStream(Files.newOutputStream(statisticsPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE))
statisticOutput.println(header + "\n")
printStatistics(statisticOutput, simulator, counter) printStatistics(statisticOutput, simulator, counter)
return counter return counter
} }

View File

@ -1,6 +1,7 @@
package inr.numass.trapping package inr.numass.trapping
import org.apache.commons.rng.simple.RandomSource import org.apache.commons.rng.simple.RandomSource
import java.io.File
import java.time.Duration import java.time.Duration
import java.time.Instant import java.time.Instant
@ -10,20 +11,25 @@ fun main(args: Array<String>) {
//val b = doubleArrayOf(3.70754, 0.62786, 0.60474, 0.60325, 0.60333, 0.60503, 0.6285, 3.70478) //val b = doubleArrayOf(3.70754, 0.62786, 0.60474, 0.60325, 0.60333, 0.60503, 0.6285, 3.70478)
// System.out.println("Press any key to start..."); // System.out.println("Press any key to start...");
// System.in.read(); // System.in.read();
val startTime = Instant.now()
System.out.printf("Starting at %s%n%n", startTime.toString()) val es = listOf(12000.0, 14000.0,16000.0,18000.0)
SimulationManager().apply {
generator = RandomSource.create(RandomSource.SPLIT_MIX_64)
setOutputFile("D:\\Work\\Numass\\trapping\\test1.out")
setFields(0.6, 3.7, 7.2)
gasDensity = 1e19
initialE = 14000.0
range = 4000.0
}.simulateAll(1e6)
val finishTime = Instant.now() for(e in es) {
System.out.printf("%nFinished at %s%n", finishTime.toString()) val startTime = Instant.now()
System.out.printf("Calculation took %s%n", Duration.between(startTime, finishTime).toString())
System.out.printf("Starting at %s%n%n", startTime.toString())
SimulationManager().apply {
fileName = "trap[regular]"
generator = RandomSource.create(RandomSource.SPLIT_MIX_64)
setFields(0.6, 3.7, 7.2)
gasDensity = 1e19
initialE = e
range = 4000.0
}.simulateAll(1e6)
val finishTime = Instant.now()
System.out.printf("%nFinished at %s%n", finishTime.toString())
System.out.printf("Calculation took %s%n", Duration.between(startTime, finishTime).toString())
}
} }