Search in sources :

Example 6 with SystemCalculator

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

the class WasteScenarioUnrollingTest method testUnrolling.

@Test
public void testUnrolling() throws Exception {
    var dataSet = new CsvDataSet();
    // unit: kg
    dataSet.units().add(new UnitRow().name("kg").conversionFactor(1.0).referenceUnit("kg").quantity("Mass"));
    // quantity: mass
    dataSet.quantities().add(new QuantityRow().name("Mass").hasDimension(true));
    // CO2
    dataSet.airborneEmissions().add(new ElementaryFlowRow().name("Carbon dioxide").unit("kg").cas("000124-38-9").comment("Formula: CO2").platformId("DBE27C73-9DF4-4F21-B2EB-A0CE1FE87A4B"));
    // material
    var material = new ProcessBlock().category(ProcessCategory.MATERIAL).name("material 1").identifier("Dev99229000009369600012");
    material.products().add(new ProductOutputRow().name("material 1").amount(Numeric.of(1.0)).unit("kg").wasteType("Some waste"));
    dataSet.processes().add(material);
    // waste treatment
    var wasteTreatment = new ProcessBlock().category(ProcessCategory.WASTE_TREATMENT).name("waste treatment 1").identifier("Dev99229000009369600013");
    wasteTreatment.wasteTreatment(new WasteTreatmentRow().name("waste treatment 1").amount(Numeric.of(1)).unit("kg"));
    wasteTreatment.emissionsToAir().add(new ElementaryExchangeRow().name("Carbon dioxide").amount(Numeric.of(1.0)).unit("kg"));
    dataSet.processes().add(wasteTreatment);
    // waste scenario
    var wasteScenario = new ProcessBlock().category(ProcessCategory.WASTE_SCENARIO).name("waste scenario 1").identifier("Dev99229000009369600014");
    wasteScenario.wasteScenario(new WasteTreatmentRow().name("waste scenario 1").wasteType("All waste types").amount(Numeric.of(1)).unit("kg"));
    wasteScenario.separatedWaste().add(new WasteFractionRow().wasteType("All waste types").fraction(100).wasteTreatment("waste treatment 1"));
    wasteScenario.remainingWaste().add(new WasteFractionRow().wasteType("All waste types").fraction(100).wasteTreatment("waste treatment 1"));
    dataSet.processes().add(wasteScenario);
    // assembly
    var assembly = new ProductStageBlock().category(ProductStageCategory.ASSEMBLY);
    assembly.products().add(new ProductStageOutputRow().name("assembly 1").amount(Numeric.of(1)).unit("kg"));
    assembly.materialsAndAssemblies().add(new TechExchangeRow().name("material 1").amount(Numeric.of(2)).unit("kg"));
    dataSet.productStages().add(assembly);
    // life cycle
    var lifeCycle = new ProductStageBlock().category(ProductStageCategory.LIFE_CYCLE);
    lifeCycle.products().add(new ProductStageOutputRow().name("life cycle 1").amount(Numeric.of(1)).unit("kg"));
    lifeCycle.assembly(new TechExchangeRow().name("assembly 1").amount(Numeric.of(2)).unit("kg"));
    lifeCycle.wasteOrDisposalScenario(new TechExchangeRow().name("waste scenario 1"));
    dataSet.productStages().add(lifeCycle);
    // write the file
    var file = Files.createTempFile("_olca_", ".csv").toFile();
    dataSet.write(file);
    new SimaProCsvImport(db, file).generateLifeCycleSystems(true).unrollWasteScenarios(true).run();
    var systems = db.getAll(ProductSystem.class);
    assertEquals(1, systems.size());
    var system = systems.get(0);
    assertEquals(1, system.parameterSets.size());
    var setup = CalculationSetup.simple(systems.get(0)).withParameters(system.parameterSets.get(0).parameters);
    var result = new SystemCalculator(db).calculateSimple(setup);
    var inventory = result.getTotalFlowResults();
    assertEquals(1, inventory.size());
    var co2Result = inventory.get(0);
    assertEquals("Carbon dioxide", co2Result.flow().name);
    assertEquals(4.0, co2Result.value(), 1e-10);
    assertTrue(file.delete());
}
Also used : ProcessBlock(org.openlca.simapro.csv.process.ProcessBlock) ProductStageBlock(org.openlca.simapro.csv.process.ProductStageBlock) WasteFractionRow(org.openlca.simapro.csv.process.WasteFractionRow) TechExchangeRow(org.openlca.simapro.csv.process.TechExchangeRow) ProductOutputRow(org.openlca.simapro.csv.process.ProductOutputRow) ElementaryFlowRow(org.openlca.simapro.csv.refdata.ElementaryFlowRow) WasteTreatmentRow(org.openlca.simapro.csv.process.WasteTreatmentRow) CsvDataSet(org.openlca.simapro.csv.CsvDataSet) ProductStageOutputRow(org.openlca.simapro.csv.process.ProductStageOutputRow) UnitRow(org.openlca.simapro.csv.refdata.UnitRow) ElementaryExchangeRow(org.openlca.simapro.csv.process.ElementaryExchangeRow) QuantityRow(org.openlca.simapro.csv.refdata.QuantityRow) SystemCalculator(org.openlca.core.math.SystemCalculator) Test(org.junit.Test)

Example 7 with SystemCalculator

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

the class ResultExportTest method testSimpleResult.

@Test
public void testSimpleResult() throws Exception {
    // create a simple process
    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("C02", mass));
    var process = Process.of("Steel production", steel);
    process.output(co2, 2);
    process = db.insert(process);
    // create a system and calculate it
    var sys = ProductSystem.of(process);
    var setup = CalculationSetup.fullAnalysis(sys);
    var calculator = new SystemCalculator(db);
    var result = calculator.calculateFull(setup);
    // run the export
    var file = Files.createTempFile("_olca_test_", ".xlsx").toFile();
    new ResultExport(setup, result, file, EntityCache.create(db)).run();
    Assert.assertTrue(file.length() > 0);
    // clean up
    Files.delete(file.toPath());
    db.clear();
}
Also used : ResultExport(org.openlca.io.xls.results.system.ResultExport) SystemCalculator(org.openlca.core.math.SystemCalculator) Test(org.junit.Test)

Example 8 with SystemCalculator

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

the class LibraryCalculatorExample method main.

public static void main(String[] args) throws Exception {
    var workspace = "C:/Users/Win10/openLCA-data-1.4";
    var db = new Derby(new File(workspace + "/databases/libre2"));
    var libDir = new LibraryDir(new File(workspace + "/libraries"));
    var system = db.get(ProductSystem.class, "6b32cda2-5aa4-44b9-b32a-c654da48436d");
    var setup = CalculationSetup.simple(system);
    var result = new SystemCalculator(db).calculateSimple(setup);
    var flow = result.enviIndex().at(0);
    System.out.println(flow.flow().name + " => " + result.getTotalFlowResult(flow));
    db.close();
}
Also used : Derby(org.openlca.core.database.Derby) File(java.io.File) SystemCalculator(org.openlca.core.math.SystemCalculator) LibraryDir(org.openlca.core.library.LibraryDir)

Example 9 with SystemCalculator

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

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

the class TestSystem method contributions.

public static ContributionResult contributions(ProductSystem system) {
    var setup = CalculationSetup.contributions(system).withCosts(true);
    SystemCalculator calc = new SystemCalculator(Tests.getDb());
    return calc.calculateContributions(setup);
}
Also used : 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