Skip to content

Commit 3e07936

Browse files
committed
Adjoint test is almost working
1 parent f711a13 commit 3e07936

File tree

3 files changed

+52
-107
lines changed

3 files changed

+52
-107
lines changed
Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,24 @@
11
"""
2-
Test for the adjoint problem for 2D acoustics
2+
Test for the adjoint problem for 1D acoustics
33
"""
44

5-
import sys
6-
import unittest
5+
from pathlib import Path
6+
import pytest
77

88
import clawpack.amrclaw.test as test
9-
import clawpack.amrclaw.data as data
109

11-
class Acoustics1DAdjointTest(test.AMRClawRegressionTest):
10+
def test_acoustics_1d_adjoint(tmp_path: Path, save: bool):
11+
"""Acoustics 1D adjoint test"""
1212

13-
def runTest(self, save=False):
13+
ctr = test.AMRClawTestRunner(tmp_path)
14+
ctr.set_data()
15+
ctr.write_data()
16+
ctr.build_executable()
17+
ctr.run_code()
1418

15-
# Write out data files
16-
self.load_rundata()
17-
18-
# self.rundata.clawdata.num_output_times = 10
19-
# self.rundata.clawdata.tfinal = 1.0
19+
ctr.check_gauge(gauge_id=0)
20+
ctr.check_gauge(gauge_id=1)
2021

21-
# self.rundata.clawdata.use_fwaves = False
2222

23-
self.write_rundata_objects()
24-
25-
self.run_code()
26-
27-
# Perform Tests
28-
self.check_gauges(save=save, gauge_id=0)
29-
self.check_gauges(save=save, gauge_id=1)
30-
31-
self.success = True
32-
33-
34-
if __name__=="__main__":
35-
if len(sys.argv) > 1:
36-
if bool(sys.argv[1]):
37-
# Fake the setup and save out output
38-
test = Acoustics1DAdjointTest()
39-
try:
40-
test.setUp()
41-
test.runTest(save=True)
42-
finally:
43-
test.tearDown()
44-
sys.exit(0)
45-
unittest.main()
23+
if __name__ == "__main__":
24+
pytest.main([__file__])

examples/acoustics_1d_adjoint/test_acoustics_1d_adjoint_forward.py

Lines changed: 35 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,41 @@
33
"""
44

55
from pathlib import Path
6-
import sys
7-
import shutil
8-
import unittest
6+
import os
7+
import pytest
98

109
import clawpack.amrclaw.test as test
11-
import clawpack.clawutil.runclaw
1210

13-
from adjoint.test_acoustics_1d_adjoint import Acoustics1DAdjointTest
14-
15-
16-
class Acoustics1DAdjointForwardTest(test.AMRClawRegressionTest):
17-
r"""Basic test for a 1D acoustics adjoint-flagging forward problem test case"""
18-
19-
20-
def runTest(self, save=False):
21-
22-
# Run adjoint problem
23-
try:
24-
adjoint_run = Acoustics1DAdjointTest()
25-
adjoint_run.setUp()
26-
adjoint_run.runTest()
27-
28-
# Copy output to local directory
29-
adjoint_output = Path(self.temp_path) / "_adjoint_output"
30-
31-
if Path(adjoint_output).exists():
32-
shutil.rmtree(adjoint_output)
33-
shutil.copytree(adjoint_run.temp_path, adjoint_output)
34-
finally:
35-
adjoint_run.tearDown()
36-
37-
# Write out data files
38-
self.load_rundata()
39-
40-
# self.rundata.clawdata.num_output_times = 1
41-
# self.rundata.clawdata.tfinal = 0.5
42-
43-
# self.rundata.amrdata.flag2refine_tol = 0.05
44-
45-
# self.rundata.gaugedata.gauges = []
46-
# self.rundata.gaugedata.gauges.append([0, -1.0, 0., 1e9])
47-
# self.rundata.gaugedata.gauges.append([1, -2.5, 0., 1e9])
48-
# self.rundata.gaugedata.gauges.append([2, -1.75, 0., 1e9])
49-
50-
# Look for adjoint data
51-
self.rundata.adjointdata.adjoint_outdir = adjoint_output
52-
53-
self.write_rundata_objects()
54-
55-
# Run code
56-
self.run_code()
57-
58-
# Perform tests
59-
self.check_gauges(save=save, gauge_id=0)
60-
self.check_gauges(save=save, gauge_id=1)
61-
# self.check_gauges(save=save, gauge_id=2)
62-
63-
self.success = True
64-
65-
66-
67-
if __name__=="__main__":
68-
if len(sys.argv) > 1:
69-
if bool(sys.argv[1]):
70-
# Fake the setup and save out output
71-
test = Acoustics1DAdjointForwardTest()
72-
try:
73-
test.setUp()
74-
test.runTest(save=True)
75-
finally:
76-
test.tearDown()
77-
sys.exit(0)
78-
unittest.main()
11+
# import adjoint.test_acoustics_1d_adjoint
12+
13+
def test_acoustics_1d_adjoint_forward(tmp_path: Path, save: bool):
14+
"""Test for a 1D acoustics adjoint-flagging forward problem test case"""
15+
16+
ctr = test.AMRClawTestRunner(tmp_path, test_path=__file__)
17+
18+
# Run adjoint problem
19+
adjoint_output = ctr.temp_path / "_adjoint_output"
20+
21+
if not adjoint_output.exists():
22+
os.makedirs(adjoint_output)
23+
adjoint_ctr = test.AMRClawTestRunner(adjoint_output,
24+
test_path=ctr.test_path / "adjoint")
25+
adjoint_ctr.set_data(setrun_path=adjoint_ctr.test_path / "setrun.py")
26+
adjoint_ctr.write_data()
27+
adjoint_ctr.build_executable()
28+
adjoint_ctr.run_code()
29+
30+
# Write problem data
31+
ctr.set_data()
32+
ctr.rundata.adjointdata.adjoint_outdir = adjoint_output
33+
ctr.write_data()
34+
35+
ctr.build_executable()
36+
ctr.run_code()
37+
38+
ctr.check_gauge(gauge_id=0)
39+
ctr.check_gauge(gauge_id=1)
40+
41+
42+
if __name__ == "__main__":
43+
pytest.main([__file__])

src/python/amrclaw/test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from pathlib import Path
99
import os
10+
from typing import Optional
1011

1112
import clawpack.clawutil.test as test
1213

@@ -25,6 +26,6 @@
2526

2627
class AMRClawTestRunner(test.ClawpackTestRunner):
2728

28-
def __init__(self, path: Path):
29-
super(AMRClawTestRunner, self).__init__(path)
29+
def __init__(self, path: Path, test_path: Optional[Path]=None):
30+
super(AMRClawTestRunner, self).__init__(path, test_path=test_path)
3031
self.executable_name = 'xamr'

0 commit comments

Comments
 (0)