Search in sources :

Example 1 with CalcExchange

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

the class TechIndexBuilder method build.

@Override
public TechIndex build(TechFlow refFlow) {
    log.trace("build product index for {}", refFlow);
    TechIndex index = new TechIndex(refFlow);
    index.setDemand(1.0);
    addSystemLinks(index);
    List<TechFlow> block = new ArrayList<>();
    block.add(refFlow);
    HashSet<TechFlow> handled = new HashSet<>();
    while (!block.isEmpty()) {
        var nextBlock = new ArrayList<TechFlow>();
        log.trace("fetch next block with {} entries", block.size());
        Map<Long, List<CalcExchange>> exchanges = fetchExchanges(block);
        for (TechFlow recipient : block) {
            handled.add(recipient);
            List<CalcExchange> all = exchanges.get(recipient.providerId());
            List<CalcExchange> candidates = providers.getLinkCandidates(all);
            for (CalcExchange linkExchange : candidates) {
                TechFlow provider = providers.find(linkExchange);
                if (provider == null)
                    continue;
                LongPair exchange = new LongPair(recipient.providerId(), linkExchange.exchangeId);
                index.putLink(exchange, provider);
                if (!handled.contains(provider) && !nextBlock.contains(provider))
                    nextBlock.add(provider);
            }
        }
        block = nextBlock;
    }
    return index;
}
Also used : LongPair(org.openlca.core.matrix.index.LongPair) TechFlow(org.openlca.core.matrix.index.TechFlow) ArrayList(java.util.ArrayList) CalcExchange(org.openlca.core.matrix.CalcExchange) TechIndex(org.openlca.core.matrix.index.TechIndex) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 2 with CalcExchange

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

the class UMatrixTest method testSimpleExchange.

@Test
public void testSimpleExchange() {
    CalcExchange e = baseExchange();
    UMatrix u = new UMatrix();
    u.add(42, 42, e);
    HashPointMatrix m = new HashPointMatrix(100, 100);
    u.generate(m, new FormulaInterpreter());
    Assert.assertEquals(42.0, m.get(42, 42), 1e-16);
}
Also used : CalcExchange(org.openlca.core.matrix.CalcExchange) HashPointMatrix(org.openlca.core.matrix.format.HashPointMatrix) FormulaInterpreter(org.openlca.expressions.FormulaInterpreter) Test(org.junit.Test)

Example 3 with CalcExchange

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

the class UMatrixTest method testUniformExchange.

@Test
public void testUniformExchange() {
    CalcExchange e = baseExchange();
    e.parameter1 = 10;
    e.parameter2 = 20;
    e.uncertaintyType = UncertaintyType.UNIFORM;
    UMatrix u = new UMatrix();
    u.add(42, 42, e);
    HashPointMatrix m = new HashPointMatrix(100, 100);
    for (int i = 0; i < 10; i++) {
        u.generate(m, new FormulaInterpreter());
        double val = m.get(42, 42);
        Assert.assertTrue(val >= 10 && val <= 20);
    }
}
Also used : CalcExchange(org.openlca.core.matrix.CalcExchange) HashPointMatrix(org.openlca.core.matrix.format.HashPointMatrix) FormulaInterpreter(org.openlca.expressions.FormulaInterpreter) Test(org.junit.Test)

Example 4 with CalcExchange

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

the class UMatrixTest method baseExchange.

private CalcExchange baseExchange() {
    CalcExchange e = new CalcExchange();
    e.amount = 42.0;
    e.conversionFactor = 1.0;
    e.flowType = FlowType.ELEMENTARY_FLOW;
    e.exchangeId = e.flowId = e.processId = 42L;
    return e;
}
Also used : CalcExchange(org.openlca.core.matrix.CalcExchange)

Example 5 with CalcExchange

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

the class UMatrixTest method testAddExchanges.

@Test
public void testAddExchanges() {
    CalcExchange e1 = baseExchange();
    e1.parameter1 = 10;
    e1.parameter2 = 20;
    e1.uncertaintyType = UncertaintyType.UNIFORM;
    CalcExchange e2 = baseExchange();
    e2.parameter1 = 10;
    e2.parameter2 = 20;
    e2.uncertaintyType = UncertaintyType.UNIFORM;
    UMatrix u = new UMatrix();
    u.add(42, 42, e1);
    u.add(42, 42, e2);
    HashPointMatrix m = new HashPointMatrix(100, 100);
    for (int i = 0; i < 10; i++) {
        u.generate(m, new FormulaInterpreter());
        double val = m.get(42, 42);
        Assert.assertTrue(val >= 20 && val <= 40);
    }
}
Also used : CalcExchange(org.openlca.core.matrix.CalcExchange) HashPointMatrix(org.openlca.core.matrix.format.HashPointMatrix) FormulaInterpreter(org.openlca.expressions.FormulaInterpreter) Test(org.junit.Test)

Aggregations

CalcExchange (org.openlca.core.matrix.CalcExchange)7 Test (org.junit.Test)5 HashPointMatrix (org.openlca.core.matrix.format.HashPointMatrix)4 FormulaInterpreter (org.openlca.expressions.FormulaInterpreter)4 HashSet (java.util.HashSet)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ProcessDao (org.openlca.core.database.ProcessDao)1 LongPair (org.openlca.core.matrix.index.LongPair)1 TechFlow (org.openlca.core.matrix.index.TechFlow)1 TechIndex (org.openlca.core.matrix.index.TechIndex)1 Exchange (org.openlca.core.model.Exchange)1 Process (org.openlca.core.model.Process)1 Uncertainty (org.openlca.core.model.Uncertainty)1