Skip to content

Commit

Permalink
[347] refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
voj committed Dec 4, 2024
1 parent e2c0c74 commit 90445b4
Show file tree
Hide file tree
Showing 29 changed files with 364 additions and 939 deletions.
Original file line number Diff line number Diff line change
@@ -1,104 +1,50 @@
package nz.cri.gns.NZSHM22.opensha.inversion;

import static nz.cri.gns.NZSHM22.util.TestHelpers.createRupSet;
import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.util.List;

import nz.cri.gns.NZSHM22.opensha.calc.SimplifiedScalingRelationship;
import nz.cri.gns.NZSHM22.opensha.enumTreeBranches.NZSHM22_FaultModels;
import nz.cri.gns.NZSHM22.opensha.enumTreeBranches.NZSHM22_LogicTreeBranch;
import nz.cri.gns.NZSHM22.opensha.enumTreeBranches.NZSHM22_ScalingRelationshipNode;
import nz.cri.gns.NZSHM22.opensha.faults.FaultSectionList;
import nz.cri.gns.NZSHM22.util.TestHelpers;
import org.dom4j.DocumentException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import org.opensha.commons.util.io.archive.ArchiveInput;
import org.opensha.sha.earthquake.faultSysSolution.FaultSystemRupSet;
import org.opensha.sha.earthquake.faultSysSolution.FaultSystemSolution;
import org.opensha.sha.earthquake.faultSysSolution.RupSetScalingRelationship;
import org.opensha.sha.earthquake.faultSysSolution.modules.ClusterRuptures;
import scratch.UCERF3.enumTreeBranches.ScalingRelationships;

