Skip to content

Commit

Permalink
misc for upstream, and new BBP data loading optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Milner committed Nov 14, 2024
1 parent cf9a330 commit dc9b509
Show file tree
Hide file tree
Showing 15 changed files with 637 additions and 135 deletions.
81 changes: 60 additions & 21 deletions src/main/java/scratch/kevin/bbp/SpectraPlotter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -75,12 +76,18 @@ private static DiscretizedFunc[] loadAll(File file) throws IOException {
return loadAll(Files.readLines(file, Charset.defaultCharset()));
}

private static double[] PREV_X_VALUES;

private static DiscretizedFunc[] loadAll(List<String> lines) {
DiscretizedFunc[] ret = null;
// DiscretizedFunc[] ret = null;
String[] names = null;

List<String[]> dataSplits = new ArrayList<>(lines.size());

boolean parsedNames = false;
for (String line : lines) {
line = line.trim();
if (names == null && line.startsWith("#")) {
if (!parsedNames && names == null && line.startsWith("#")) {
String nameStr = line.substring(1).trim();
if (line.contains("\t"))
names = nameStr.split("\t");
Expand All @@ -89,33 +96,65 @@ private static DiscretizedFunc[] loadAll(List<String> lines) {
// System.out.println("Names:");
// for (String name : names)
// System.out.println("\t"+name);
parsedNames = true;
}
if (line.startsWith("#") || line.isEmpty())
continue;
String[] split = line.split("\\s+");
if (ret == null) {
Preconditions.checkState(split.length > 1);
ret = new DiscretizedFunc[split.length - 1];
for (int i=0; i<ret.length; i++) {
ret[i] = new ArbitrarilyDiscretizedFunc();
if (names != null) {
if (names.length == ret.length)
ret[i].setName(names[i].trim());
else if (names.length == ret.length+1) // has x axis
ret[i].setName(names[i+1].trim());
}
Preconditions.checkState(split.length > 1);
if (dataSplits.isEmpty() && names != null) {
// first time through, see if x axis is labeled or not
if (names.length != split.length) {
if (names.length == split.length+1) // has x axis
names = Arrays.copyOfRange(names, 1, names.length);
else
names = null;
}
} else {
Preconditions.checkState(ret.length == split.length-1);
}
dataSplits.add(split);
}
Preconditions.checkState(!dataSplits.isEmpty());
double[] prevXValues = SpectraPlotter.PREV_X_VALUES;
if (prevXValues != null && prevXValues.length != dataSplits.size())
prevXValues = null;

double[] xValues = prevXValues == null ? new double[dataSplits.size()] : null;
double[][] yValues = new double[dataSplits.get(0).length-1][dataSplits.size()];

for (int i=0; i<dataSplits.size(); i++) {
String[] split = dataSplits.get(i);
Preconditions.checkState(split.length == yValues.length+1);

double x = Double.parseDouble(split[0]);
for (int i=0; i<ret.length; i++) {
// *'s mean zero seimograms, which should never happen
// if (split[i+1].startsWith("***"))
// ret[i].set(x, 0d);
// else
ret[i].set(x, Double.parseDouble(split[i+1]));
if (prevXValues != null && x != prevXValues[i]) {
Preconditions.checkState(xValues == null);
// need to keep track of our own x-values
if (i > 0)
// copy over what we had before
xValues = Arrays.copyOf(prevXValues, prevXValues.length);
else
xValues = new double[dataSplits.size()];
prevXValues = null;
}
if (xValues != null)
xValues[i] = x;

for (int j=0; j<split.length-1; j++)
yValues[j][i] = Double.parseDouble(split[j+1]);
}

if (xValues == null) {
Preconditions.checkNotNull(prevXValues);
xValues = prevXValues;
} else {
// cache for future use
SpectraPlotter.PREV_X_VALUES = xValues;
}
DiscretizedFunc[] ret = new DiscretizedFunc[yValues.length];
for (int i=0; i<ret.length; i++) {
ret[i] = new LightFixedXFunc(xValues, yValues[i]);
if (names != null)
ret[i].setName(names[i]);
}
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;

import org.opensha.commons.geo.GriddedRegion;
import org.opensha.commons.geo.Region;
import org.opensha.commons.geo.json.Feature;
import org.opensha.commons.hpc.JavaShellScriptWriter;
import org.opensha.commons.hpc.mpj.FastMPJShellScriptWriter;
Expand All @@ -29,15 +30,23 @@ public static void main(String[] args) throws IOException {

IncludeBackgroundOption[] bgOps = IncludeBackgroundOption.values();

boolean linkFromBase = true;
Double vs30 = null;
double gridSpacing = 0.1;

double[] periods = { 0d, 0.2d, 1d, 5d };
AttenRelRef[] gmms = null;

Region region = NSHM23_RegionLoader.loadFullConterminousWUS();

/*
* NSHM23
*/
// String baseDirName = "2024_02_02-nshm23_branches-WUS_FM_v3";
// String baseDirName = "2023_11_20-nshm23_branches-dm_sampling-randB-randSeg-NSHM23_v2-CoulombRupSet-DsrUni-TotNuclRate-NoRed-ThreshAvgIterRelGR";
// String baseDirName = "2023_11_17-nshm23_branches-dm_sampling-NSHM23_v2-CoulombRupSet-DsrUni-TotNuclRate-NoRed-ThreshAvgIterRelGR";
// String baseDirName = "2023_11_16-nshm23_branches-randB-randSeg-NSHM23_v2-CoulombRupSet-DsrUni-TotNuclRate-NoRed-ThreshAvgIterRelGR";
// String baseDirName = "2024_05_07-nshm23_branches-WUS_FM_v3-AvgSupraB-AvgSeg";
// String baseDirName = "2024_07_31-prvi25_subduction_branches";
// String baseDirName = "2024_09_04-prvi25_crustal_subduction_combined_branches";
// String baseDirName = "2024_09_04-prvi25_crustal_branches-dmSample5x";
String baseDirName = "2024_09_04-prvi25_subduction_branches";

// String suffix = "true_mean";
// String solFileName = "true_mean_solution.zip";
Expand All @@ -49,44 +58,68 @@ public static void main(String[] args) throws IOException {
// String suffix = "ba_only-mod_gridded";
// String solFileName = "results_WUS_FM_v3_branch_averaged_mod_gridded.zip";

// String suffix = "ba_only-LARGE";
// String solFileName = "results_PRVI_SUB_FM_LARGE_branch_averaged_gridded.zip";

// String suffix = "ba_only-LARGE-true_pt_src";
// String solFileName = "results_PRVI_SUB_FM_LARGE_branch_averaged_gridded.zip";

// String suffix = "ba_only";
// String solFileName = "combined_branch_averaged_solution.zip";

// String suffix = "ba_only";
// String solFileName = "results_PRVI_CRUSTAL_FM_V1p1_branch_averaged_gridded.zip";

// String suffix = "ba_only-SLAB_only";
// String solFileName = "results_PRVI_SLAB_ONLY_branch_averaged_gridded.zip";
// bgOps = new IncludeBackgroundOption[] { IncludeBackgroundOption.ONLY };

// String suffix = "ba_only-INTERFACE_only";
// String solFileName = "results_PRVI_INTERFACE_ONLY_branch_averaged_gridded.zip";

String suffix = "ba_only-both_fms";
String solFileName = "results_PRVI_SUB_FMs_combined_branch_averaged_gridded.zip";
/*
* PRVI
*/
//// String baseDirName = "2024_07_31-prvi25_subduction_branches";
//// String baseDirName = "2024_09_04-prvi25_crustal_subduction_combined_branches";
//// String baseDirName = "2024_09_04-prvi25_crustal_branches-dmSample5x";
// String baseDirName = "2024_09_04-prvi25_subduction_branches";
// region = PRVI25_RegionLoader.loadPRVI_ModelBroad();
// gridSpacing = 0.01;
//
// gmms = new AttenRelRef[] { AttenRelRef.USGS_PRVI_ACTIVE, AttenRelRef.USGS_PRVI_INTERFACE, AttenRelRef.USGS_PRVI_SLAB };
//
//// String suffix = "ba_only-LARGE";
//// String solFileName = "results_PRVI_SUB_FM_LARGE_branch_averaged_gridded.zip";
//
//// String suffix = "ba_only-LARGE-true_pt_src";
//// String solFileName = "results_PRVI_SUB_FM_LARGE_branch_averaged_gridded.zip";
//
//// String suffix = "ba_only";
//// String solFileName = "combined_branch_averaged_solution.zip";
//
//// String suffix = "ba_only";
//// String solFileName = "results_PRVI_CRUSTAL_FM_V1p1_branch_averaged_gridded.zip";
//
//// String suffix = "ba_only-SLAB_only";
//// String solFileName = "results_PRVI_SLAB_ONLY_branch_averaged_gridded.zip";
//// bgOps = new IncludeBackgroundOption[] { IncludeBackgroundOption.ONLY };
//
//// String suffix = "ba_only-INTERFACE_only";
//// String solFileName = "results_PRVI_INTERFACE_ONLY_branch_averaged_gridded.zip";
//
// String suffix = "ba_only-both_fms";
// String solFileName = "results_PRVI_SUB_FMs_combined_branch_averaged_gridded.zip";

/*
* RSQSim
*/
String suffix = null;

String baseDirName = "2024_11_12-rsqsim-wus-5895";
String solFileName = "fss_m6_skip10000_sectArea0.5_minSubSects2.zip";

// String baseDirName = "2024_11_12-rsqsim-wus-5892";
// String solFileName = "fss_m6_skip20000_sectArea0.5_minSubSects2.zip";
linkFromBase = false;

// periods = new double[] {0d, 1d, 2d, 3d, 5d};
// vs30 = 500d; suffix = "vs30_500";
periods = new double[] {2d, 3d, 5d};
gridSpacing = 0.5; suffix = "bbp_"+(float)gridSpacing;

boolean noMFDs = false;

// GriddedRegion gridReg = new GriddedRegion(
// NSHM23_RegionLoader.loadFullConterminousWUS(), 0.1, GriddedRegion.ANCHOR_0_0);
// GriddedRegion gridReg = new GriddedRegion(
// PRVI25_RegionLoader.loadPRVI_ModelBroad(), 0.1, GriddedRegion.ANCHOR_0_0);
GriddedRegion gridReg = new GriddedRegion(
PRVI25_RegionLoader.loadPRVI_MapExtents(), 0.01, GriddedRegion.ANCHOR_0_0);
region, gridSpacing, GriddedRegion.ANCHOR_0_0);
// GriddedRegion gridReg = new GriddedRegion(
// PRVI25_RegionLoader.loadPRVI_ModelBroad(), gridSpacing, GriddedRegion.ANCHOR_0_0);
System.out.println("Region has "+gridReg.getNodeCount()+" nodes");

double[] periods = { 0d, 0.2d, 1d, 5d };
// AttenRelRef gmm = AttenRelRef.AG_2020_GLOBAL_INTERFACE;
AttenRelRef[] gmms = { AttenRelRef.USGS_PRVI_ACTIVE, AttenRelRef.USGS_PRVI_INTERFACE, AttenRelRef.USGS_PRVI_SLAB };
// AttenRelRef[] gmms = null;

String dirName = baseDirName+"-"+suffix;
String dirName = baseDirName;
if (suffix != null && !suffix.isBlank())
dirName += "-"+suffix;
if (noMFDs)
dirName += "-no_mfds";

Expand Down Expand Up @@ -149,9 +182,11 @@ public static void main(String[] args) throws IOException {
String solFilePath = "$DIR/"+solFileName;

List<String> setupLines = new ArrayList<>();
setupLines.add("if [[ ! -e "+solFilePath+" ]];then");
setupLines.add(" ln -s $MAIN_DIR/"+baseDirName+"/"+solFileName+" "+solFilePath);
setupLines.add("fi");
if (linkFromBase) {
setupLines.add("if [[ ! -e "+solFilePath+" ]];then");
setupLines.add(" ln -s $MAIN_DIR/"+baseDirName+"/"+solFileName+" "+solFilePath);
setupLines.add("fi");
}
parallelMPJWrite.setCustomSetupLines(setupLines);
singleMPJWrite.setCustomSetupLines(setupLines);

Expand Down Expand Up @@ -189,6 +224,8 @@ else if (gridReg.getNodeCount() > 5000)
argz += " --region "+regPath;
if (noMFDs)
argz += " --no-mfds";
if (vs30 != null)
argz += " --vs30 "+vs30.floatValue();
argz += " --gridded-seis "+bgOp.name();
if (gmms != null)
for (AttenRelRef gmm : gmms)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.awt.geom.Point2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -107,7 +108,10 @@ private DiscretizedFunc calc(Site site, IMT imt, double curveDuration,
// double minRate = Double.POSITIVE_INFINITY;
simProv.getMinimumCurvePlotRate(site);
boolean allRatesSame = true;
for (E rupture : simProv.getRupturesForSite(site)) {
Collection<? extends E> rups = simProv.getRupturesForSite(site);
if (rups == null)
return null;
for (E rupture : rups) {
double rupRate = simProv.getAnnualRate(rupture);
if (rupRateScalars != null) {
Double scale = rupRateScalars.get(rupture);
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/scratch/kevin/simulators/RSQSimCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.opensha.sha.simulators.iden.LogicalAndRupIden;
import org.opensha.sha.simulators.iden.LogicalOrRupIden;
import org.opensha.sha.simulators.iden.MagRangeRuptureIdentifier;
import org.opensha.sha.simulators.iden.MinNumMappedSubSectsIden;
import org.opensha.sha.simulators.iden.RegionIden;
import org.opensha.sha.simulators.iden.RuptureIdentifier;
import org.opensha.sha.simulators.iden.SectionIDIden;
Expand Down Expand Up @@ -793,7 +794,7 @@ public enum Catalogs {
"WUSav, delta=2.0km, sigma0=100, b=.008, alpha=0.25",
NSHM23_FaultModels.WUS_FM_v3, NSHM23_DeformationModels.AVERAGE),
BRUCE_5895("rundir5895", "Bruce 5895", "Bruce Shaw", cal(2024, 9, 12),
"WUSav, delta=2.0km, sigma0=100, b=.008, alpha=0.25, dynamic",
"WUSav, delta=2.0km, sigma0=100, bdeep=.009 bshallow=.003, alpha=0.25, hload=hst=3, dynamic",
NSHM23_FaultModels.WUS_FM_v3, NSHM23_DeformationModels.AVERAGE),
BRUCE_5932("rundir5932", "Bruce 5932", "Bruce Shaw", cal(2024, 9, 12),
"CA, delta=1.0km, sigma0=100, bdeep=.013 bshallow=.003, alpha=0.25, hload=hst=3, dynamic tcausalFactor=.60",
Expand Down Expand Up @@ -1762,8 +1763,16 @@ public void setFractForInclusion(double minFractForInclusion) {
}

public synchronized List<? extends FaultSection> getSubSects() throws IOException {
if (subSects == null)
subSects = getDeformationModel().build(getFaultModel());
if (subSects == null) {
if (getDeformationModel() != null && getFaultModel() != null) {
subSects = getDeformationModel().build(getFaultModel());
} else {
FaultSystemSolution compSol = getComparisonSolution();
Preconditions.checkNotNull(compSol,
"Can't get subsections without a fault model and deformation model nor comparison sol");
subSects = compSol.getRupSet().getFaultSectionDataList();
}
}
return subSects;
}

Expand Down Expand Up @@ -1976,6 +1985,15 @@ public Loader forEventIDRange(com.google.common.collect.Range<Integer> range) {
return this;
}

public Loader minMappedSubSects(int minSubSects) throws IOException {
return minMappedSubSects(minSubSects, getSubSectMapper());
}

public Loader minMappedSubSects(int minSubSects, RSQSimSubSectionMapper subSectMapper) {
loadIdens.add(new MinNumMappedSubSectsIden(minSubSects, subSectMapper));
return this;
}

public List<RSQSimEvent> load() throws IOException {
LogicalAndRupIden loadIden = new LogicalAndRupIden(loadIdens);
List<RuptureIdentifier> rupIdens = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;

import org.opensha.commons.exceptions.GMT_MapException;
import org.opensha.sha.earthquake.faultSysSolution.FaultSystemSolution;
import org.opensha.sha.simulators.RSQSimEvent;
import org.opensha.sha.simulators.SimulatorElement;
import org.opensha.sha.simulators.iden.EventTimeIdentifier;
Expand Down Expand Up @@ -75,7 +76,7 @@ public static void main(String[] args) throws IOException, GMT_MapException, Run

FaultModels fm = FaultModels.FM3_1;
DeformationModels dm = DeformationModels.GEOLOGIC;
U3SlipEnabledSolution sol = RSQSimUtils.buildFaultSystemSolution(RSQSimUtils.getUCERF3SubSectsForComparison(
FaultSystemSolution sol = RSQSimUtils.buildFaultSystemSolution(RSQSimUtils.getUCERF3SubSectsForComparison(
fm, dm), elements, events, minMag);

Preconditions.checkState(plotDir.exists() || plotDir.mkdir());
Expand Down
Loading

0 comments on commit dc9b509

Please sign in to comment.