Skip to content

Instantly share code, notes, and snippets.

@mandli
Created April 27, 2019 18:59
Show Gist options
  • Save mandli/3342ae02098e3a7c88351d60cdc1bd00 to your computer and use it in GitHub Desktop.
Save mandli/3342ae02098e3a7c88351d60cdc1bd00 to your computer and use it in GitHub Desktop.
Draft Adjoint Based Regression Tests Class
#!/usr/bin/env python
r"""chile2010_adjoint regression test for GeoClaw
To create new regression data use
`python regression_tests.py True`
"""
from __future__ import absolute_import
import os
import sys
import unittest
import numpy
import clawpack.geoclaw.test as test
import clawpack.geoclaw.topotools as topotools
thisfile = os.path.realpath(__file__)
testdir = os.path.split(thisfile)[0]
class Chile2010AdjointTest(test.GeoClawRegressionTest):
r"""Chile2010AdjointTest regression test for GeoClaw"""
def __init__(self, methodName="runTest"):
super(Chile2010AdjointTest, self).__init__(methodName=methodName)
# Add extra adjoint data
self.executable_name_adjoint = "xgeoclaw_adjoint"
self.rundata_adjoint = None
def setUp(self):
"""Modified to also create adjoint directory and exectuable"""
super(Chile2010AdjointTest, self).setUp()
# Make adjoint directory in temp path
os.mkdir(os.path.join(self.temp_path, "adjoint"))
# Build adjoint executable
self.build_adjoint_executable()
def build_adjoint_executable(self):
# Build adjoint executable
try:
subprocess.check_call("cd %s ; make adjoint" % self.test_path,
stdout=self.stdout,
stderr=self.stderr,
shell=True)
except subprocess.CalledProcessError as e:
self.tearDown()
raise e
shutil.move(os.path.join(self.test_path, self.executable_name_adjoint),
os.path.join(self.temp_path, "adjoint"))
def load_rundata(self):
"""Load both the forward and backward setrun files"""
# Load forward data in the usual way
super(Chile2010AdjointTest, self).load_rundata()
# Load adjoint data
if 'setrun_adjoint' in sys.modules:
del(sys.modules['setrun_adjoint'])
sys.path.insert(0, self.test_path)
import setrun_adjoint
self.rundata_adjoint = setrun_adjoint.setrun()
sys.path.pop(0)
# Make topography
import maketopo
maketopo.get_topo(False)
maketopo.make_dtopo(False)
def write_rundata_objects(self, path=None):
# Write out forward data
super(Chile2010AdjointTest, self).write_rundata_objects(path)
# Write out adjoint data
if path is None:
path = os.path.join(self.temp_path, "adjoint")
else:
path = os.path.join(path, "adjoint")
orig_path = os.getcwd()
os.chdir(path)
self.rundata_adjoint.write()
os.chdir(orig_path)
def runTest(self, save=False, indices=(2, 3)):
r"""Test chile2010_adjoint example
Note that this stub really only runs the code and performs no tests.
"""
# Write out data files
self.load_rundata()
self.write_rundata_objects()
# Run forward code
self.run_code()
# Run backward code
runclaw_cmd = " ".join((
"cd %s ;" % self.temp_path,
"python",
"$CLAW/clawutil/src/python/clawutil/runclaw.py",
self.executable_name_adjoint,
os.path.join(self.temp_path, "adjoint"),
"True",
"False",
os.path.join(self.temp_path, "adjoint")))
subprocess.check_call(runclaw_cmd, stdout=self.stdout,
stderr=self.stderr,
shell=True)
self.stdout.flush()
self.stderr.flush()
# Perform tests
self.check_gauges(save=save, gauge_id=1, indices=(2, 3))
self.success = True
if __name__=="__main__":
if len(sys.argv) > 1:
if bool(sys.argv[1]):
# Fake the setup and save out output
test = Chile2010AdjointTest()
try:
test.setUp()
test.runTest(save=True)
finally:
test.tearDown()
sys.exit(0)
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment