Search in sources :

Example 1 with JavaSolver

use of org.openlca.core.matrix.solvers.JavaSolver in project olca-modules by GreenDelta.

the class SimulatorTest method testImpactParam.

@Test
public void testImpactParam() {
    // create a simple model with an uncertain
    // parameter in a LCIA category
    Process p = TestProcess.refProduct("p", 1.0, "kg").elemOut("CH4", 1.0, "kg").get();
    ProductSystem s = TestSystem.of(p).get();
    ImpactMethod m = TestData.method("method", TestData.impact("GWP").factor("CH4", "1 * param", "kg").parameter("param", Uncertainty.uniform(22, 26)).get());
    // create the simulator
    var setup = CalculationSetup.monteCarlo(s, 100).withImpactMethod(m);
    var simulator = Simulator.create(setup, db).withSolver(new JavaSolver());
    // check the simulation results
    for (int i = 0; i < 100; i++) {
        SimpleResult r = simulator.nextRun();
        double[] impacts = r.totalImpactResults();
        Assert.assertEquals(1, impacts.length);
        double val = impacts[0];
        Assert.assertTrue(val >= 22 && val <= 26);
    }
    Arrays.asList(s, m, p).forEach(db::delete);
}
Also used : JavaSolver(org.openlca.core.matrix.solvers.JavaSolver) ProductSystem(org.openlca.core.model.ProductSystem) Process(org.openlca.core.model.Process) TestProcess(org.openlca.core.TestProcess) SimpleResult(org.openlca.core.results.SimpleResult) ImpactMethod(org.openlca.core.model.ImpactMethod) Test(org.junit.Test)

Example 2 with JavaSolver

use of org.openlca.core.matrix.solvers.JavaSolver in project olca-modules by GreenDelta.

the class NoForegroundElemFlowsTest method test.

@Test
public void test() throws IOException {
    // init the library
    var tmpDir = Files.createTempDirectory("_olca_tests");
    var libDir = LibraryDir.of(tmpDir.toFile());
    var lib = Library.create(libDir, LibraryInfo.of("testlib", Version.of(1)));
    var db = Tests.getDb();
    // create the reference data
    var units = UnitGroup.of("Units of mass", "kg");
    var mass = FlowProperty.of("Mass", units);
    var e1 = Flow.elementary("e1", mass);
    var e2 = Flow.elementary("e2", mass);
    db.insert(units, mass, e1, e2);
    var enviIndex = EnviIndex.create();
    enviIndex.add(EnviFlow.inputOf(FlowDescriptor.of(e1)));
    enviIndex.add(EnviFlow.outputOf(FlowDescriptor.of(e2)));
    // create stubs for the library processes
    var libProviders = new ArrayList<TechFlow>();
    Process libProcess = null;
    for (int i = 1; i < 4; i++) {
        var product = db.insert(Flow.product("p" + i, mass));
        var process = Process.of("p" + i, product);
        process.library = lib.id();
        db.insert(process);
        libProviders.add(TechFlow.of(process));
        if (i == 2) {
            libProcess = process;
        }
    }
    var techIdx = new TechIndex(libProviders.get(0));
    techIdx.add(libProviders.get(1));
    techIdx.add(libProviders.get(2));
    // create the foreground system
    var product = db.insert(Flow.product("fp", mass));
    var process = Process.of("fp", product);
    process.input(libProcess.quantitativeReference.flow, 0.5);
    db.insert(process);
    var system = ProductSystem.of(process).link(libProcess, process);
    system.targetAmount = 2;
    db.insert(system);
    // create library resources
    LibTechIndex.write(lib, db, techIdx);
    LibEnviIndex.write(lib, db, enviIndex);
    // write the library matrices
    var matrixA = DenseMatrix.of(new double[][] { // p1
    { 1.0, -0.1, 0.0 }, // p2
    { -0.5, 1.0, -0.2 }, // p3
    { 0.0, -0.7, 1.0 } });
    var matrixB = DenseMatrix.of(new double[][] { // e1
    { -3.0, -4.0, -7.0 }, // e2
    { 9.0, 2.0, 3.0 } });
    var solver = new JavaSolver();
    var inv = solver.invert(matrixA);
    var matrixM = solver.multiply(matrixB, inv);
    LibMatrix.A.write(lib, matrixA);
    LibMatrix.B.write(lib, matrixB);
    LibMatrix.INV.write(lib, inv);
    LibMatrix.M.write(lib, matrixM);
    // calculate the results
    var setup = CalculationSetup.fullAnalysis(system);
    var result = new SystemCalculator(db).withLibraryDir(libDir).calculateFull(setup);
    // check the result
    var expected = matrixM.getColumn(1);
    var totals = result.totalFlowResults();
    for (int i = 0; i < expected.length; i++) {
        Assert.assertEquals(expected[i], totals[i], 1e-16);
    }
    db.clear();
    Dirs.delete(libDir.folder());
}
Also used : JavaSolver(org.openlca.core.matrix.solvers.JavaSolver) ArrayList(java.util.ArrayList) TechIndex(org.openlca.core.matrix.index.TechIndex) LibTechIndex(org.openlca.core.library.LibTechIndex) Process(org.openlca.core.model.Process) SystemCalculator(org.openlca.core.math.SystemCalculator) Test(org.junit.Test)

Example 3 with JavaSolver

use of org.openlca.core.matrix.solvers.JavaSolver in project olca-modules by GreenDelta.

the class Tests method getServer.

private static Server getServer() {
    if (server == null) {
        server = new Server(0);
        server.withDefaultHandlers(Derby.createInMemory(), new JavaSolver());
        server.start();
    }
    return server;
}
Also used : JavaSolver(org.openlca.core.matrix.solvers.JavaSolver)

Aggregations

JavaSolver (org.openlca.core.matrix.solvers.JavaSolver)3 Test (org.junit.Test)2 Process (org.openlca.core.model.Process)2 ArrayList (java.util.ArrayList)1 TestProcess (org.openlca.core.TestProcess)1 LibTechIndex (org.openlca.core.library.LibTechIndex)1 SystemCalculator (org.openlca.core.math.SystemCalculator)1 TechIndex (org.openlca.core.matrix.index.TechIndex)1 ImpactMethod (org.openlca.core.model.ImpactMethod)1 ProductSystem (org.openlca.core.model.ProductSystem)1 SimpleResult (org.openlca.core.results.SimpleResult)1