Search in sources :

Example 1 with ProductSystemBuilder

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

the class ModelHandler method createProductSystem.

@Rpc("create/product_system")
public RpcResponse createProductSystem(RpcRequest req) {
    if (req.params == null || !req.params.isJsonObject())
        return Responses.invalidParams("params must be an object with valid processId", req);
    var obj = req.params.getAsJsonObject();
    if (!obj.has("processId") || !obj.get("processId").isJsonPrimitive())
        return Responses.invalidParams("params must be an object with valid processId", req);
    var processId = obj.get("processId").getAsString();
    if (Strings.nullOrEmpty(processId))
        return Responses.invalidParams("params must be an object with valid processId", req);
    var refProcess = new ProcessDao(db).getForRefId(processId);
    if (refProcess == null)
        return Responses.invalidParams("No process found for ref id " + processId, req);
    var system = ProductSystem.of(refProcess);
    system = new ProductSystemDao(db).insert(system);
    var config = new LinkingConfig().preferredType(ProcessType.UNIT_PROCESS);
    if (obj.has("preferredType") && obj.get("preferredType").getAsString().equalsIgnoreCase("lci_result")) {
        config.preferredType(ProcessType.LCI_RESULT);
    }
    config.providerLinking(ProviderLinking.PREFER_DEFAULTS);
    if (obj.has("providerLinking")) {
        if (obj.get("providerLinking").getAsString().equalsIgnoreCase("ignore")) {
            config.providerLinking(ProviderLinking.IGNORE_DEFAULTS);
        } else if (obj.get("providerLinking").getAsString().equalsIgnoreCase("only")) {
            config.providerLinking(ProviderLinking.ONLY_DEFAULTS);
        }
    }
    var builder = new ProductSystemBuilder(MatrixCache.createLazy(db), config);
    builder.autoComplete(system);
    system = ProductSystemBuilder.update(db, system);
    var res = new JsonObject();
    res.addProperty("@id", system.refId);
    return Responses.ok(res, req);
}
Also used : ProcessDao(org.openlca.core.database.ProcessDao) LinkingConfig(org.openlca.core.matrix.linking.LinkingConfig) ProductSystemBuilder(org.openlca.core.matrix.ProductSystemBuilder) JsonObject(com.google.gson.JsonObject) ProductSystemDao(org.openlca.core.database.ProductSystemDao) Rpc(org.openlca.ipc.Rpc)

Example 2 with ProductSystemBuilder

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

the class ProductSystemBuilderExample method main.

public static void main(String[] args) {
    String dbPath = "C:/Users/Besitzer/openLCA-data-1.4/databases/ecoinvent_2_2_unit";
    IDatabase db = new Derby(new File(dbPath));
    // load the reference process of the new product system
    Process p = new ProcessDao(db).getForRefId("81261285-cc4a-3588-8cce-3aabb786d7aa");
    // create and auto-complete the product system
    var config = new LinkingConfig().providerLinking(ProviderLinking.PREFER_DEFAULTS).preferredType(ProcessType.UNIT_PROCESS);
    var system = new ProductSystemBuilder(MatrixCache.createLazy(db), config).build(p);
    // save the product system
    new ProductSystemDao(db).insert(system);
}
Also used : IDatabase(org.openlca.core.database.IDatabase) Derby(org.openlca.core.database.Derby) ProcessDao(org.openlca.core.database.ProcessDao) LinkingConfig(org.openlca.core.matrix.linking.LinkingConfig) ProductSystemBuilder(org.openlca.core.matrix.ProductSystemBuilder) Process(org.openlca.core.model.Process) File(java.io.File) ProductSystemDao(org.openlca.core.database.ProductSystemDao)

Example 3 with ProductSystemBuilder

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

the class ProductSystemInMemoryCalculationExample method main.

