Search in sources :

Example 1 with TLongByteHashMap

use of gnu.trove.map.hash.TLongByteHashMap 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)

Aggregations

TLongByteHashMap (gnu.trove.map.hash.TLongByteHashMap)1 TLongHashSet (gnu.trove.set.hash.TLongHashSet)1 ArrayList (java.util.ArrayList)1 ImpactFactor (org.openlca.core.model.ImpactFactor)1 Location (org.openlca.core.model.Location)1 FormulaInterpreter (org.openlca.expressions.FormulaInterpreter)1 Pair (org.openlca.util.Pair)1