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);
}
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);
}
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());
}
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();
}
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));
}
}
Aggregations