Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/GaugeDischargeValuesFinder.java @ 9157:f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
author | mschaefer |
---|---|
date | Tue, 19 Jun 2018 14:19:32 +0200 |
parents | |
children |
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde * Software engineering by * Björnsen Beratende Ingenieure GmbH * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt * * This file is Free Software under the GNU AGPL (>=v3) * and comes with ABSOLUTELY NO WARRANTY! Check out the * documentation coming with Dive4Elements River for details. */ package org.dive4elements.river.artifacts.sinfo.collision; import org.apache.commons.lang.math.DoubleRange; import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.interpolation.LinearInterpolator; import org.dive4elements.river.artifacts.model.Calculation; import org.dive4elements.river.model.DischargeTable; import org.dive4elements.river.model.DischargeTableValue; import org.dive4elements.river.model.Gauge; import gnu.trove.TDoubleArrayList; /** * Loading and search/interpolation of a gauge's discharge table * * @author Matthias Schäfer * */ public final class GaugeDischargeValuesFinder { /***** FIELDS *****/ // private static Logger log = Logger.getLogger(GaugeDischargeValuesFinder.class); private final Gauge gauge; private final Calculation problems; private final UnivariateRealFunction wInterpolator; private final DoubleRange wRange; /***** CONSTRUCTORS *****/ private GaugeDischargeValuesFinder(final Gauge gauge, final Calculation problems, final DischargeTable dischargeTable) { this.gauge = gauge; this.problems = problems; final TDoubleArrayList ws = new TDoubleArrayList(); final TDoubleArrayList qs = new TDoubleArrayList(); for (final DischargeTableValue v : DischargeTable.getValuesSortedByW(dischargeTable)) { ws.add(v.getW().doubleValue()); qs.add(v.getQ().doubleValue()); } if (!ws.isEmpty()) this.wRange = new DoubleRange(ws.get(0), ws.get(ws.size() - 1)); else this.wRange = null; this.wInterpolator = new LinearInterpolator().interpolate(ws.toNativeArray(), qs.toNativeArray()); } /***** METHODS *****/ /** * Loads the the main discharge table of a gauge (GAUGE.at) * * @return The discharge table values finder of the gauge, or null */ public static GaugeDischargeValuesFinder loadValues(final Gauge gauge, final Calculation problems) { final DischargeTable table = DischargeTable.getGaugeMainDischargeTable(gauge); if ((table == null) || (table.getDischargeTableValues().size() == 0)) return null; else { return new GaugeDischargeValuesFinder(gauge, problems, table); } } /** * If this provider may return valid data at all. */ public boolean isValid() { return (this.wInterpolator != null); } /** * Discharge for a W * * @return Q, or NegInf for w less than all, or PosInf for w greater then all, or NaN in case of exception */ public double getDischarge(final double w) { try { if (this.wInterpolator == null) return Double.NaN; else if (w < this.wRange.getMinimumDouble()) return Double.NEGATIVE_INFINITY; else if (w > this.wRange.getMaximumDouble()) return Double.POSITIVE_INFINITY; else return this.wInterpolator.value(w); } catch (@SuppressWarnings("unused") final FunctionEvaluationException e) { // ignore exception because this can/will happen regularly return Double.NaN; } } }