public class NZSHM22_InversionRunner_IntegrationTest {

private static URL alpineVernonRupturesUrl;
private static File tempFolder;

@BeforeClass
public static void setUp() throws IOException {
alpineVernonRupturesUrl = Thread.currentThread().getContextClassLoader().getResource("AlpineVernonRuptureSet.zip");
tempFolder = Files.createTempDirectory("_testNew").toFile();
}

@AfterClass
public static void tearDown() throws IOException {
File[] files = tempFolder.listFiles();
if (files != null) {
for (File f : files) {
if (f != null) {
f.delete();
}
}
}
try {
Files.deleteIfExists(tempFolder.toPath());
} catch (DirectoryNotEmptyException x) {
// if there are rupture sets in the temp folder, it might not be possible to delete them right away
x.printStackTrace();
}
}

public static FaultSystemRupSet createRupSet(NZSHM22_FaultModels faultModel, RupSetScalingRelationship scalingRelationship, List<List<Integer>> sectionForRups) throws DocumentException, IOException {
FaultSectionList sections = new FaultSectionList();
faultModel.fetchFaultSections(sections);
// simulate subsections exactly the same size as the parents
sections.forEach(section -> {
section.setParentSectionId(section.getSectionId());
section.setParentSectionName(section.getSectionName());
});

NZSHM22_LogicTreeBranch branch = new NZSHM22_LogicTreeBranch();
branch.setValue(faultModel);
branch.setValue(new NZSHM22_ScalingRelationshipNode(scalingRelationship));

return FaultSystemRupSet.builder(sections, sectionForRups)
.forScalingRelationship(scalingRelationship)
.addModule(branch)
.build();
}


public NZSHM22_AbstractInversionRunner buildRunner() throws URISyntaxException, DocumentException, IOException {
SimplifiedScalingRelationship scaling = (SimplifiedScalingRelationship) NZSHM22_ScalingRelationshipNode.createRelationShip("SimplifiedScalingRelationship");
scaling.setupCrustal(4, 4.1);
public ArchiveInput ruptureSet() throws DocumentException, IOException {
FaultSystemRupSet rupSet = createRupSet(
NZSHM22_FaultModels.CFM_1_0A_DOM_ALL,
scaling,
ScalingRelationships.SHAW_2009_MOD,
List.of(List.of(0, 1),
List.of(5, 6, 7, 8, 9)));
File rupSetFile = new File(tempFolder, "buildRunnerRupSet.zip");
rupSet.write(rupSetFile);
return TestHelpers.archiveInput(rupSet);
}

public NZSHM22_AbstractInversionRunner buildRunner() throws DocumentException, IOException {
return new NZSHM22_CrustalInversionRunner()
.setGutenbergRichterMFD(4.0, 0.81, 0.91, 1.05, 7.85)
.setInversionSeconds(1)
.setSelectionInterval(1)
.setScalingRelationship(scaling, false)
.setRuptureSetFile(rupSetFile)
.setScalingRelationship(ScalingRelationships.SHAW_2009_MOD, false)
.setRuptureSetArchiveInput(ruptureSet())
//.setGutenbergRichterMFDWeights(100.0, 1000.0)
//.setSlipRateConstraint("BOTH", 1000, 1000)
.setSlipRateUncertaintyConstraint(1000, 2)
.setUncertaintyWeightedMFDWeights(0.5, 0.5, 0.5);
}

@Test
public void testRunExclusion() throws URISyntaxException, DocumentException, IOException {
public void testRunExclusion() throws DocumentException, IOException {
NZSHM22_AbstractInversionRunner runner = buildRunner().setExcludeRupturesBelowMinMag(true);
FaultSystemSolution solution = runner.runInversion();

Expand All @@ -117,9 +63,9 @@ public void testRunExclusion() throws URISyntaxException, DocumentException, IOE
* @throws URISyntaxException
*/
@Test
public void testLoadRuptureSetForInversion() throws IOException, DocumentException, URISyntaxException {
NZSHM22_InversionFaultSystemRuptSet ruptureSet = NZSHM22_InversionFaultSystemRuptSet.loadCrustalRuptureSet(new File(alpineVernonRupturesUrl.toURI()), NZSHM22_LogicTreeBranch.crustalInversion());
assertEquals(3101, ruptureSet.getModule(ClusterRuptures.class).getAll().size());
public void testLoadRuptureSetForInversion() throws IOException, DocumentException {
NZSHM22_InversionFaultSystemRuptSet ruptureSet = NZSHM22_InversionFaultSystemRuptSet.loadCrustalRuptureSet(ruptureSet(), NZSHM22_LogicTreeBranch.crustalInversion());
assertEquals(2, ruptureSet.getModule(ClusterRuptures.class).getAll().size());
}

//TODO we should use junit>=4.13 and assertThrows instead
Expand Down
Original file line number Diff line number Diff line change
@@ -1,84 +1,62 @@
package nz.cri.gns.NZSHM22.opensha.ruptures;

import com.google.common.collect.Sets;
import nz.cri.gns.NZSHM22.opensha.enumTreeBranches.NZSHM22_FaultModels;
import org.dom4j.DocumentException;
import org.junit.Test;
import org.opensha.sha.earthquake.faultSysSolution.FaultSystemRupSet;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

import static org.junit.Assert.*;

public class NZSHM22_CoulombRuptureSetBuilder_IntegrationTest {

public boolean hasRuptureWithFaults(Set<Integer> faults, FaultSystemRupSet rupSet) {

for (List<Integer> sections : rupSet.getSectionIndicesForAllRups()) {
/**
* Turns all ruptures into sets of parent ids
* @param rupSet
* @return
*/
public static List<Set<Integer>> toParentIds(FaultSystemRupSet rupSet) {
List<Set<Integer>> parentIds = new ArrayList<>();
for(List<Integer> rupture:rupSet.getSectionIndicesForAllRups()) {
Set<Integer> parents = new HashSet<>();
for (int s : sections) {
parents.add(rupSet.getFaultSectionData(s).getParentSectionId());
}
if (parents.equals(faults)) {
return true;
for(Integer sectionId: rupture) {
parents.add(rupSet.getFaultSectionData(sectionId).getParentSectionId());
}
parentIds.add(parents);
}
return false;
}

@Test
public void testRuptureSetSlipRateMethods() throws IOException, DocumentException {

FaultSystemRupSet ruptureSet =
new NZSHM22_CoulombRuptureSetBuilder()
.setFaultModelFile(new File("src/integration/resources/KAIK2016.xml"))
.buildRuptureSet();

assertEquals(1192, ruptureSet.getNumRuptures());
assertEquals(68, ruptureSet.getSlipRateForAllSections().length);
assertEquals(68, ruptureSet.getSlipRateStdDevForAllSections().length);

assertEquals("HopeConwayOS", ruptureSet.getFaultSectionData(11).getParentSectionName());

assertEquals(new Float(0.0214), new Float(ruptureSet.getSlipRateForSection(11)));
assertEquals(new Float(2.3), new Float(ruptureSet.getFaultSectionData(11).getOrigSlipRateStdDev()));
assertEquals(new Float(0.0023), new Float(ruptureSet.getSlipRateStdDevForSection(11)));
return parentIds;
}

@Test
public void testCantBuildKaikoura2016() throws IOException, DocumentException {

Set<Integer> kaikouraFaults = Sets.newHashSet(95, 132, 136, 149, 162, 178, 189, 245, 310, 387, 400);

FaultSystemRupSet ruptureSet =
new NZSHM22_CoulombRuptureSetBuilder()
.setFaultModelFile(new File("src/integration/resources/KAIK2016.xml"))
.buildRuptureSet();

//FaultSystemIO.writeRupSet(ruptureSet, new File("/tmp/testCantBuildKaikoura2016.zip"));
assertFalse(hasRuptureWithFaults(kaikouraFaults, ruptureSet));
public Set<Integer> getMostParents(FaultSystemRupSet rupSet){
List<Set<Integer>> parents = toParentIds(rupSet);
parents.sort(Comparator.comparing(Set::size));
return parents.get(parents.size()-1);
}

@Test
public void testAlpineVernon() throws IOException, DocumentException {
Set<Integer> faults = Sets.newHashSet( 23, 24, 130, 50, 48, 46, 585);

Set<Integer> faults = Set.of(35, 123, 30, 31);
FaultSystemRupSet ruptureSet =
new NZSHM22_CoulombRuptureSetBuilder()
.setFaultModelFile(new File("src/integration/resources/alpine-vernon.xml"))
.setFaultModel(NZSHM22_FaultModels.CFM_1_0A_DOM_SANSTVZ)
.setFaultFilter(faults)
.buildRuptureSet();

//FaultSystemIO.writeRupSet(ruptureSet, new File("./tmp/testAlpineVernon.zip"));

assertEquals(2178, ruptureSet.getNumRuptures());
assertTrue(hasRuptureWithFaults(faults, ruptureSet));
assertEquals(87, ruptureSet.getNumRuptures());

assertEquals(86, ruptureSet.getSlipRateForAllSections().length);
assertEquals(86, ruptureSet.getSlipRateStdDevForAllSections().length);
}
// we can build a rupture that has at least one of each fault
assertEquals(faults, getMostParents(ruptureSet));

assertEquals(18, ruptureSet.getNumSections());
assertEquals(18, ruptureSet.getSlipRateForAllSections().length);
assertEquals(18, ruptureSet.getSlipRateStdDevForAllSections().length);

assertEquals("Fowlers", ruptureSet.getFaultSectionData(11).getParentSectionName());
assertEquals(5.0E-4, ruptureSet.getSlipRateForSection(11), 0.0000001);
assertEquals(0.35,ruptureSet.getFaultSectionData(11).getOrigSlipRateStdDev(), 0.0000001);
assertEquals(3.5E-4, ruptureSet.getSlipRateStdDevForSection(11),0.0000001);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;

import nz.cri.gns.NZSHM22.opensha.enumTreeBranches.NZSHM22_FaultModels;
import nz.cri.gns.NZSHM22.opensha.util.Parameters;
import org.dom4j.DocumentException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.opensha.sha.earthquake.faultSysSolution.FaultSystemSolution;
import scratch.UCERF3.enumTreeBranches.ScalingRelationships;


public class NZSHM22_ReportPageGen_IntegrationTest {

private static URL alpineVernonInversionSolutionUrl;
private static File tempFolder;

@BeforeClass
public static void setUp() throws IOException {
alpineVernonInversionSolutionUrl = Thread.currentThread().getContextClassLoader().getResource("AlpineVernonInversionSolution.zip");
tempFolder = Files.createTempDirectory("_NZSHM22_RupSetDiagnosticsReport").toFile();
System.setProperty("java.awt.headless", "true");
}
Expand All @@ -44,10 +46,21 @@ public static void tearDown() throws IOException {
}

@Test
public void testRunReportForInversionSolution() throws IOException {
public void testRunReportForInversionSolution() throws IOException, DocumentException {
System.out.println("hello");
try {
Parameters param = Parameters.NZSHM22.INVERSION_CRUSTAL.getParameters();
}catch(Exception x) {
assertTrue(x.getMessage(), false);
}

FaultSystemSolution solution = null;

solution = TestHelpers.createCrustalSolution(
TestHelpers.makeRupSet(NZSHM22_FaultModels.CFM_1_0A_DOM_SANSTVZ, ScalingRelationships.SHAW_2009_MOD));
new NZSHM22_ReportPageGen().setOutputPath(tempFolder.toString())
.setName("test")
.setSolution(alpineVernonInversionSolutionUrl.getFile().toString())
.setSolution(solution)
.setPlotLevel(null)
.addPlot("SolMFDPlot")
.generatePage();
Expand Down
Loading

0 comments on commit 90445b4

Please sign in to comment.