|
| 1 | +import unittest |
| 2 | +import numpy as np |
| 3 | +import glob |
| 4 | +import os |
| 5 | +import time |
| 6 | +import shutil |
| 7 | +import subprocess as sp |
| 8 | + |
| 9 | +def cleanDir(dir): |
| 10 | + if os.path.exists('{}/pscond.dat'.format(dir)): |
| 11 | + os.remove('{}/pscond.dat'.format(dir)) |
| 12 | + if os.path.exists('{}/scond.dat'.format(dir)): |
| 13 | + os.remove('{}/scond.dat'.format(dir)) |
| 14 | + if os.path.exists('{}/run_magic.sh'.format(dir)): |
| 15 | + os.remove('{}/run_magic.sh'.format(dir)) |
| 16 | + if os.path.exists('{}/run_magic_mpi.sh'.format(dir)): |
| 17 | + os.remove('{}/run_magic_mpi.sh'.format(dir)) |
| 18 | + for f in glob.glob('{}/*_BIS'.format(dir)): |
| 19 | + os.remove(f) |
| 20 | + for f in glob.glob('{}/*.test'.format(dir)): |
| 21 | + os.remove(f) |
| 22 | + if os.path.exists('{}/stdout.out'.format(dir)): |
| 23 | + os.remove('{}/stdout.out'.format(dir)) |
| 24 | + for f in glob.glob('{}/*.pyc'.format(dir)): |
| 25 | + os.remove(f) |
| 26 | + if os.path.exists('{}/__pycache__'.format(dir)): |
| 27 | + shutil.rmtree('{}/__pycache__'.format(dir)) |
| 28 | + |
| 29 | + |
| 30 | +def get_ratios(): |
| 31 | + """ |
| 32 | + Get the ratio of the temperature to radial velocity at CMB. |
| 33 | + """ |
| 34 | + from magic import MagicGraph |
| 35 | + G = MagicGraph(tag='test') |
| 36 | + |
| 37 | + expr = ( 0.35 * (6/7) * np.sin(G.colatitude)**2 / |
| 38 | + ( (2/7) * (3 * np.cos(G.colatitude)**2 - 1) + |
| 39 | + (8/7) * np.sin(G.colatitude)**2) ) |
| 40 | + |
| 41 | + maxratio = np.zeros(G.ntheta) |
| 42 | + for i in range(G.ntheta): |
| 43 | + maxratio[i] = G.entropy[:,i,0].max()/G.vr[:,i,0].max() |
| 44 | + |
| 45 | + return expr, maxratio |
| 46 | + |
| 47 | +class TidesTest(unittest.TestCase): |
| 48 | + |
| 49 | + def __init__(self, testName, dir, execCmd='mpirun -n 8 ../tmp/magic.exe', |
| 50 | + precision=1e-8): |
| 51 | + super(TidesTest, self).__init__(testName) |
| 52 | + self.dir = dir |
| 53 | + self.precision = precision |
| 54 | + self.execCmd = execCmd |
| 55 | + self.startDir = os.getcwd() |
| 56 | + self.description = "Tides: radial velocity" |
| 57 | + |
| 58 | + def list2reason(self, exc_list): |
| 59 | + if exc_list and exc_list[-1][0] is self: |
| 60 | + return exc_list[-1][1] |
| 61 | + |
| 62 | + def setUp(self): |
| 63 | + # Cleaning when entering |
| 64 | + print('\nDirectory : {}'.format(self.dir)) |
| 65 | + print('Description : {}'.format(self.description)) |
| 66 | + self.startTime = time.time() |
| 67 | + cleanDir(self.dir) |
| 68 | + os.chdir(self.dir) |
| 69 | + cmd = '{} {}/input.nml'.format(self.execCmd, self.dir) |
| 70 | + sp.call(cmd, shell=True, stdout=open(os.devnull, 'wb'), |
| 71 | + stderr=open(os.devnull, 'wb')) |
| 72 | + |
| 73 | + def tearDown(self): |
| 74 | + # Cleaning when leaving |
| 75 | + os.chdir(self.startDir) |
| 76 | + cleanDir(self.dir) |
| 77 | + |
| 78 | + t = time.time()-self.startTime |
| 79 | + st = time.strftime("%M:%S", time.gmtime(t)) |
| 80 | + print('Time used : {}'.format(st)) |
| 81 | + |
| 82 | + if hasattr(self, '_outcome'): # python 3.4+ |
| 83 | + if hasattr(self._outcome, 'errors'): # python 3.4-3.10 |
| 84 | + result = self.defaultTestResult() |
| 85 | + self._feedErrorsToResult(result, self._outcome.errors) |
| 86 | + else: # python 3.11+ |
| 87 | + result = self._outcome.result |
| 88 | + else: # python 2.7-3.3 |
| 89 | + result = getattr(self, '_outcomeForDoCleanups', |
| 90 | + self._resultForDoCleanups) |
| 91 | + |
| 92 | + error = self.list2reason(result.errors) |
| 93 | + failure = self.list2reason(result.failures) |
| 94 | + ok = not error and not failure |
| 95 | + |
| 96 | + if ok: |
| 97 | + print('Validating results.. OK') |
| 98 | + else: |
| 99 | + if error: |
| 100 | + print('Validating results.. ERROR!') |
| 101 | + print('\n') |
| 102 | + print(result.errors[-1][-1]) |
| 103 | + if failure: |
| 104 | + print('Validating results.. FAIL!') |
| 105 | + print('\n') |
| 106 | + print(result.failures[-1][-1]) |
| 107 | + |
| 108 | + def outputFileDiff(self): |
| 109 | + datRef, datTmp = get_ratios() |
| 110 | + np.testing.assert_allclose(datRef, datTmp, rtol=self.precision, |
| 111 | + atol=1e-7) |
0 commit comments