Search in sources :

Example 16 with ImpactFactor

use of org.openlca.core.model.ImpactFactor in project olca-modules by GreenDelta.

the class ParameterUsageTreeTest method testImpactFactor.

@Test
public void testImpactFactor() {
    var global = db.insert(Parameter.global("param", 42));
    var flow = new Flow();
    flow.name = "CH4";
    new FlowDao(db).insert(flow);
    var impact = new ImpactCategory();
    impact.name = "GWP";
    var factor = new ImpactFactor();
    factor.flow = flow;
    factor.value = 24.0;
    factor.formula = "2 * param";
    impact.impactFactors.add(factor);
    new ImpactCategoryDao(db).insert(impact);
    // find the factor node
    var tree = ParameterUsageTree.of("param", db);
    var node = find(tree, "GWP", "CH4");
    assertNotNull(node);
    assertEquals(Descriptor.of(flow), node.model);
    assertEquals(UsageType.FORMULA, node.usageType);
    assertEquals("2 * param", node.usage);
    // find the global parameter definition
    node = find(tree, "param");
    assertNotNull(node);
    assertEquals(Descriptor.of(global), node.model);
    assertEquals(UsageType.DEFINITION, node.usageType);
}
Also used : ImpactFactor(org.openlca.core.model.ImpactFactor) FlowDao(org.openlca.core.database.FlowDao) ImpactCategory(org.openlca.core.model.ImpactCategory) ImpactCategoryDao(org.openlca.core.database.ImpactCategoryDao) Flow(org.openlca.core.model.Flow) Test(org.junit.Test)

Example 17 with ImpactFactor

use of org.openlca.core.model.ImpactFactor in project olca-app by GreenDelta.

the class FormulaCellEditor method doSetValue.

@Override
protected void doSetValue(Object value) {
    selectedElement = value;
    String formula = null;
    if (value instanceof Parameter) {
        formula = ((Parameter) value).formula;
    } else if (value instanceof Exchange) {
        Exchange e = (Exchange) value;
        if (Strings.notEmpty(e.formula)) {
            formula = e.formula;
        } else {
            formula = Double.toString(e.amount);
        }
    } else if (value instanceof ImpactFactor) {
        ImpactFactor f = (ImpactFactor) value;
        if (Strings.notEmpty(f.formula)) {
            formula = f.formula;
        } else {
            formula = Double.toString(f.value);
        }
    }
    formula = formula == null ? "" : formula;
    super.doSetValue(formula);
    oldValue = formula;
}
Also used : Exchange(org.openlca.core.model.Exchange) ImpactFactor(org.openlca.core.model.ImpactFactor) Parameter(org.openlca.core.model.Parameter)

Example 18 with ImpactFactor

use of org.openlca.core.model.ImpactFactor in project olca-app by GreenDelta.

the class GeoFactorCalculator method createFactors.

private void createFactors(Map<Location, List<Pair<GeoProperty, Double>>> locParams) {
    // remove all LCIA factors with a flow and location
    // that will be calculated
    TLongHashSet setupFlows = new TLongHashSet();
    for (GeoFlowBinding b : setup.bindings) {
        if (b.flow == null)
            continue;
        setupFlows.add(b.flow.id);
    }
    TLongHashSet setupLocations = new TLongHashSet();
    for (Location loc : locations) {
        setupLocations.add(loc.id);
    }
    TLongByteHashMap isDefaultPresent = new TLongByteHashMap();
    List<ImpactFactor> removals = new ArrayList<>();
    for (ImpactFactor factor : impact.impactFactors) {
        if (factor.flow == null)
            return;
        long flowID = factor.flow.id;
        if (!setupFlows.contains(flowID))
            continue;
        if (factor.location == null) {
            isDefaultPresent.put(flowID, (byte) 1);
        } else if (setupLocations.contains(factor.location.id)) {
            removals.add(factor);
        }
    }
    impact.impactFactors.removeAll(removals);
    // generate the non-regionalized default factors
    // for setup flows that are not yet present
    FormulaInterpreter fi = new FormulaInterpreter();
    for (GeoProperty param : setup.properties) {
        fi.bind(param.identifier, Double.toString(param.defaultValue));
    }
    for (GeoFlowBinding b : setup.bindings) {
        if (b.flow == null)
            continue;
        byte present = isDefaultPresent.get(b.flow.id);
        if (present == (byte) 1)
            continue;
        try {
            double val = fi.eval(b.formula);
            impact.factor(b.flow, val);
        } catch (Exception e) {
            log.error("failed to evaluate formula {} " + " of binding with flow {}", b.formula, b.flow);
        }
    }
    // finally, generate regionalized factors
    for (Location loc : locParams.keySet()) {
        // bind the location specific parameter values
        // to a formula interpreter
        fi = new FormulaInterpreter();
        List<Pair<GeoProperty, Double>> pairs = locParams.get(loc);
        if (pairs == null)
            continue;
        for (Pair<GeoProperty, Double> pair : pairs) {
            GeoProperty param = pair.first;
            double val = pair.second == null ? param.defaultValue : pair.second;
            fi.bind(param.identifier, Double.toString(val));
        }
        for (GeoFlowBinding b : setup.bindings) {
            if (b.flow == null || b.formula == null)
                continue;
            try {
                double val = fi.eval(b.formula);
                var factor = impact.factor(b.flow, val);
                factor.location = loc;
            } catch (Exception e) {
                log.error("Failed to calculate factor from formula " + b.formula + " in binding with flow " + b.flow, e);
            }
        }
    }
}
Also used : ImpactFactor(org.openlca.core.model.ImpactFactor) ArrayList(java.util.ArrayList) FormulaInterpreter(org.openlca.expressions.FormulaInterpreter) TLongByteHashMap(gnu.trove.map.hash.TLongByteHashMap) TLongHashSet(gnu.trove.set.hash.TLongHashSet) Location(org.openlca.core.model.Location) Pair(org.openlca.util.Pair)

