Skip to content

Commit

Permalink
for upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Milner committed Aug 21, 2024
1 parent 560af03 commit 16d141e
Show file tree
Hide file tree
Showing 9 changed files with 1,155 additions and 526 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ apply from: '../opensha/build-common.gradle'

dependencies {

api "ghsc:nshmp-lib:1.4.11"
api "ghsc:nshmp-lib:1.4.26"

implementation 'org.apache.pdfbox:pdfbox:2.0.6'
implementation 'org.apache.commons:commons-collections4:4.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.opensha.commons.logicTree.LogicTreeLevel;
import org.opensha.commons.logicTree.LogicTreeNode;
import org.opensha.commons.util.ExceptionUtils;
import org.opensha.commons.util.ExecutorUtils;
import org.opensha.commons.util.modules.OpenSHA_Module;
import org.opensha.sha.earthquake.faultSysSolution.FaultSystemSolution;
import org.opensha.sha.earthquake.faultSysSolution.modules.BranchAverageableModule;
Expand Down Expand Up @@ -262,9 +263,7 @@ public static void main(String[] args) throws IOException {
// this dicates how far ahead we will read in solutions, needs to be > asyncThreads to make use of parallel
// reading, but should be small enough soas to not use too much memory storing to-be-processed solutions
int maxTasks = Integer.min(asyncThreads * 2, asyncThreads + 2);
ExecutorService exec = new ThreadPoolExecutor(asyncThreads, asyncThreads,
0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(maxTasks), new ThreadPoolExecutor.CallerRunsPolicy());
ExecutorService exec = ExecutorUtils.newBlockingThreadPool(asyncThreads, maxTasks);

int count = 0;
List<Future<?>> futures = new ArrayList<>();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,37 @@ public static void main(String[] args) throws IOException {
if (cmd.hasOption("gridded-seis"))
bgOp = IncludeBackgroundOption.valueOf(cmd.getOptionValue("gridded-seis"));

LogicTree<?> crustalLT;
SolutionLogicTree crustalSLT;
LogicTree<?> subductionLT;
SolutionLogicTree subductionSLT;
if (cmd.hasOption("logic-tree-file-name")) {
String logicTreeFileName = cmd.getOptionValue("logic-tree-file-name");
File crustalLogicTreefile = new File(crustalDir, logicTreeFileName);
LogicTree<?> crustalTree = LogicTree.read(crustalLogicTreefile);
crustalLT = LogicTree.read(crustalLogicTreefile);
File subductionLogicTreeFile = new File(subductionDir, logicTreeFileName);
LogicTree<?> subductionTree = LogicTree.read(subductionLogicTreeFile);
crustalSLT = SolutionLogicTree.load(new File(crustalDir, resultsFileName), crustalTree);
subductionSLT = SolutionLogicTree.load(new File(subductionDir, resultsFileName), subductionTree);
subductionLT = LogicTree.read(subductionLogicTreeFile);
File crustalSLTFile = new File(crustalDir, resultsFileName);
if (crustalSLTFile.exists())
crustalSLT = SolutionLogicTree.load(crustalSLTFile, crustalLT);
else
crustalSLT = null;
File subductionSLTFile = new File(subductionDir, resultsFileName);
if (subductionSLTFile.exists())
subductionSLT = SolutionLogicTree.load(subductionSLTFile, subductionLT);
else
subductionSLT = null;
} else {
crustalSLT = SolutionLogicTree.load(new File(crustalDir, resultsFileName));
crustalLT = crustalSLT.getLogicTree();
subductionSLT = SolutionLogicTree.load(new File(subductionDir, resultsFileName));
subductionLT = subductionSLT.getLogicTree();
}

crustalSLT.setVerbose(false);
subductionSLT.setVerbose(false);
if (crustalSLT != null)
crustalSLT.setVerbose(false);
if (subductionSLT != null)
subductionSLT.setVerbose(false);

GriddedRegion gridReg = loadGridReg(new File(crustalDir, gridRegFileName));
Preconditions.checkState(gridReg.equals(loadGridReg(new File(subductionDir, gridRegFileName))),
Expand All @@ -91,35 +105,67 @@ public static void main(String[] args) throws IOException {
else if (bgOp == IncludeBackgroundOption.EXCLUDE)
outputHazardFileName = "results_hazard.zip";
else if (bgOp == IncludeBackgroundOption.INCLUDE)
outputHazardFileName = "results_hazard_gridded.zip";
outputHazardFileName = "results_hazard_full_gridded.zip";
else if (bgOp == IncludeBackgroundOption.ONLY)
outputHazardFileName = "results_hazard_gridded_only.zip";
else
throw new IllegalStateException();

File resultsOutputFile = cmd.hasOption("disable-write-results") ? null : new File(outputDir, resultsFileName);

// CrustalSubductionLogicTreeCombine combiner = new CrustalSubductionLogicTreeCombine(
// crustalLT, crustalSLT, new File(crustalDir, hazardDirName), bgOp,
// subductionLT, subductionSLT, new File(subductionDir, hazardDirName), bgOp,
// resultsOutputFile,
// new File(outputDir, outputHazardFileName), gridReg);
CrustalSubductionLogicTreeCombine combiner = new CrustalSubductionLogicTreeCombine(
crustalSLT, new File(crustalDir, hazardDirName), bgOp,
subductionSLT, new File(subductionDir, hazardDirName), bgOp,
resultsOutputFile,
new File(outputDir, outputHazardFileName), gridReg);
crustalLT, subductionLT);

if (!cmd.hasOption("disable-write-results")) {
File resultsOutputFile = new File(outputDir, resultsFileName);
Preconditions.checkNotNull(crustalSLT);
Preconditions.checkNotNull(subductionSLT);
combiner.setWriteSLTs(crustalSLT, subductionSLT, resultsOutputFile);
}

combiner.setCombineHazardMaps(new File(crustalDir, hazardDirName), bgOp,
new File(subductionDir, hazardDirName), bgOp, new File(outputDir, outputHazardFileName), gridReg);

long treeSize = (long)crustalLT.size()*(long)subductionLT.size();
if (cmd.hasOption("pre-samples")) {
// more memory efficient pre-sampling version
int samples = Integer.parseInt(cmd.getOptionValue("pre-samples"));
long seed = cmd.hasOption("rand-seed") ? Long.parseLong(cmd.getOptionValue("rand-seed")) : treeSize*(long)samples;
combiner.pairwiseSampleTree(samples, seed);
}

if (cmd.hasOption("samples")) {
int samples = Integer.parseInt(cmd.getOptionValue("samples"));
int treeSize = combiner.getCombTree().size();
if (samples < treeSize) {
long seed = cmd.hasOption("rand-seed") ? Long.parseLong(cmd.getOptionValue("rand-seed")) : (long)treeSize*(long)samples;
long seed = cmd.hasOption("rand-seed") ? Long.parseLong(cmd.getOptionValue("rand-seed")) : treeSize*(long)samples;
combiner.sampleTree(samples, seed);
}
}

combiner.build();
LogicTree<LogicTreeNode> combTree = combiner.getCombTree();
File treeOutputFile;
if (cmd.hasOption("logic-tree-file-name"))
treeOutputFile = new File(outputDir, cmd.getOptionValue("logic-tree-file-name"));
else
treeOutputFile = new File(outputDir, "logic_tree_"+bgOp.name()+".json");
combTree.write(treeOutputFile);

try {
combiner.build();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

public static Options createOptions() {
Options ops = new Options();


ops.addOption(null, "pre-samples", true, "Number of pre-samples to draw. This is more memory efficient and doesn't require "
+ "instantiating the entire tree in memory first.");
ops.addOption(null, "samples", true, "Number of samples to draw");
ops.addOption(null, "rand-seed", true, "Random seed for use when sampling");
ops.addOption(null, "gridded-seis", true, "Gridded seismicity option. One of "
Expand All @@ -134,11 +180,8 @@ public static Options createOptions() {
return ops;
}

public CrustalSubductionLogicTreeCombine(SolutionLogicTree outerSLT, File outerHazardDir,
IncludeBackgroundOption outerBGOp, SolutionLogicTree innerSLT, File innerHazardDir,
IncludeBackgroundOption innerBGOp, File outputSLTFile, File outputHazardFile, GriddedRegion gridReg) {
super(outerSLT, outerHazardDir, outerBGOp, innerSLT, innerHazardDir, innerBGOp, outputSLTFile, outputHazardFile,
gridReg);
public CrustalSubductionLogicTreeCombine(LogicTree<?> outerLT, LogicTree<?> innerLT) {
super(outerLT, innerLT);
}

private static GriddedRegion loadGridReg(File regFile) throws IOException {
Expand Down Expand Up @@ -212,8 +255,8 @@ protected boolean isSerializeGridded() {
}

@Override
protected boolean canSkipLevel(LogicTreeLevel<?> level, boolean inner) {
return false;
protected List<LogicTreeLevel<?>> getCommonLevels() {
return List.of();
}

}
Loading

0 comments on commit 16d141e

Please sign in to comment.