Skip to content

Commit b5730ab

Browse files
committed
utils for branch average hazard job dirs
1 parent fb83b54 commit b5730ab

File tree

4 files changed

+273
-2
lines changed

4 files changed

+273
-2
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package scratch.kevin.nshm23;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.opensha.commons.geo.GriddedRegion;
9+
import org.opensha.commons.geo.json.Feature;
10+
import org.opensha.commons.hpc.JavaShellScriptWriter;
11+
import org.opensha.commons.hpc.mpj.FastMPJShellScriptWriter;
12+
import org.opensha.commons.hpc.mpj.NoMPJSingleNodeShellScriptWriter;
13+
import org.opensha.commons.hpc.pbs.BatchScriptWriter;
14+
import org.opensha.commons.hpc.pbs.HovenweepScriptWriter;
15+
import org.opensha.commons.hpc.pbs.USC_CARC_ScriptWriter;
16+
import org.opensha.sha.earthquake.faultSysSolution.hazard.mpj.MPJ_SingleSolHazardCalc;
17+
import org.opensha.sha.earthquake.param.IncludeBackgroundOption;
18+
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.util.NSHM23_RegionLoader;
19+
20+
import com.google.common.base.Preconditions;
21+
22+
import edu.usc.kmilner.mpj.taskDispatch.MPJTaskCalculator;
23+
24+
public class BranchAveragedHazardScriptWriter {
25+
26+
public static void main(String[] args) throws IOException {
27+
String baseDirName = "2024_02_02-nshm23_branches-WUS_FM_v3";
28+
29+
// String suffix = "true_mean";
30+
// String solFileName = "true_mean_solution.zip";
31+
32+
String suffix = "ba_only";
33+
String solFileName = "results_WUS_FM_v3_branch_averaged_gridded.zip";
34+
35+
boolean noMFDs = false;
36+
37+
GriddedRegion gridReg = new GriddedRegion(
38+
NSHM23_RegionLoader.loadFullConterminousWUS(), 0.1, GriddedRegion.ANCHOR_0_0);
39+
40+
IncludeBackgroundOption[] bgOps = IncludeBackgroundOption.values();
41+
42+
String dirName = baseDirName+"-"+suffix;
43+
if (noMFDs)
44+
dirName += "-no_mfds";
45+
46+
File localMainDir = new File("/home/kevin/OpenSHA/UCERF4/batch_inversions");
47+
File localDir = new File(localMainDir, dirName);
48+
Preconditions.checkState(localDir.exists() || localDir.mkdir());
49+
50+
// File remoteMainDir = new File("/project/scec_608/kmilner/nshm23/batch_inversions");
51+
// int remoteTotalThreads = 20;
52+
// int remoteTotalMemGB = 50;
53+
// String queue = "scec";
54+
// int nodes = 36;
55+
// int mins = 600;
56+
//// int nodes = 18;
57+
//// JavaShellScriptWriter mpjWrite = new MPJExpressShellScriptWriter(
58+
//// USC_CARC_ScriptWriter.JAVA_BIN, remoteTotalMemGB*1024, null, USC_CARC_ScriptWriter.MPJ_HOME);
59+
// JavaShellScriptWriter parallelMPJWrite = new FastMPJShellScriptWriter(
60+
// USC_CARC_ScriptWriter.JAVA_BIN, remoteTotalMemGB*1024, null, USC_CARC_ScriptWriter.FMPJ_HOME);
61+
// JavaShellScriptWriter singleMPJWrite = new NoMPJSingleNodeShellScriptWriter(USC_CARC_ScriptWriter.JAVA_BIN,
62+
// remoteTotalMemGB*1024, null);
63+
// BatchScriptWriter pbsWrite = new USC_CARC_ScriptWriter();
64+
65+
File remoteMainDir = new File("/caldera/hovenweep/projects/usgs/hazards/ehp/kmilner/nshm23/batch_inversions");
66+
int remoteTotalThreads = 128;
67+
int remoteTotalMemGB = 448;
68+
String queue = null;
69+
int nodes = 4;
70+
int mins = 180;
71+
// int nodes = 18;
72+
// JavaShellScriptWriter mpjWrite = new MPJExpressShellScriptWriter(
73+
// USC_CARC_ScriptWriter.JAVA_BIN, remoteTotalMemGB*1024, null, USC_CARC_ScriptWriter.MPJ_HOME);
74+
JavaShellScriptWriter parallelMPJWrite = new FastMPJShellScriptWriter(
75+
HovenweepScriptWriter.JAVA_BIN, remoteTotalMemGB*1024, null, HovenweepScriptWriter.FMPJ_HOME);
76+
JavaShellScriptWriter singleMPJWrite = new NoMPJSingleNodeShellScriptWriter(HovenweepScriptWriter.JAVA_BIN,
77+
remoteTotalMemGB*1024, null);
78+
BatchScriptWriter pbsWrite = new HovenweepScriptWriter();
79+
80+
parallelMPJWrite.setEnvVar("MAIN_DIR", remoteMainDir.getAbsolutePath());
81+
singleMPJWrite.setEnvVar("MAIN_DIR", remoteMainDir.getAbsolutePath());
82+
String mainDirPath = "$MAIN_DIR";
83+
parallelMPJWrite.setEnvVar("DIR", mainDirPath+"/"+dirName);
84+
singleMPJWrite.setEnvVar("DIR", mainDirPath+"/"+dirName);
85+
String dirPath = "$DIR";
86+
87+
List<File> classpath = new ArrayList<>();
88+
classpath.add(new File(dirPath+"/opensha-dev-all.jar"));
89+
parallelMPJWrite.setClasspath(classpath);
90+
91+
List<File> singleClasspath = new ArrayList<>(classpath);
92+
singleClasspath.add(new File("/project/scec_608/kmilner/git/opensha/lib/mpj-0.38.jar"));
93+
singleMPJWrite.setClasspath(singleClasspath);
94+
95+
// write the region
96+
File localReg = new File(localDir, "gridded_region.json");
97+
Feature.write(gridReg.toFeature(), localReg);
98+
99+
String resultsPath = dirPath+"/results";
100+
String regPath = dirPath+"/"+localReg.getName();
101+
102+
String solFilePath = "$DIR/"+solFileName;
103+
104+
List<String> setupLines = new ArrayList<>();
105+
setupLines.add("if [[ ! -e "+solFilePath+" ]];then");
106+
setupLines.add(" ln -s $MAIN_DIR/"+baseDirName+"/"+solFileName+" "+solFilePath);
107+
setupLines.add("fi");
108+
parallelMPJWrite.setCustomSetupLines(setupLines);
109+
singleMPJWrite.setCustomSetupLines(setupLines);
110+
111+
112+
for (IncludeBackgroundOption bgOp : bgOps) {
113+
int myNodes;
114+
JavaShellScriptWriter mpjWrite;
115+
String dispatchArgs;
116+
if (bgOp == IncludeBackgroundOption.INCLUDE && bgOps.length == 3) {
117+
// do single node
118+
myNodes = 1;
119+
mpjWrite = singleMPJWrite;
120+
dispatchArgs = MPJTaskCalculator.argumentBuilder().exactDispatch(gridReg.getNodeCount()).threads(remoteTotalThreads).build();
121+
} else {
122+
// do parallel
123+
myNodes = nodes;
124+
mpjWrite = parallelMPJWrite;
125+
126+
int maxDispatch;
127+
if (gridReg.getNodeCount() > 50000)
128+
maxDispatch = 1000;
129+
else if (gridReg.getNodeCount() > 10000)
130+
maxDispatch = 500;
131+
else if (gridReg.getNodeCount() > 5000)
132+
maxDispatch = remoteTotalThreads*5;
133+
else
134+
maxDispatch = remoteTotalThreads*3;
135+
dispatchArgs = MPJTaskCalculator.argumentBuilder().minDispatch(remoteTotalThreads)
136+
.maxDispatch(maxDispatch).threads(remoteTotalThreads).build();
137+
}
138+
139+
String argz = "--input-file "+dirPath+"/"+solFileName;
140+
argz += " --output-dir "+resultsPath;
141+
argz += " --output-file "+resultsPath+"_hazard_"+bgOp.name()+".zip";
142+
argz += " --region "+regPath;
143+
if (noMFDs)
144+
argz += " --no-mfds";
145+
argz += " --gridded-seis "+bgOp.name();
146+
argz += " "+dispatchArgs;
147+
148+
File jobFile = new File(localDir, "batch_hazard_"+bgOp.name()+".slurm");
149+
150+
List<String> script = mpjWrite.buildScript(MPJ_SingleSolHazardCalc.class.getName(), argz);
151+
152+
System.out.println("Writing "+jobFile.getAbsolutePath());
153+
154+
pbsWrite.writeScript(jobFile, script, mins, myNodes, remoteTotalThreads, queue);
155+
}
156+
}
157+
158+
}

