Search in sources :

Example 1 with SimpleResult

use of org.openlca.core.results.SimpleResult in project olca-modules by GreenDelta.

the class SimulatorTest method testImpactParam.

@Test
public void testImpactParam() {
    // create a simple model with an uncertain
    // parameter in a LCIA category
    Process p = TestProcess.refProduct("p", 1.0, "kg").elemOut("CH4", 1.0, "kg").get();
    ProductSystem s = TestSystem.of(p).get();
    ImpactMethod m = TestData.method("method", TestData.impact("GWP").factor("CH4", "1 * param", "kg").parameter("param", Uncertainty.uniform(22, 26)).get());
    // create the simulator
    var setup = CalculationSetup.monteCarlo(s, 100).withImpactMethod(m);
    var simulator = Simulator.create(setup, db).withSolver(new JavaSolver());
    // check the simulation results
    for (int i = 0; i < 100; i++) {
        SimpleResult r = simulator.nextRun();
        double[] impacts = r.totalImpactResults();
        Assert.assertEquals(1, impacts.length);
        double val = impacts[0];
        Assert.assertTrue(val >= 22 && val <= 26);
    }
    Arrays.asList(s, m, p).forEach(db::delete);
}
Also used : JavaSolver(org.openlca.core.matrix.solvers.JavaSolver) ProductSystem(org.openlca.core.model.ProductSystem) Process(org.openlca.core.model.Process) TestProcess(org.openlca.core.TestProcess) SimpleResult(org.openlca.core.results.SimpleResult) ImpactMethod(org.openlca.core.model.ImpactMethod) Test(org.junit.Test)

Example 2 with SimpleResult

use of org.openlca.core.results.SimpleResult in project olca-modules by GreenDelta.

the class JsonRpc method encode.

static JsonObject encode(SimpleResult r, String id, EntityCache cache) {
    JsonObject obj = new JsonObject();
    obj.addProperty("@id", id);
    if (r == null)
        return obj;
    obj.addProperty("@type", r.getClass().getSimpleName());
    obj.add("flows", encode(r.getFlows().stream().map(EnviFlow::flow).collect(Collectors.toSet()), cache));
    obj.add("processes", encode(r.getProcesses(), cache));
    obj.add("flowResults", encode(r.getTotalFlowResults(), result -> encode(result, cache)));
    if (!r.hasImpacts())
        return obj;
    obj.add("impacts", encode(r.getImpacts(), cache));
    obj.add("impactResults", encode(r.getTotalImpactResults(), result -> encode(result, cache)));
    return obj;
}
Also used : Descriptor(org.openlca.core.model.descriptors.Descriptor) UpstreamNode(org.openlca.core.results.UpstreamNode) JsonObject(com.google.gson.JsonObject) SimpleResult(org.openlca.core.results.SimpleResult) ImpactValue(org.openlca.core.results.ImpactValue) UpstreamTree(org.openlca.core.results.UpstreamTree) Collection(java.util.Collection) FlowValue(org.openlca.core.results.FlowValue) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) JsonArray(com.google.gson.JsonArray) EnviFlow(org.openlca.core.matrix.index.EnviFlow) TechIndex(org.openlca.core.matrix.index.TechIndex) EntityCache(org.openlca.core.database.EntityCache) Contribution(org.openlca.core.results.Contribution) JsonObject(com.google.gson.JsonObject)

Example 3 with SimpleResult

use of org.openlca.core.results.SimpleResult in project olca-modules by GreenDelta.

the class Utils method simple.

RpcResponse simple(RpcRequest req, Simple handler) {
    if (req == null || req.params == null || !req.params.isJsonObject())
        return Responses.invalidParams("No parameter given", req);
    JsonObject json = req.params.getAsJsonObject();
    SimpleResult result = getResult(json);
    EntityCache cache = EntityCache.create(ctx.db);
    return Responses.ok(handler.handle(result, cache), req);
}
Also used : EntityCache(org.openlca.core.database.EntityCache) JsonObject(com.google.gson.JsonObject) SimpleResult(org.openlca.core.results.SimpleResult)

Example 4 with SimpleResult

use of org.openlca.core.results.SimpleResult in project olca-app by GreenDelta.

the class Combo method on.

public static Builder on(IResult r) {
    Combo c = new Combo();
    c.flows = new ArrayList<>();
    TLongHashSet flowIDs = new TLongHashSet();
    for (var f : r.getFlows()) {
        if (f.flow() == null || flowIDs.contains(f.flow().id))
            continue;
        flowIDs.add(f.flow().id);
        c.flows.add(f.flow());
    }
    // add LCIA categories
    if (r.hasImpacts()) {
        c.impacts = r.getImpacts();
    }
    // add cost / added value selection
    if (r.hasCosts() && (r instanceof SimpleResult)) {
        SimpleResult sr = (SimpleResult) r;
        CostResultDescriptor d1 = new CostResultDescriptor();
        d1.forAddedValue = false;
        d1.name = M.Netcosts;
        CostResultDescriptor d2 = new CostResultDescriptor();
        d2.forAddedValue = true;
        d2.name = M.AddedValue;
        c.costs = sr.totalCosts() >= 0 ? Arrays.asList(d1, d2) : Arrays.asList(d2, d1);
    }
    return new Builder(c);
}
Also used : TLongHashSet(gnu.trove.set.hash.TLongHashSet) CostResultDescriptor(org.openlca.app.util.CostResultDescriptor) SimpleResult(org.openlca.core.results.SimpleResult)

Example 5 with SimpleResult

use of org.openlca.core.results.SimpleResult in project olca-modules by GreenDelta.

the class ExportHandler method excel.

@Rpc("export/excel")
public RpcResponse excel(RpcRequest req) {
    if (req == null || req.params == null || !req.params.isJsonObject())
        return Responses.badRequest("No @id given", req);
    JsonObject obj = req.params.getAsJsonObject();
    String id = Json.getString(obj, "@id");
    if (id == null)
        return Responses.badRequest("No `@id` given", req);
    String path = Json.getString(obj, "path");
    if (path == null)
        return Responses.badRequest("No `path` given", req);
    Object val = context.cache.get(id);
    if (!(val instanceof CachedResult))
        return Responses.notImplemented("The Excel export is currently" + " only implemented for calculation results", req);
    CachedResult<?> r = (CachedResult<?>) val;
    if (r.result instanceof SimpleResult)
        return exportSimpleResult(req, path, r);
    if (r.result instanceof Simulator)
        return exportSimulationResult(req, path, r);
    return Responses.notImplemented("The Excel export is currently" + " only implemented for calculation results", req);
}
Also used : JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) SimpleResult(org.openlca.core.results.SimpleResult) Simulator(org.openlca.core.math.Simulator) Rpc(org.openlca.ipc.Rpc)

Aggregations

SimpleResult (org.openlca.core.results.SimpleResult)8 JsonObject (com.google.gson.JsonObject)3 File (java.io.File)2 EntityCache (org.openlca.core.database.EntityCache)2 Simulator (org.openlca.core.math.Simulator)2 Process (org.openlca.core.model.Process)2 ImpactDescriptor (org.openlca.core.model.descriptors.ImpactDescriptor)2 JsonArray (com.google.gson.JsonArray)1 TLongHashSet (gnu.trove.set.hash.TLongHashSet)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)1 CostResultDescriptor (org.openlca.app.util.CostResultDescriptor)1