Skip to content

Commit bc357ac

Browse files
committed
EAL calculation demo
1 parent cf129ab commit bc357ac

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package scratch.kevin.risk;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.List;
6+
7+
import org.opensha.commons.data.CSVFile;
8+
import org.opensha.commons.data.Site;
9+
import org.opensha.commons.data.TimeSpan;
10+
import org.opensha.commons.param.Parameter;
11+
import org.opensha.commons.param.ParameterList;
12+
import org.opensha.sha.earthquake.ProbEqkRupture;
13+
import org.opensha.sha.earthquake.ProbEqkSource;
14+
import org.opensha.sha.earthquake.param.IncludeBackgroundOption;
15+
import org.opensha.sha.earthquake.param.IncludeBackgroundParam;
16+
import org.opensha.sha.earthquake.param.ProbabilityModelOptions;
17+
import org.opensha.sha.earthquake.param.ProbabilityModelParam;
18+
import org.opensha.sha.imr.AttenRelRef;
19+
import org.opensha.sha.imr.ScalarIMR;
20+
import org.opensha.sra.gui.portfolioeal.Asset;
21+
import org.opensha.sra.gui.portfolioeal.PortfolioParser;
22+
import org.opensha.sra.vulnerability.Vulnerability;
23+
import org.opensha.sra.vulnerability.VulnerabilityFetcher;
24+
import org.opensha.sra.vulnerability.models.SimpleVulnerability;
25+
26+
import com.google.common.base.Preconditions;
27+
28+
import scratch.UCERF3.erf.mean.MeanUCERF3;
29+
import scratch.UCERF3.erf.mean.MeanUCERF3.Presets;
30+
31+
public class U3_EAL_ExampleCalc {
32+
33+
public static void main(String[] args) throws IOException {
34+
// this is a pretty weird setup; you instantiate the asset by first giving a comma separated list of parameters
35+
String assetParamNames = "AssetGroup,AssetID,AssetName,Lat,Lon,ValHi,ValLo,Value,VulnModel,Vs30";
36+
// then will pass the values in to the asset; here's a sample from one of Keith's files
37+
// the vulnerability model is in the 2nd to last column, and will be looked up
38+
String assetValues = "CEA 2019 100pct proxy portfolio,347,06001400100,37.859419,-122.230389,8106,1054,1054,RM1L-m-RES1-DF,627.7137";
39+
Asset asset = new Asset(assetParamNames);
40+
41+
// you'll notice that the 2nd to last column contained the vulnerability model name
42+
// that name is matched against a list of vulnerability functions; there is currently no way to just set your
43+
// own custom vulnerability. This is a pretty bad design that really only makes sense in a portfolio context
44+
// where you're parsing an input file.
45+
46+
// Vulnerability models can be loaded in Keith's 'VUL06' file format. Here's how you could do that
47+
// VulnerabilityFetcher.getVulnerabilities(new File("/path/to/vul06.csv"));
48+
// or you can put in your own custom implementation like this. we need to make this better...
49+
// Vulnerability vuln = new SimpleVulnerability(name, shortName, imType, imLevels, mfdVals, covVals);
50+
// VulnerabilityFetcher.getVulnerabilities().put("MyVulnName", vuln);
51+
52+
// output file
53+
File outputCSV = new File("/tmp/eal_demo.csv");
54+
55+
List<String> assetArrayList = PortfolioParser.parseLine(assetValues);
56+
String[] assetValueList = assetArrayList.toArray(new String[0]);
57+
ParameterList assetParams = asset.getParameterList();
58+
Preconditions.checkState(assetParams.size() == assetValueList.length,
59+
"Have %s params but %s values", assetParams.size(), assetValueList.length);
60+
System.out.println("Asset parameters (before parsing)");
61+
for (int p=0; p<assetValueList.length; p++)
62+
System.out.println(assetParams.getParameter(p).getName()+":\t"+assetValueList[p]);
63+
asset.setAssetParameters(assetValueList);
64+
65+
// now we need an ERF, lets use UCERF3 (FM3.1, excluding background seismicity)
66+
MeanUCERF3 erf = new MeanUCERF3();
67+
erf.setPreset(Presets.FM3_1_BRANCH_AVG);
68+
erf.setParameter(IncludeBackgroundParam.NAME, IncludeBackgroundOption.EXCLUDE);
69+
// set to poisson, 1 year
70+
erf.setParameter(ProbabilityModelParam.NAME, ProbabilityModelOptions.POISSON);
71+
erf.getTimeSpan().setDuration(1d);
72+
// build the forecast; this will download a data file the first time
73+
erf.updateForecast();
74+
75+
// max source-site dist in KM
76+
double maxDistance = 200d;
77+
78+
// also need a GMM, let's use ASK 2014
79+
ScalarIMR gmm = AttenRelRef.ASK_2014.get();
80+
gmm.setParamDefaults();
81+
82+
// this exists in order to supply all required GMM paremeters, e.g. basin depth, that aren't included in the asset
83+
// well set those as default. the location and Vs30 value will be overridden automatically when we do the calculation
84+
Site site = new Site();
85+
for (Parameter<?> param : gmm.getSiteParams())
86+
site.addParameter((Parameter<?>) param.clone());
87+
88+
// conditional losses for each rupture
89+
double[][] rupConditionalLosses = asset.calculateExpectedLossPerRup(gmm, maxDistance, null, site, erf, null);
90+
double eal = asset.getAssetEAL();
91+
92+
System.out.println("EAL: "+eal);
93+
94+
// lets write out the individual losses to a CSV File
95+
CSVFile<String> csv = new CSVFile<>(false);
96+
97+
csv.addLine("Source ID", "Rupture ID", "Magnitude", "Rate", "Conditional Loss");
98+
for (int sourceID=0; sourceID<erf.getNumSources(); sourceID++) {
99+
if (rupConditionalLosses[sourceID] == null)
100+
// too far away
101+
continue;
102+
ProbEqkSource source = erf.getSource(sourceID);
103+
for (int rupID=0; rupID<source.getNumRuptures(); rupID++) {
104+
ProbEqkRupture rup = source.getRupture(rupID);
105+
double rate = rup.getMeanAnnualRate(erf.getTimeSpan().getDuration());
106+
csv.addLine(sourceID+"", rupID+"", (float)rup.getMag()+"", rate+"", rupConditionalLosses[sourceID][rupID]+"");
107+
}
108+
}
109+
110+
// add EAL at the bottom
111+
csv.addLine("");
112+
csv.addLine("EAL:", eal+"");
113+
114+
csv.writeToFile(outputCSV);
115+
}
116+
117+
}

0 commit comments

Comments
 (0)