Search in sources :

Example 1 with SystemCalculator

use of org.openlca.core.math.SystemCalculator in project olca-modules by GreenDelta.

the class MatrixExceptionTest method testSingularSystem.

@Test
public void testSingularSystem() {
    var db = Tests.getDb();
    var units = db.insert(UnitGroup.of("Mass units", "kg"));
    var mass = db.insert(FlowProperty.of("Mass", units));
    var p = db.insert(Flow.product("p", mass));
    var q = db.insert(Flow.product("q", mass));
    var pp = Process.of("pp", p);
    pp.input(q, 1);
    db.insert(pp);
    var qq = Process.of("qq", q);
    qq.input(p, 1);
    db.insert(qq);
    var sys = ProductSystem.of(pp);
    sys.link(qq, pp);
    sys.link(pp, qq);
    db.insert(sys);
    boolean caughtIt = false;
    assertTrue(Julia.load());
    try {
        var setup = CalculationSetup.simple(sys);
        new SystemCalculator(db).calculateSimple(setup);
    } catch (SingularMatrixException e) {
        caughtIt = true;
    }
    assertTrue(caughtIt);
    db.delete(sys, pp, qq, p, q, mass, units);
}
Also used : SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException) SystemCalculator(org.openlca.core.math.SystemCalculator) Test(org.junit.Test)

Example 2 with SystemCalculator

use of org.openlca.core.math.SystemCalculator in project olca-modules by GreenDelta.

the class DirectCalculationTest method testSimpleProcess.

@Test
public void testSimpleProcess() {
    // reference data
    var units = db.insert(UnitGroup.of("Mass units", Unit.of("kg")));
    var mass = db.insert(FlowProperty.of("Mass", units));
    var steel = db.insert(Flow.product("steel", mass));
    var co2 = db.insert(Flow.elementary("CO2", mass));
    // process
    var process = Process.of("process", steel);
    process.output(co2, 2).dqEntry = "(1;2;3;4;5)";
    process.exchangeDqSystem = dqSystem();
    process = db.insert(process);
    // calculation
    var setup = CalculationSetup.fullAnalysis(process);
    var calculator = new SystemCalculator(Tests.getDb());
    var result = calculator.calculateFull(setup);
    var dqSetup = DQCalculationSetup.of(setup);
    var dqResult = DQResult.of(db, dqSetup, result);
    // check the result; note that there could be some
    // artifact flows from other tests in the result;
    // this we first find the entry for CO2 in the
    // result index
    EnviFlow co2IdxFlow = null;
    for (var f : result.enviIndex()) {
        if (f.flow().id == co2.id) {
            co2IdxFlow = f;
            break;
        }
    }
    var dq = dqResult.get(co2IdxFlow);
    Assert.assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, dq);
    // delete artifacts
    db.delete(process, process.exchangeDqSystem, co2, steel, mass, units);
}
Also used : EnviFlow(org.openlca.core.matrix.index.EnviFlow) SystemCalculator(org.openlca.core.math.SystemCalculator) Test(org.junit.Test)

Example 3 with SystemCalculator

use of org.openlca.core.math.SystemCalculator 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 4 with SystemCalculator

use of org.openlca.core.math.SystemCalculator in project olca-modules by GreenDelta.

the class ResultService method calculate.

@Override
public void calculate(ProtoCalculationSetup req, StreamObserver<ProtoResultRef> resp) {
    var setup = CalculationSetupReader.read(DbEntityResolver.of(db), req);
    if (setup == null) {
        resp.onError(Status.INVALID_ARGUMENT.withDescription("invalid calculation setup").asException());
        return;
    }
    var result = new SystemCalculator(db).calculateFull(setup);
    var key = UUID.randomUUID().toString();
    results.put(key, result);
    var r = ProtoResultRef.newBuilder().setId(key).build();
    resp.onNext(r);
    resp.onCompleted();
}
Also used : SystemCalculator(org.openlca.core.math.SystemCalculator)

Example 5 with SystemCalculator

use of org.openlca.core.math.SystemCalculator in project olca-modules by GreenDelta.

the class CalculationExample method main.

public static void main(String[] args) {
    Julia.load();
    try (var db = Derby.fromDataDir("ei22")) {
        var system = db.get(ProductSystem.class, "7d1cbce0-b5b3-47ba-95b5-014ab3c7f569");
        var method = new ImpactMethodDao(db).getForRefId("207ffac9-aaa8-401d-ac90-874defd3751a");
        var setup = CalculationSetup.fullAnalysis(system).withImpactMethod(method);
        var calc = new SystemCalculator(db);
        var r = calc.calculateFull(setup);
        var f = r.enviIndex().at(0);
        System.out.println(f.flow().name + "  -> " + r.getTotalFlowResult(f));
        var impact = r.impactIndex().at(0);
        System.out.println(impact.name + "  -> " + r.getTotalImpactResult(impact));
    }
}
Also used : ImpactMethodDao(org.openlca.core.database.ImpactMethodDao) SystemCalculator(org.openlca.core.math.SystemCalculator)

Aggregations

SystemCalculator (org.openlca.core.math.SystemCalculator)11 Test (org.junit.Test)5 File (java.io.File)2 Derby (org.openlca.core.database.Derby)2 Process (org.openlca.core.model.Process)2 ArrayList (java.util.ArrayList)1 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)1 IDatabase (org.openlca.core.database.IDatabase)1 ImpactMethodDao (org.openlca.core.database.ImpactMethodDao)1 ProcessDao (org.openlca.core.database.ProcessDao)1 LibTechIndex (org.openlca.core.library.LibTechIndex)1 LibraryDir (org.openlca.core.library.LibraryDir)1 DQResult (org.openlca.core.math.data_quality.DQResult)1 ProductSystemBuilder (org.openlca.core.matrix.ProductSystemBuilder)1 MatrixCache (org.openlca.core.matrix.cache.MatrixCache)1 EnviFlow (org.openlca.core.matrix.index.EnviFlow)1 TechIndex (org.openlca.core.matrix.index.TechIndex)1 LinkingConfig (org.openlca.core.matrix.linking.LinkingConfig)1 JavaSolver (org.openlca.core.matrix.solvers.JavaSolver)1 ImpactDescriptor (org.openlca.core.model.descriptors.ImpactDescriptor)1