src/main/java/scratch/kevin/nshm23/MPJ_LogicTreeInversionRunnerScriptWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ else if (mpjWrite instanceof FastMPJShellScriptWriter)
887887
// true mean job
888888
argz = resultsPath+".zip true_mean_solution.zip";
889889
if (griddedBAName != null)
890-
argz += dirPath+"/"+griddedBAName;
890+
argz += " "+dirPath+"/"+griddedBAName;
891891
script = javaWrite.buildScript(TrueMeanSolutionCreator.class.getName(), argz);
892892
pbsWrite.writeScript(new File(localDir, "true_mean_builder.slurm"), script, mins, 1, remoteTotalThreads, queue);
893893

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package scratch.kevin.nshm23.prvi;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.Comparator;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
10+
import org.opensha.commons.geo.json.Feature;
11+
import org.opensha.commons.geo.json.FeatureCollection;
12+
import org.opensha.commons.geo.json.FeatureProperties;
13+
import org.opensha.sha.faultSurface.GeoJSONFaultSection;
14+
15+
import com.google.common.base.Preconditions;
16+
17+
public class InitialDefModConvert {
18+
19+
public static void main(String[] args) throws IOException {
20+
File inputFile = new File("/home/kevin/OpenSHA/nshm23/prvi/2024_02-initial_dm/NSHM2025_GeoDefModel_PRVI.geojson");
21+
File outputFile = new File("/home/kevin/OpenSHA/nshm23/prvi/2024_02-initial_dm/NSHM2025_GeoDefModel_PRVI_mod.geojson");
22+
List<Feature> features = new ArrayList<>(FeatureCollection.read(inputFile).features);
23+
24+
features.sort(new Comparator<Feature>() {
25+
26+
@Override
27+
public int compare(Feature o1, Feature o2) {
28+
String name1 = o1.properties.getString("FaultID");
29+
String name2 = o2.properties.getString("FaultID");
30+
Preconditions.checkState(name1.startsWith("FS") || name1.startsWith("FP"));
31+
Preconditions.checkState(name2.startsWith("FS") || name2.startsWith("FP"));
32+
if (name1.startsWith("FS") && name2.startsWith("FS")) {
33+
int id1 = Integer.parseInt(name1.substring(2));
34+
int id2 = Integer.parseInt(name2.substring(2));
35+
return Integer.compare(id1, id2);
36+
} else if (name1.startsWith("FP") && name2.startsWith("FP")) {
37+
int id1 = Integer.parseInt(name1.substring(2));
38+
int id2 = Integer.parseInt(name2.substring(2));
39+
return Integer.compare(id1, id2);
40+
} else if (name1.startsWith("FS")) {
41+
return -1;
42+
} else {
43+
return 1;
44+
}
45+
}
46+
});
47+
48+
HashSet<String> prevNames = new HashSet<>();
49+
50+
for (int i=0; i<features.size(); i++) {
51+
Feature feature = features.get(i);
52+
feature.properties.remove("id");
53+
54+
int id = i+1;
55+
feature = Feature.getWithID(feature, id);
56+
57+
FeatureProperties props = feature.properties;
58+
59+
String name = props.getString("Fault");
60+
props.remove(name);
61+
props.set(GeoJSONFaultSection.FAULT_NAME, name);
62+
63+
Preconditions.checkState(!prevNames.contains(name), "Duplicate name: %s", name);
64+
prevNames.add(name);
65+
66+
props.set("FaultID", id);
67+
68+
double upDepth = props.getDouble("UpDep", Double.NaN);
69+
props.remove("UpDep");
70+
props.set(GeoJSONFaultSection.UPPER_DEPTH, upDepth);
71+
72+
double lowDepth = props.getDouble("LowerDep", Double.NaN);
73+
props.remove("LowerDep");
74+
props.set(GeoJSONFaultSection.LOW_DEPTH, lowDepth);
75+
76+
double dip = props.getDouble("FaultDip", Double.NaN);
77+
props.remove("FaultDip");
78+
props.set(GeoJSONFaultSection.DIP, dip);
79+
80+
props.remove("DipDir");
81+
82+
double slip = props.getDouble("PrefSlpRat", Double.NaN);
83+
props.remove("PrefSlpRat");
84+
props.set(GeoJSONFaultSection.SLIP_RATE, slip);
85+
86+
features.set(i, feature);
87+
}
88+
89+
FeatureCollection.write(new FeatureCollection(features), outputFile);
90+
}
91+
92+
}