public static void main(String[] args) throws Exception {
    // load the database and matrix cache
    String workspace = "C:/Users/Besitzer/openLCA-data-1.4";
    String dbPath = workspace + "/databases/ecoinvent_2_2_unit";
    IDatabase db = new Derby(new File(dbPath));
    MatrixCache mcache = MatrixCache.createLazy(db);
    // load the reference process and create
    // the product system with auto-completion
    // the system is not saved in the database
    Process p = new ProcessDao(db).getForRefId("7ff672e3-a296-30e8-b1bb-a3173711a28b");
    var config = new LinkingConfig().providerLinking(ProviderLinking.PREFER_DEFAULTS).preferredType(ProcessType.UNIT_PROCESS);
    var builder = new ProductSystemBuilder(mcache, config);
    var system = builder.build(p);
    var method = db.get(ImpactMethod.class, "207ffac9-aaa8-401d-ac90-874defd3751a");
    // create the calculation setup
    var setup = CalculationSetup.simple(system).withImpactMethod(method);
    // load the native library and calculate the result
    // TODO: load Julia libraries first here
    SystemCalculator calc = new SystemCalculator(db);
    SimpleResult r = calc.calculateSimple(setup);
    // print the LCIA results
    for (ImpactDescriptor impact : r.getImpacts()) {
        System.out.println(impact.name + "\t" + r.getTotalImpactResult(impact) + "\t" + impact.referenceUnit);
    }
    // finally, close the database
    db.close();
}
Also used : IDatabase(org.openlca.core.database.IDatabase) Derby(org.openlca.core.database.Derby) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) Process(org.openlca.core.model.Process) SimpleResult(org.openlca.core.results.SimpleResult) MatrixCache(org.openlca.core.matrix.cache.MatrixCache) ProcessDao(org.openlca.core.database.ProcessDao) LinkingConfig(org.openlca.core.matrix.linking.LinkingConfig) ProductSystemBuilder(org.openlca.core.matrix.ProductSystemBuilder) File(java.io.File) SystemCalculator(org.openlca.core.math.SystemCalculator)

Example 4 with ProductSystemBuilder

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

the class SubResultsTest method testSubResults.

/**
 * Calculate a simple sub-system model:
 * <pre>
 * {@code
 * 		A [1] -> [2] B [0.5] -> [2] C [1]
 *
 * 		expected total CO2 result:
 * 		C: 1 * 1 kg CO2
 * 		B: 4 * 1 kg CO2
 * 		A: 8 * 1 kg CO2
 * 		----------------
 * 		13 kg CO2
 * }
 * </pre>
 */
