Skip to content

Commit

Permalink
updates for upstream project, mostly PRVI and some RSQSim
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Milner committed Sep 6, 2024
1 parent 2409324 commit 04973ce
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ public static void main(String[] args) throws IOException {
List<RandomlySampledLevel<?>> individualRandomLevels = new ArrayList<>();
int samplingBranchCountMultiplier = 1;

// String dirName = new SimpleDateFormat("yyyy_MM_dd").format(new Date());
String dirName = "2024_08_16";
String dirName = new SimpleDateFormat("yyyy_MM_dd").format(new Date());
// String dirName = "2024_09_04";
String dirSuffix = null;

/*
Expand Down Expand Up @@ -602,28 +602,28 @@ public static void main(String[] args) throws IOException {
* PRVI25 logic tree
* TODO (this is a just a marker to find this part quickly, not an actual todo)
*/
List<LogicTreeLevel<? extends LogicTreeNode>> levels = PRVI25_LogicTreeBranch.levelsOnFault;
dirName += "-prvi25_crustal_branches";
double avgNumRups = 50000;
gmpes = new AttenRelRef[] { AttenRelRef.USGS_PRVI_ACTIVE };

// random DM sampling
levels = new ArrayList<>(levels);
int origNumLevels = levels.size();
for (int i=levels.size(); --i>=0;)
if (levels.get(i).getNodes().get(0) instanceof PRVI25_CrustalDeformationModels)
levels.remove(i);
Preconditions.checkState(levels.size() == origNumLevels -1);
individualRandomLevels.add(new PRVI25_CrustalRandomlySampledDeformationModelLevel());
samplingBranchCountMultiplier = 5; // 5 for each branch
dirName += "-dmSample";
if (samplingBranchCountMultiplier > 1)
dirName += samplingBranchCountMultiplier+"x";
// List<LogicTreeLevel<? extends LogicTreeNode>> levels = PRVI25_LogicTreeBranch.levelsOnFault;
// dirName += "-prvi25_crustal_branches";
// double avgNumRups = 50000;
// gmpes = new AttenRelRef[] { AttenRelRef.USGS_PRVI_ACTIVE };
//
// // random DM sampling
// levels = new ArrayList<>(levels);
// int origNumLevels = levels.size();
// for (int i=levels.size(); --i>=0;)
// if (levels.get(i).getNodes().get(0) instanceof PRVI25_CrustalDeformationModels)
// levels.remove(i);
// Preconditions.checkState(levels.size() == origNumLevels -1);
// individualRandomLevels.add(new PRVI25_CrustalRandomlySampledDeformationModelLevel());
// samplingBranchCountMultiplier = 5; // 5 for each branch
// dirName += "-dmSample";
// if (samplingBranchCountMultiplier > 1)
// dirName += samplingBranchCountMultiplier+"x";

// List<LogicTreeLevel<? extends LogicTreeNode>> levels = PRVI25_LogicTreeBranch.levelsSubduction;
// dirName += "-prvi25_subduction_branches";
// double avgNumRups = 10000;
// gmpes = new AttenRelRef[] { AttenRelRef.USGS_PRVI_INTERFACE, AttenRelRef.USGS_PRVI_SLAB };
List<LogicTreeLevel<? extends LogicTreeNode>> levels = PRVI25_LogicTreeBranch.levelsSubduction;
dirName += "-prvi25_subduction_branches";
double avgNumRups = 10000;
gmpes = new AttenRelRef[] { AttenRelRef.USGS_PRVI_INTERFACE, AttenRelRef.USGS_PRVI_SLAB };

// levels = new ArrayList<>(levels);
// levels.add(NSHM23_LogicTreeBranch.SUB_SECT_CONSTR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void main(String[] args) throws IOException {
File crustalDir = new File(args[0]);
File subductionDir = new File(args[1]);
File outputFile = new File(args[2]);
boolean gridded = args.length < 4 ? false : Boolean.parseBoolean(args[3]);
boolean gridded = args.length < 4 ? true : Boolean.parseBoolean(args[3]);

Map<PRVI25_CrustalFaultModels, FaultSystemSolution> crustalBASols = new HashMap<>();
Map<PRVI25_SubductionFaultModels, FaultSystemSolution> subductionBASols = new HashMap<>();
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/scratch/kevin/prvi25/SolAreaValidate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package scratch.kevin.prvi25;

import java.io.File;
import java.io.IOException;

import org.opensha.commons.calc.FaultMomentCalc;
import org.opensha.commons.eq.MagUtils;
import org.opensha.sha.earthquake.faultSysSolution.FaultSystemRupSet;
import org.opensha.sha.earthquake.faultSysSolution.modules.AveSlipModule;

public class SolAreaValidate {

public static void main(String[] args) throws IOException {
FaultSystemRupSet rupSet = FaultSystemRupSet.load(new File(args[0]));
AveSlipModule slips = rupSet.getModule(AveSlipModule.class);

int worstSectMismatch = -1;
double worstSectDiff = 0d;
double worstSectSumArea = 0d;

int worstMagMismatch = -1;
double worstMagDiff = 0d;
double worstMomentToMag = 0d;
for (int r=0; r<rupSet.getNumRuptures(); r++) {
double rupArea = rupSet.getAreaForRup(r);
double sumSectArea = 0d;
for (int s : rupSet.getSectionsIndicesForRup(r))
sumSectArea += rupSet.getAreaForSection(s);

if (slips != null) {
double mag = rupSet.getMagForRup(r);
double slip = slips.getAveSlip(r);
// double moment = MagUtils.magToMoment(mag);
double moment = FaultMomentCalc.getMoment(rupArea, slip);
double momentToMag = MagUtils.momentToMag(moment);
double magDiff = Math.abs(momentToMag - mag);
if (magDiff > worstMagDiff) {
worstMagDiff = magDiff;
worstMagMismatch = r;
worstMomentToMag = momentToMag;
}
}

double sectDiff = Math.abs(sumSectArea - rupArea);
if (sectDiff > worstSectDiff) {
worstSectMismatch = r;
worstSectDiff = sectDiff;
worstSectSumArea = sumSectArea;
}
}

if (worstSectMismatch >= 0) {
System.out.println("Worst sum[sectAreas] vs rupArea mismatch: rupture "+worstSectMismatch);
System.out.println("\tsum[sectAreas]:\t"+(float)worstSectSumArea);
System.out.println("\trupArea:\t"+(float)rupSet.getAreaForRup(worstSectMismatch));
System.out.println("\tdiff:\t"+(float)(rupSet.getAreaForRup(worstSectMismatch) - worstSectSumArea));
}
if (worstMagMismatch >= 0) {
System.out.println("Worst mag vs area&slip->moment->mag: rupture "+worstMagMismatch);
System.out.println("\tmag:\t"+(float)rupSet.getMagForRup(worstMagMismatch));
System.out.println("\tmomentToMag:\t"+(float)worstMomentToMag);
System.out.println("\trupArea:\t"+(float)rupSet.getAreaForRup(worstMagMismatch));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
import org.opensha.commons.gui.plot.PlotLineType;
import org.opensha.commons.gui.plot.PlotSpec;
import org.opensha.commons.gui.plot.PlotUtils;
import org.opensha.commons.logicTree.LogicTreeBranch;
import org.opensha.sha.earthquake.faultSysSolution.FaultSystemSolution;
import org.opensha.sha.earthquake.faultSysSolution.util.FaultSysTools;
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_MaxMagOffFault;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_DeclusteringAlgorithms;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_LogicTreeBranch;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_RegionalSeismicity;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_SeisSmoothingAlgorithms;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.util.PRVI25_RegionLoader;
Expand Down Expand Up @@ -49,7 +52,8 @@ public static void main(String[] args) throws IOException {

EvenlyDiscretizedFunc refMFD = FaultSysTools.initEmptyMFD(sol.getRupSet());

double mMax = 7.6;
NSHM23_MaxMagOffFault mMax = NSHM23_MaxMagOffFault.MAG_7p6;

DecimalFormat fractDF = new DecimalFormat("0.###");

for (int r=0; r<regions.size(); r++) {
Expand All @@ -71,9 +75,9 @@ public static void main(String[] args) throws IOException {

System.out.println("fractN="+(float)fractN+" for "+names.get(r));

IncrementalMagFreqDist lower = PRVI25_RegionalSeismicity.LOW.build(PRVI25_SeismicityRegions.CRUSTAL, refMFD, mMax);
IncrementalMagFreqDist upper = PRVI25_RegionalSeismicity.HIGH.build(PRVI25_SeismicityRegions.CRUSTAL, refMFD, mMax);
IncrementalMagFreqDist pref = PRVI25_RegionalSeismicity.PREFFERRED.build(PRVI25_SeismicityRegions.CRUSTAL, refMFD, mMax);
IncrementalMagFreqDist lower = PRVI25_RegionalSeismicity.LOW.build(PRVI25_SeismicityRegions.CRUSTAL, refMFD, mMax.getMaxMagOffFault());
IncrementalMagFreqDist upper = PRVI25_RegionalSeismicity.HIGH.build(PRVI25_SeismicityRegions.CRUSTAL, refMFD, mMax.getMaxMagOffFault());
IncrementalMagFreqDist pref = PRVI25_RegionalSeismicity.PREFFERRED.build(PRVI25_SeismicityRegions.CRUSTAL, refMFD, mMax.getMaxMagOffFault());
IncrementalMagFreqDist origPref = pref.deepClone();
if (remapped) {
lower.scale(fractN);
Expand Down
80 changes: 45 additions & 35 deletions src/main/java/scratch/kevin/simulators/RSQSimCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ public RSQSimCatalog instance(File baseDir) {
private Map<IDPairing, Double> subSectDistsCache;
private RSQSimSubSectionMapper subSectMapper;

private static final File fmDmSolDir = new File(System.getProperty("user.home"), ".opensha/ucerf3_fm_dm_sols/");
// private static final File fmDmSolDir = new File(System.getProperty("user.home"), ".opensha/ucerf3_fm_dm_sols/");
private FaultSystemSolution compSol;

public static final double MIN_SUB_SECT_FRACT_DEFAULT = 0.2;
Expand Down Expand Up @@ -1782,28 +1782,6 @@ private File getSolCacheDir() {
}
}

public FaultSystemSolution getCompareSol() throws IOException {
synchronized (compSolsTable) {
FaultSystemSolution sol = compSolsTable.get(fm, dm);

if (sol == null) {
File solDir = getSolCacheDir();
File solFile = new File(solDir, fm.getFilePrefix()+"_"+dm.getFilePrefix()+"_MEAN_BRANCH_AVG_SOL.zip");
if (!solFile.exists()) {
// download it
String addr = "http://opensha.usc.edu/ftp/kmilner/ucerf3/2013_05_10-ucerf3p3-production-10runs_fm_dm_sub_plots/"
+ fm.getFilePrefix()+"_"+dm.getFilePrefix()+"/"+solFile.getName();
FileUtils.downloadURL(addr, solFile);
}

sol = FaultSystemSolution.load(solFile);
compSolsTable.put(fm, dm, sol);
}

return sol;
}
}

private synchronized Map<Integer, Double> getSubSectAreas() throws IOException {
if (subSectAreas == null)
subSectAreas = RSQSimUtils.calcSubSectAreas(getElements(), getSubSects());
Expand Down Expand Up @@ -2019,15 +1997,39 @@ public FaultSystemSolution buildSolution(List<RSQSimEvent> events, double minMag
return RSQSimUtils.buildFaultSystemSolution(getSubSects(), getElements(), events, minMag, minFractForInclusion);
}

// TODO duplicate with getCompareSol()?
public FaultSystemSolution getComparisonSolution() throws IOException {
File solFile = new File(fmDmSolDir, getFaultModel().getFilePrefix()
+"_"+getDeformationModel().getFilePrefix()+"_MEAN_BRANCH_AVG_SOL.zip");
if (solFile.exists()) {
System.out.println("Loading comparison FSS from "+solFile.getAbsolutePath());
compSol = FaultSystemSolution.load(solFile);
} else {
System.out.println("Comparison sol file doesn't exist: "+solFile.getAbsolutePath());
public synchronized FaultSystemSolution getComparisonSolution() throws IOException {
if (compSol != null)
return compSol;
File solFile = null;
if (getCatalogDir() != null) {
solFile = new File(getCatalogDir(), "comparison_solution.zip");
if (!solFile.exists())
solFile = null;
}
if (solFile == null && fm != null && dm != null) {
File solDir = getSolCacheDir();
solFile = new File(solDir, fm.getFilePrefix()+"_"+dm.getFilePrefix()+"_MEAN_BRANCH_AVG_SOL.zip");
if (!solFile.exists()) {
// download it
String addr = "http://data.opensha.org/ftp/kmilner/ucerf3/2013_05_10-ucerf3p3-production-10runs_fm_dm_sub_plots/"
+ fm.getFilePrefix()+"_"+dm.getFilePrefix()+"/"+solFile.getName();
try {
FileUtils.downloadURL(addr, solFile);
} catch (IOException e) {
System.err.println("Failed to download comparison solution from: "+addr);
e.printStackTrace();
solFile = null;
}
}
}

if (solFile != null) {
if (solFile.exists()) {
System.out.println("Loading comparison FSS from "+solFile.getAbsolutePath());
compSol = FaultSystemSolution.load(solFile);
} else {
System.out.println("Comparison sol file doesn't exist: "+solFile.getAbsolutePath());
}
}
return compSol;
}
Expand Down Expand Up @@ -2059,14 +2061,22 @@ private List<AbstractPlot> buildStandardPlotLines(File outputDir, int skipYears,

boolean hasFM = getFaultModel() != null && getDeformationModel() != null;

String compSolName;
if (fm != null && fm instanceof FaultModels)
compSolName = "UCERF3";
else if (fm != null && fm instanceof NSHM23_FaultModels)
compSolName = "NSHM23";
else
compSolName = "Comparison";

TableBuilder table;

if (plotsSet.contains(StandardPlots.MFD)) {
if (replot || !new File(outputDir, "mfd.png").exists()) {
MFDPlot mfdPlot = new MFDPlot(minMag);
File mfdCSV = null;
if (hasFM)
mfdCSV = new File(fmDmSolDir, getFaultModel().getFilePrefix()
mfdCSV = new File(getSolCacheDir(), getFaultModel().getFilePrefix()
+"_"+getDeformationModel().getFilePrefix()+"_supra_plus_sub_seis_cumulative.csv");
// System.out.println(mfdCSV.getAbsolutePath()+" ? "+mfdCSV.exists());
if (mfdCSV != null && mfdCSV.exists()) {
Expand Down Expand Up @@ -2583,12 +2593,12 @@ private List<AbstractPlot> buildStandardPlotLines(File outputDir, int skipYears,
if (plotsSet.contains(StandardPlots.SECTION_RECURRENCE) && hasFM) {
if (replot || !new File(outputDir, "interevent_elements_m"+testMagStr+"_scatter.png").exists()) {
RSQSimSubSectionMapper mapper = getSubSectMapper();
SectionRecurrenceComparePlot elemCompare = new SectionRecurrenceComparePlot(getElements(), getCompareSol(), "UCERF3",
SectionRecurrenceComparePlot elemCompare = new SectionRecurrenceComparePlot(getElements(), getComparisonSolution(), compSolName,
SectionRecurrenceComparePlot.SectType.ELEMENT, mapper, riMinMags);
elemCompare.initialize(getName(), outputDir, "interevent_elements");
plots.add(elemCompare);

SectionRecurrenceComparePlot subSectCompare = new SectionRecurrenceComparePlot(getElements(), getCompareSol(), "UCERF3",
SectionRecurrenceComparePlot subSectCompare = new SectionRecurrenceComparePlot(getElements(), getComparisonSolution(), compSolName,
SectionRecurrenceComparePlot.SectType.SUBSECTION, mapper, riMinMags);
subSectCompare.initialize(getName(), outputDir, "interevent_sub_sects");
plots.add(subSectCompare);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ public static void main(String[] args) throws IOException, DocumentException, GM
// lines.add("");

System.out.println("Plotting connectivity clusters");
plotConnectivity(catalog.getCompareSol().getRupSet(), resourcesDir, "connectivity_ucerf3", "UCERF3 Connectivity");
plotConnectivity(catalog.getComparisonSolution().getRupSet(), resourcesDir, "connectivity_ucerf3", "UCERF3 Connectivity");
plotConnectivity(sol.getRupSet(), resourcesDir, "connectivity_"+catalogTypeFileName, catalogName+" Connectivity");

lines.add("## Fault Connectivity Clusters");
Expand All @@ -713,7 +713,7 @@ public static void main(String[] args) throws IOException, DocumentException, GM

// cumulant mag
System.out.println("Plotting cumulant mag");
plotCumulantMags(catalog.getCompareSol(), sol, catalogType, resourcesDir);
plotCumulantMags(catalog.getComparisonSolution(), sol, catalogType, resourcesDir);

lines.add("## Cumulant Magnitude");
lines.add(topLink); lines.add("");
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/scratch/kevin/ucerf3/PureScratch.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_DeformationModels;
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_FaultModels;
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_LogicTreeBranch;
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_MaxMagOffFault;
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_ScalingRelationships;
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_SegmentationModels;
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_SingleStates;
Expand All @@ -164,8 +165,10 @@
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_CrustalDeformationModels;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_CrustalFaultModels;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_CrustalGMMs;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_DeclusteringAlgorithms;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_LogicTreeBranch;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_RegionalSeismicity;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.logicTree.PRVI25_SeisSmoothingAlgorithms;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.util.PRVI25_RegionLoader;
import org.opensha.sha.earthquake.rupForecastImpl.prvi25.util.PRVI25_RegionLoader.PRVI25_SeismicityRegions;
import org.opensha.sha.faultSurface.FaultSection;
Expand Down Expand Up @@ -2825,13 +2828,31 @@ private static void test325() throws IOException {
tree.write(outFile);
}

private static void test326() throws IOException {
File dir = new File("/home/kevin/OpenSHA/nshm23/batch_inversions/2024_08_16-prvi25_crustal_branches-dmSample5x/");
File baSolFile = new File(dir, "results_PRVI_CRUSTAL_FM_V1p1_branch_averaged.zip");
FaultSystemSolution baSol = FaultSystemSolution.load(baSolFile);

LogicTreeBranch<?> gridBranch = PRVI25_LogicTreeBranch.fromValues(PRVI25_LogicTreeBranch.levelsCrustalOffFault,
PRVI25_RegionalSeismicity.PREFFERRED,
PRVI25_DeclusteringAlgorithms.AVERAGE,
PRVI25_SeisSmoothingAlgorithms.AVERAGE,
NSHM23_MaxMagOffFault.MAG_7p6);

PRVI25_GridSourceBuilder.doPreGridBuildHook(baSol, baSol.requireModule(LogicTreeBranch.class));
GridSourceList gridSources = PRVI25_GridSourceBuilder.buildCrustalGridSourceProv(baSol, gridBranch);
baSol.setGridSourceProvider(gridSources);

baSol.write(new File("/tmp/prvi_test_grid.zip"));
}

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
try {
test325();
test326();
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
Expand Down
Loading

0 comments on commit 04973ce

Please sign in to comment.