Example 19 with ImpactFactor

use of org.openlca.core.model.ImpactFactor in project olca-app by GreenDelta.

the class FactorClipboard method factor.

private ImpactFactor factor(String[] row) {
    if (row.length < 4)
        return null;
    String name = row[0];
    String category = row[1];
    String amount = row[2];
    String unit = row[3];
    // filter the flows by matching names and categories
    List<Flow> candidates = flows.stream().filter(d -> Strings.nullOrEqual(d.name, name)).map(d -> new FlowDao(db).getForId(d.id)).filter(flow -> {
        if (flow.category == null)
            return Strings.nullOrEmpty(category);
        String path = CategoryPath.getFull(flow.category);
        return Strings.nullOrEqual(path, category);
    }).collect(Collectors.toList());
    if (candidates.isEmpty())
        return null;
    // find a matching flow for the unit
    // the unit in the table has the format:
    // <LCIA ref. unit> / <flow unit>
    // the following only works if the LCIA
    // ref. unit does not contain a slash, but
    // this should be very unlikely
    int i = unit.indexOf('/');
    if (i >= 0) {
        unit = unit.substring(i + 1).trim();
    }
    ImpactFactor factor = new ImpactFactor();
    for (Flow flow : candidates) {
        for (FlowPropertyFactor p : flow.flowPropertyFactors) {
            if (p.flowProperty == null || p.flowProperty.unitGroup == null)
                continue;
            Unit u = p.flowProperty.unitGroup.getUnit(unit);
            if (u == null)
                continue;
            factor.flow = flow;
            factor.flowPropertyFactor = p;
            factor.unit = u;
            if (Objects.equals(p.flowProperty, flow.referenceFlowProperty))
                break;
        }
        if (factor.flow != null)
            break;
    }
    if (factor.flow == null)
        return null;
    // set the amount value / formula
    try {
        factor.value = Double.parseDouble(amount);
    } catch (Exception e) {
        factor.formula = amount;
    }
    // uncertainty value
    if (row.length > 4) {
        factor.uncertainty = Uncertainty.fromString(row[4]);
    }
    // location
    if (row.length > 5) {
        String code = row[5];
        if (!Strings.nullOrEmpty(code)) {
            LocationDao dao = new LocationDao(db);
            factor.location = dao.getDescriptors().stream().filter(d -> Strings.nullOrEqual(code, d.code)).map(d -> dao.getForId(d.id)).findFirst().orElse(null);
        }
    }
    return factor;
}
Also used : M(org.openlca.app.M) Unit(org.openlca.core.model.Unit) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Flow(org.openlca.core.model.Flow) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) ImpactFactor(org.openlca.core.model.ImpactFactor) Objects(java.util.Objects) LocationDao(org.openlca.core.database.LocationDao) List(java.util.List) Database(org.openlca.app.db.Database) Strings(org.openlca.util.Strings) IDatabase(org.openlca.core.database.IDatabase) Uncertainty(org.openlca.core.model.Uncertainty) FlowDao(org.openlca.core.database.FlowDao) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) Collections(java.util.Collections) CategoryPath(org.openlca.io.CategoryPath) ImpactFactor(org.openlca.core.model.ImpactFactor) FlowDao(org.openlca.core.database.FlowDao) LocationDao(org.openlca.core.database.LocationDao) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Unit(org.openlca.core.model.Unit) Flow(org.openlca.core.model.Flow)

Aggregations

ImpactFactor (org.openlca.core.model.ImpactFactor)19 ImpactCategory (org.openlca.core.model.ImpactCategory)6 Parameter (org.openlca.core.model.Parameter)5 Test (org.junit.Test)4 Flow (org.openlca.core.model.Flow)4 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)4 ArrayList (java.util.ArrayList)3 Uncertainty (org.openlca.core.model.Uncertainty)3 FlowDao (org.openlca.core.database.FlowDao)2 ImpactCategoryDao (org.openlca.core.database.ImpactCategoryDao)2 Exchange (org.openlca.core.model.Exchange)2 Unit (org.openlca.core.model.Unit)2 AbstractZipTest (org.openlca.jsonld.AbstractZipTest)2 JsonElement (com.google.gson.JsonElement)1 TLongByteHashMap (gnu.trove.map.hash.TLongByteHashMap)1 TLongHashSet (gnu.trove.set.hash.TLongHashSet)1 Collections (java.util.Collections)1 Date (java.util.Date)1 List (java.util.List)1 Objects (java.util.Objects)1