@Test
public void testSubResults() {
    var units = UnitGroup.of("Mass units", "kg");
    var mass = FlowProperty.of("Mass", units);
    var co2 = Flow.elementary("CO2", mass);
    var prodA = Flow.product("A", mass);
    var prodB = Flow.product("B", mass);
    var prodC = Flow.product("C", mass);
    // A [1] -> [2] B [0.5] -> [2] C [1]
    var procA = Process.of("A", prodA);
    var procB = Process.of("B", prodB);
    procB.quantitativeReference.amount = 0.5;
    procB.input(prodA, 2);
    var procC = Process.of("C", prodC);
    procC.input(prodB, 2);
    List.of(procA, procB, procC).forEach(p -> {
        p.output(co2, 1);
        p.tags = p.name;
    });
    db.insert(units, mass, co2, prodA, prodB, prodC, procA, procB, procC);
    var systems = Stream.of(procA, procB, procC).map(p -> {
        var linker = new SubSystemLinker(db);
        var system = new ProductSystemBuilder(linker).build(p);
        return db.insert(system);
    }).toList();
    var sysA = systems.get(0);
    var sysB = systems.get(1);
    var sysC = systems.get(2);
    var setup = CalculationSetup.fullAnalysis(sysC);
    var resultC = new SystemCalculator(db).calculateFull(setup);
    var co2Idx = resultC.enviIndex().at(0);
    // check the total CO2 result: 13 kg
    assertEquals(13, resultC.getTotalFlowResult(co2Idx), 1e-10);
    // check sub-result B
    var techFlowB = TechFlow.of(sysB);
    var resultB = (FullResult) resultC.subResultOf(techFlowB);
    assertEquals(3, resultB.getTotalFlowResult(co2Idx), 1e-10);
    assertEquals(4, resultC.getScalingFactor(techFlowB), 1e-10);
    // check sub-result A
    var techFlowA = TechFlow.of(sysA);
    var resultA = (FullResult) resultB.subResultOf(techFlowA);
    assertEquals(1, resultA.getTotalFlowResult(co2Idx), 1e-10);
    assertEquals(2, resultB.getScalingFactor(techFlowA), 1e-10);
    // scale a sub-result to get the full result
    assertEquals(13, 1 + resultC.getScalingFactor(techFlowB) * resultB.getTotalFlowResult(co2Idx), 1e-10);
    // test TagResult
    var tagResults = TagResult.allOf(resultC);
    var expectedTagResults = List.of(Pair.of("A", 8.0), Pair.of("B", 4.0), Pair.of("C", 1.0));
    for (var expected : expectedTagResults) {
        var tagResult = tagResults.stream().filter(r -> r.tag().equals(expected.first)).findFirst().orElseThrow();
        assertEquals(expected.second, tagResult.inventoryResultOf(co2Idx).value(), 1e-10);
    }
    db.delete(sysC, sysB, sysA, procA, procB, procC, prodA, prodB, prodC, co2, mass, units);
}
Also used : SubSystemLinker(org.openlca.core.matrix.linking.SubSystemLinker) TagResult(org.openlca.core.results.TagResult) ProductSystemBuilder(org.openlca.core.matrix.ProductSystemBuilder) Pair(org.openlca.util.Pair) CalculationSetup(org.openlca.core.model.CalculationSetup) Process(org.openlca.core.model.Process) Test(org.junit.Test) Flow(org.openlca.core.model.Flow) Tests(org.openlca.core.Tests) TechFlow(org.openlca.core.matrix.index.TechFlow) List(java.util.List) Stream(java.util.stream.Stream) IDatabase(org.openlca.core.database.IDatabase) UnitGroup(org.openlca.core.model.UnitGroup) FlowProperty(org.openlca.core.model.FlowProperty) FullResult(org.openlca.core.results.FullResult) Assert.assertEquals(org.junit.Assert.assertEquals) FullResult(org.openlca.core.results.FullResult) SubSystemLinker(org.openlca.core.matrix.linking.SubSystemLinker) ProductSystemBuilder(org.openlca.core.matrix.ProductSystemBuilder) Test(org.junit.Test)

Aggregations

ProductSystemBuilder (org.openlca.core.matrix.ProductSystemBuilder)4 IDatabase (org.openlca.core.database.IDatabase)3 ProcessDao (org.openlca.core.database.ProcessDao)3 LinkingConfig (org.openlca.core.matrix.linking.LinkingConfig)3 Process (org.openlca.core.model.Process)3 File (java.io.File)2 Derby (org.openlca.core.database.Derby)2 ProductSystemDao (org.openlca.core.database.ProductSystemDao)2 JsonObject (com.google.gson.JsonObject)1 List (java.util.List)1 Stream (java.util.stream.Stream)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Test (org.junit.Test)1 Tests (org.openlca.core.Tests)1 SystemCalculator (org.openlca.core.math.SystemCalculator)1 MatrixCache (org.openlca.core.matrix.cache.MatrixCache)1 TechFlow (org.openlca.core.matrix.index.TechFlow)1 SubSystemLinker (org.openlca.core.matrix.linking.SubSystemLinker)1 CalculationSetup (org.openlca.core.model.CalculationSetup)1 Flow (org.openlca.core.model.Flow)1