Skip to content

Commit

Permalink
Merge pull request #238 from pmpowers-usgs/system-rate-237
Browse files Browse the repository at this point in the history
System rate 237
  • Loading branch information
pmpowers-usgs authored Aug 9, 2017
2 parents d99d8f3 + 9c4b069 commit cf70d11
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 43 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
nshmp-haz
=========

U.S. Geological Survey (USGS) National Seismic Hazard Model Project (NSHMP) codes.
U.S. Geological Survey ([USGS](https://www.usgs.gov)) national neismic hazard mapping project ([NSHMP](https://earthquake.usgs.gov/hazards/)) codes for performing probabilistic seismic hazard (PSHA) and related analyses.

Information relevant to obtaining, building, and running the code may be found in the [Wiki](https://github.com/usgs/nshmp-haz/wiki/).
These codes are intended for use with models developed by the NSHMP. Information relevant to obtaining, building, and running the code may be found in the [wiki](https://github.com/usgs/nshmp-haz/wiki/).
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
encoding="UTF-8"
docencoding="UTF-8"
charset="UTF-8"
additionalparam="-notimestamp">
additionalparam="-notimestamp -Xdoclint:none">
<packageset dir="${src}">
<include name="org/opensha2/**" />
<exclude name="org/opensha2/etc/**" />
Expand Down
13 changes: 8 additions & 5 deletions src/org/opensha2/calc/EqRate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.opensha2.eq.model.SystemSourceSet;
import org.opensha2.geo.Location;
import org.opensha2.mfd.Mfds;
import org.opensha2.util.Maths;

import com.google.common.base.Converter;
import com.google.common.collect.ImmutableMap;
Expand All @@ -28,8 +29,8 @@
import java.util.concurrent.Callable;

/**
* General purpose magnitude-frequency distribution (MFD) data container. This
* class makes no distinction between incremental or cumulative MFDs, or whether
* General purpose earthquake rate and probability data container. This class
* makes no distinction between incremental or cumulative MFDs, or whether
* values are stored as annual-rate or poisson probability.
*
* @author Peter Powers
Expand Down Expand Up @@ -164,7 +165,9 @@ public static EqRate toCumulative(EqRate incremental) {
* conversion
*/
public static EqRate toPoissonProbability(EqRate annualRates, double timespan) {
Converter<Double, Double> converter = Mfds.annualRateToProbabilityConverter(timespan);
Converter<Double, Double> converter =
Mfds.annualRateToProbabilityConverter(timespan)
.andThen(Maths.decimalToProbabilityConverter(2));
XySequence totalMfd = XySequence
.copyOf(annualRates.totalMfd)
.transform(converter);
Expand All @@ -189,8 +192,8 @@ public static EqRate toPoissonProbability(EqRate annualRates, double timespan) {
* <p><b>NOTE:</b> This operation is additive and will produce meaningless
* results if {@code rates} have already been converted to
* {@link #toPoissonProbability(EqRate, double) probabilities}, or are not all
* of {@link DistributionFormat#INCREMENTAL} or {@link DistributionFormat#CUMULATIVE}
* distribution format.
* of {@link DistributionFormat#INCREMENTAL} or
* {@link DistributionFormat#CUMULATIVE} distribution format.
*
* <p>Buyer beware.
*
Expand Down
28 changes: 16 additions & 12 deletions src/org/opensha2/calc/EqRateExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@
*/
public final class EqRateExport {

private static final String RATE_FORMAT = "%.8g";
private static final String PROB_FORMAT = "%.2f";

private final Logger log;
private final Path dir;
private final String valueFormat;
private final CalcConfig config;
private final boolean exportSource;

Expand All @@ -53,8 +57,13 @@ public final class EqRateExport {
private final List<EqRate> rates;

private EqRateExport(CalcConfig config, Sites sites, Logger log) throws IOException {

// whether or not rates or probabilities have been calculated
boolean rates = config.rate.valueFormat == ValueFormat.ANNUAL_RATE;

this.log = log;
this.dir = HazardExport.createOutputDir(improvedOutputDirectory(config));
this.dir = HazardExport.createOutputDir(updateOutDir(config.output.directory, rates));
this.valueFormat = rates ? RATE_FORMAT : PROB_FORMAT;
this.config = config;
this.exportSource = config.output.dataTypes.contains(DataType.SOURCE);
this.rates = new ArrayList<>();
Expand All @@ -66,17 +75,12 @@ private EqRateExport(CalcConfig config, Sites sites, Logger log) throws IOExcept
this.totalWatch = Stopwatch.createStarted();
}

/*
* If output from config is 'curves', change to the more appropriate 'eq-rate'
* or 'eq-prob'.
*/
static Path improvedOutputDirectory(CalcConfig config) {
Path out = config.output.directory;
if (out.toString().equals(CalcConfig.DEFAULT_OUT)) {
return (config.rate.valueFormat == ValueFormat.POISSON_PROBABILITY) ? Paths.get("eq-prob")
: Paths.get("eq-rate");
/* If config output is 'curves', change to 'eq-rate' or 'eq-prob'. */
static Path updateOutDir(Path dir, boolean rates) {
if (dir.toString().equals(CalcConfig.DEFAULT_OUT)) {
return (rates ? Paths.get("eq-rate") : Paths.get("eq-prob"));
}
return out;
return dir;
}

/**
Expand Down Expand Up @@ -189,7 +193,7 @@ private void writeRates() throws IOException {

OpenOption[] options = firstBatch ? HazardExport.WRITE : HazardExport.APPEND;

Function<Double, String> formatter = Parsing.formatDoubleFunction(HazardExport.RATE_FMT);
Function<Double, String> formatter = Parsing.formatDoubleFunction(valueFormat);

/* Line maps for ascii output; may or may not be used */
List<String> totalLines = new ArrayList<>();
Expand Down
43 changes: 23 additions & 20 deletions src/org/opensha2/eq/model/SystemSourceSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ private final double width() {
private final double rake() {
return rakes[index];
}

}

/**
Expand Down Expand Up @@ -395,13 +394,13 @@ SystemSourceSet build() {
stats);
}
}

/*
* Handle rate calculations internally as SystemSource is not fully implemented.
* If/when it is, this should be removed in favor using iterableForLocation
* and getRupture(0).
* Handle rate calculations internally as SystemSource is not fully
* implemented. If/when it is, this should be removed in favor using
* iterableForLocation and getRupture(0).
*/

/**
* Return an instance of a {@code Function} that converts a
* {@code SystemSourceSet} to a ground motion model {@code InputList}.
Expand All @@ -427,7 +426,7 @@ private static final class ToRates implements Function<SystemSourceSet, Interval
final Location location,
final double distance,
final IntervalArray modelMfd) {

this.location = location;
this.distance = distance;
this.modelMfd = modelMfd;
Expand All @@ -436,19 +435,18 @@ private static final class ToRates implements Function<SystemSourceSet, Interval
@Override
public IntervalArray apply(final SystemSourceSet sourceSet) {
IntervalArray.Builder mfdForLocation = IntervalArray.Builder.fromModel(modelMfd);
BitSet bitsetForLocation = sourceSet.bitsetForLocation(location, distance);
if (bitsetForLocation.isEmpty()) {
BitSet siteBitset = sourceSet.bitsetForLocation(location, distance);
if (siteBitset.isEmpty()) {
return modelMfd;
}
int[] sourceIndices = Indexing.bitsToIndices(bitsetForLocation);
for (int i : sourceIndices) {
mfdForLocation.add(sourceSet.mags[i], sourceSet.rates[i]);
Predicate<SystemSource> distanceFilter = new BitsetFilter(siteBitset);
for (SystemSource source : Iterables.filter(sourceSet, distanceFilter)) {
mfdForLocation.add(source.magnitude(), source.rate());
}
return mfdForLocation.multiply(sourceSet.weight()).build();
}
}


/*
* System source calculation pipeline.
*
Expand Down Expand Up @@ -593,23 +591,28 @@ public int compare(double[] left, double[] right) {
}
}

/*
* Predicate that tests the intersection of a site bitset (fault sections
* wihtin a specified distance of a site) with source bitsets (fault sections
* that participate in a SystemSource/Rupture).
*/
private static class BitsetFilter implements Predicate<SystemSource> {

private static final String ID = "BitsetFilter";
private final BitSet bitset;
private final BitSet siteBitset;

BitsetFilter(BitSet bitset) {
this.bitset = bitset;
BitsetFilter(BitSet siteBitset) {
this.siteBitset = siteBitset;
}

@Override
public boolean apply(SystemSource source) {
return bitset.intersects(source.bitset());
return siteBitset.intersects(source.bitset());
}

@Override
public String toString() {
return ID + " " + bitset;
return ID + " " + siteBitset;
}
}

Expand All @@ -636,13 +639,13 @@ private static final class InputGenerator implements Function<SystemSource, Haza
public HazardInput apply(SystemSource source) {

/* Find r minima. */
BitSet sectionBitset = source.bitset();
BitSet sections = source.bitset();
double rJB = Double.MAX_VALUE;
double rRup = Double.MAX_VALUE;
double rX = Double.MAX_VALUE;
int hitCount = 0;
for (int sectionIndex : rMap.keySet()) {
if (sectionBitset.get(sectionIndex)) {
if (sections.get(sectionIndex)) {
double[] distances = rMap.get(sectionIndex);
rJB = min(rJB, distances[R_JB_INDEX]);
double rRupNew = distances[R_RUP_INDEX];
Expand Down
2 changes: 1 addition & 1 deletion src/org/opensha2/gmm/ZhaoEtAl_2016.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Abstract implementation of the shallow crustal, upper mantle, subduction
* interface, and subduction slab ground motion models by Zhao et al. (2016).
*
* <p><b>Implementation notes:</b>
* <p><b>Implementation notes:</b><ul>
*
* <li>All models supply site-class specific sigma, however, only the total
* sigma is used here as reported in the various coefficient tables.</li>
Expand Down
6 changes: 4 additions & 2 deletions src/org/opensha2/mfd/Mfds.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,17 @@ public static double probToRate(double P, double timespan) {

/**
* Return a converter between annual rate and Poisson probability over a
* 1-year time span.
* 1-year time span. The converter expects probabilities to be represented as
* fractions of 1.0.
*/
public static Converter<Double, Double> annualRateToProbabilityConverter() {
return new AnnRateToPoissProbConverter(1.0);
}

/**
* Return a converter between annual rate and Poisson probability over the
* specified time span.
* specified time span. The converter expects probabilities to be represented
* as fractions of 1.0.
*/
public static Converter<Double, Double> annualRateToProbabilityConverter(double timespan) {
return new AnnRateToPoissProbConverter(timespan);
Expand Down
38 changes: 38 additions & 0 deletions src/org/opensha2/util/Maths.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.opensha2.util;

import com.google.common.base.Converter;

import java.math.BigDecimal;
import java.math.RoundingMode;

Expand Down Expand Up @@ -148,4 +150,40 @@ public static double round(double value, int scale, RoundingMode mode) {
return BigDecimal.valueOf(value).setScale(scale, mode).doubleValue();
}

/**
* Return a converter between decimal values and percentages. The forward
* operation of this converter <i>may</i> be lossy in as much as the supplied
* decimal value will be rounded to the specified scale after conversion to a
* percentage. For instance:
*
* <p>{@code decimalToProbabilityConverter(2).convert(0.23456) = 23.46}
*
* <p>Reverse operations do not affect precision at this time.
*
* @param percentScale the number of decimal places in percentages output by
* this converter
*/
public static Converter<Double, Double> decimalToProbabilityConverter(int percentScale) {
return new DecimalToProbabilityConverter(percentScale);
}

private static class DecimalToProbabilityConverter extends Converter<Double, Double> {

private final int percentScale;

DecimalToProbabilityConverter(int percentScale) {
this.percentScale = percentScale;
}

@Override
protected Double doBackward(Double percent) {
return percent / 100.0;
}

@Override
protected Double doForward(Double decimal) {
return round(decimal * 100.0, percentScale);
}
}

}

0 comments on commit cf70d11

Please sign in to comment.