src/main/java/scratch/kevin/ucerf3/PureScratch.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
import org.opensha.commons.util.IDPairing;
112112
import org.opensha.commons.util.MarkdownUtils;
113113
import org.opensha.commons.util.cpt.CPT;
114+
import org.opensha.commons.util.cpt.CPTVal;
114115
import org.opensha.commons.util.modules.ModuleArchive;
115116
import org.opensha.commons.util.modules.OpenSHA_Module;
116117
import org.opensha.sha.calc.HazardCurveCalculator;
@@ -5374,12 +5375,32 @@ private static void test277() throws IOException {
53745375
System.out.println("Grid memory:\t"+gridMB+" MB = "+(float)gridGB+" GB");
53755376
}
53765377

5378+
private static void test278() throws IOException {
5379+
Region reg = NSHM23_RegionLoader.loadFullConterminousWUS();
5380+
GriddedRegion gridReg = new GriddedRegion(reg, 0.1, GriddedRegion.ANCHOR_0_0);
5381+
System.out.println(gridReg.getNodeCount()+" nodes");
5382+
}
5383+
5384+
private static void test279() throws IOException {
5385+
CPT cpt = new CPT();
5386+
5387+
cpt.add(new CPTVal(-1.5f, Color.BLUE, -0.5f, Color.BLUE));
5388+
cpt.add(new CPTVal(-0.5f, Color.WHITE, 0.5f, Color.WHITE));
5389+
cpt.add(new CPTVal(0.5f, Color.RED, 1f, Color.RED));
5390+
5391+
cpt.setNanColor(Color.GRAY);
5392+
cpt.setBelowMinColor(Color.GRAY);
5393+
cpt.setAboveMaxColor(Color.GRAY);
5394+
5395+
cpt.writeCPTFile(new File("/tmp/for_luis.cpt"));
5396+
}
5397+
53775398
/**
53785399
* @param args
53795400
* @throws Exception
53805401
*/
53815402
public static void main(String[] args) throws Exception {
5382-
test277();
5403+
test279();
53835404
}
53845405

53855406
}

0 commit comments

Comments
 (0)