Skip to content

Commit 529c7f3

Browse files
author
Kevin Milner
committed
toy inversion model for quantum tests
1 parent 2c9518c commit 529c7f3

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package scratch.kevin.quantum;
2+
3+
import java.io.BufferedOutputStream;
4+
import java.io.File;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.util.ArrayList;
8+
import java.util.BitSet;
9+
import java.util.List;
10+
11+
import org.opensha.commons.data.CSVFile;
12+
import org.opensha.commons.data.CSVWriter;
13+
import org.opensha.commons.logicTree.LogicTreeBranch;
14+
import org.opensha.commons.logicTree.LogicTreeNode;
15+
import org.opensha.sha.earthquake.faultSysSolution.FaultSystemRupSet;
16+
import org.opensha.sha.earthquake.faultSysSolution.FaultSystemSolution;
17+
import org.opensha.sha.earthquake.faultSysSolution.inversion.InversionConfiguration;
18+
import org.opensha.sha.earthquake.faultSysSolution.inversion.InversionInputGenerator;
19+
import org.opensha.sha.earthquake.faultSysSolution.inversion.InversionSolver;
20+
import org.opensha.sha.earthquake.faultSysSolution.inversion.constraints.ConstraintWeightingType;
21+
import org.opensha.sha.earthquake.faultSysSolution.inversion.constraints.InversionConstraint;
22+
import org.opensha.sha.earthquake.faultSysSolution.inversion.constraints.impl.SlipRateInversionConstraint;
23+
import org.opensha.sha.earthquake.faultSysSolution.inversion.sa.completion.CompletionCriteria;
24+
import org.opensha.sha.earthquake.faultSysSolution.inversion.sa.completion.IterationCompletionCriteria;
25+
import org.opensha.sha.earthquake.faultSysSolution.modules.ConnectivityClusters;
26+
import org.opensha.sha.earthquake.faultSysSolution.modules.NamedFaults;
27+
import org.opensha.sha.earthquake.faultSysSolution.modules.RuptureSubSetMappings;
28+
import org.opensha.sha.earthquake.faultSysSolution.ruptures.util.ConnectivityCluster;
29+
import org.opensha.sha.earthquake.faultSysSolution.util.FaultSectionUtils;
30+
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.NSHM23_InvConfigFactory;
31+
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_DeformationModels;
32+
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_LogicTreeBranch;
33+
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_SegmentationModels;
34+
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.targetMFDs.estimators.GRParticRateEstimator;
35+
import org.opensha.sha.faultSurface.FaultSection;
36+
37+
import com.google.common.base.Preconditions;
38+
39+
public class ToyInversionModelBuilder {
40+
41+
public static void main(String[] args) throws IOException {
42+
boolean rebuildRupSet = false;
43+
44+
File outputDir = new File("/home/kevin/markdown/inversions/2024_10_15-quantum_test_problem");
45+
Preconditions.checkState(outputDir.exists() || outputDir.mkdir());
46+
47+
File rsFile = new File(outputDir, "rup_set.zip");
48+
FaultSystemRupSet rupSet;
49+
if (rsFile.exists() && !rebuildRupSet) {
50+
rupSet = FaultSystemRupSet.load(rsFile);
51+
} else {
52+
NSHM23_InvConfigFactory factory = new NSHM23_InvConfigFactory();
53+
factory.setCacheDir(new File("/home/kevin/OpenSHA/nshm23/rup_sets/cache"));
54+
LogicTreeBranch<LogicTreeNode> branch = NSHM23_LogicTreeBranch.DEFAULT_ON_FAULT.copy();
55+
branch.setValue(NSHM23_DeformationModels.GEOLOGIC);
56+
rupSet = factory.buildRuptureSet(branch, 32);
57+
System.out.println("Building connectivity clusters");
58+
ConnectivityClusters clusters = ConnectivityClusters.build(rupSet);
59+
60+
int targetParentID = FaultSectionUtils.findParentSectionID(rupSet.getFaultSectionDataList(), "Likely");
61+
FaultSection targetSect = null;
62+
for (FaultSection sect : rupSet.getFaultSectionDataList()) {
63+
if (sect.getParentSectionId() == targetParentID) {
64+
targetSect = sect;
65+
break;
66+
}
67+
}
68+
Preconditions.checkNotNull(targetSect);
69+
ConnectivityCluster cluster = null;
70+
for (ConnectivityCluster testCluster : clusters) {
71+
if (testCluster.containsSect(targetSect)) {
72+
cluster = testCluster;
73+
break;
74+
}
75+
}
76+
77+
System.out.println("Cluster for "+targetSect.getSectionName()+" has "+cluster.getNumRuptures()
78+
+" ruptures on "+cluster.getNumSections()+" sub-sections");
79+
80+
rupSet = rupSet.getForSectionSubSet(cluster.getSectIDs());
81+
rupSet.removeModuleInstances(NamedFaults.class);
82+
rupSet.write(rsFile);
83+
}
84+
85+
// write out a-priori connectivity estimates
86+
GRParticRateEstimator estimator = new GRParticRateEstimator(rupSet, 0.5, NSHM23_SegmentationModels.MID.getModel(rupSet, null));
87+
double[][] estCoeffs = calcConnCoeffs(rupSet, estimator.estimateRuptureRates());
88+
writeCoeffCSV(estCoeffs, new File(outputDir, "conn_coeffs_a_priori.csv"));
89+
90+
List<InversionConstraint> constraints = new ArrayList<>();
91+
constraints.add(new SlipRateInversionConstraint(1d, ConstraintWeightingType.NORMALIZED, rupSet));
92+
93+
CompletionCriteria completion = new IterationCompletionCriteria(1000000l);
94+
// CompletionCriteria completion = new IterationCompletionCriteria(1000l);
95+
96+
InversionConfiguration config = InversionConfiguration.builder(constraints, completion)
97+
// .avgThreads(4, new IterationCompletionCriteria(10000l))
98+
// .threads(16).subCompletion(new IterationCompletionCriteria(1000l))
99+
.build();
100+
InversionInputGenerator inputGen = new InversionInputGenerator(rupSet, config);
101+
inputGen.generateInputs(true);
102+
103+
InversionSolver.Default solver = new InversionSolver.Default();
104+
FaultSystemSolution sol = solver.run(rupSet, config, inputGen, null);
105+
106+
sol.write(new File(outputDir, "solution.zip"));
107+
108+
inputGen.writeArchive(new File(outputDir, "constraints.zip"), sol.getRateForAllRups(), false);
109+
110+
double[][] solCoeffs = calcConnCoeffs(rupSet, sol.getRateForAllRups());
111+
writeCoeffCSV(solCoeffs, new File(outputDir, "conn_coeffs_sol.csv"));
112+
}
113+
114+
public static double[][] calcConnCoeffs(FaultSystemRupSet rupSet, double[] rates) {
115+
int numSects = rupSet.getNumSections();
116+
int numRups = rupSet.getNumRuptures();
117+
Preconditions.checkState(numRups == rates.length);
118+
double[][] coeffs = new double[numSects][numSects];
119+
BitSet[] sectRups = new BitSet[numSects];
120+
for (int s=0; s<numSects; s++) {
121+
sectRups[s] = new BitSet(numRups);
122+
for (int r : rupSet.getRupturesForSection(s))
123+
sectRups[s].set(r);
124+
}
125+
126+
for (int s1=0; s1<numSects; s1++) {
127+
for (int s2=0; s2<numSects; s2++) {
128+
if (s1 == s2) {
129+
coeffs[s1][s2] = 1d;
130+
} else {
131+
double sectParticRate = 0d;
132+
double coruptureRate = 0d;
133+
for (int r=0; r<numRups; r++) {
134+
if (sectRups[s1].get(r)) {
135+
sectParticRate += rates[r];
136+
if (sectRups[s2].get(r))
137+
coruptureRate += rates[r];
138+
}
139+
}
140+
coeffs[s1][s2] = coruptureRate/sectParticRate;
141+
}
142+
}
143+
}
144+
145+
return coeffs;
146+
}
147+
148+
private static void writeCoeffCSV(double[][] coeffs, File outputFile) throws IOException {
149+
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
150+
CSVWriter csv = new CSVWriter(out, true);
151+
152+
csv.write(List.of("Primary Section Index", "Connected Section Index", "Connection Coefficient"));
153+
for (int s1=0; s1<coeffs.length; s1++)
154+
for (int s2=0; s2<coeffs.length; s2++)
155+
csv.write(List.of(s1+"", s2+"", (float)coeffs[s1][s2]+""));
156+
157+
csv.flush();
158+
out.close();
159+
}
160+
161+
}

0 commit comments

Comments
 (0)