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