mirror of
https://github.com/wassname/simpeg.git
synced 2026-07-01 02:32:33 +08:00
d0efdb509c
This creates a testable framework so that we can easily test order of convergence on things like operators by just writing the guts of the code. Merged Seogi's code into this framework.
51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
import sys
|
|
sys.path.append('../../')
|
|
from SimPEG import TensorMesh
|
|
import numpy as np
|
|
import unittest
|
|
|
|
|
|
class OrderTest(unittest.TestCase):
|
|
"""Order test sets up the basics for testing order of decrease for a function on a mesh."""
|
|
|
|
name = "Order Test"
|
|
expectedOrder = 2
|
|
meshSizes = [4, 8, 16, 32]
|
|
|
|
def setupMesh(self, nc):
|
|
# Define the mesh
|
|
h1 = np.ones(nc)/nc
|
|
h2 = np.ones(nc)/nc
|
|
h3 = np.ones(nc)/nc
|
|
h = [h1, h2, h3]
|
|
self.M = TensorMesh(h)
|
|
|
|
def getError(self):
|
|
"""Overwrite this function with the guts of the test."""
|
|
return 1.
|
|
|
|
def orderTest(self):
|
|
order = []
|
|
err_old = 0.
|
|
nc_old = 0.
|
|
for ii, nc in enumerate(self.meshSizes):
|
|
self.setupMesh(nc)
|
|
err = self.getError()
|
|
if ii == 0:
|
|
print ''
|
|
print 'Testing order of: ' + self.name
|
|
print ' h | inf norm | ratio | order'
|
|
print '------------------------------------------'
|
|
print '%4i | %8.2e |' % (nc, err)
|
|
else:
|
|
order.append(np.log(err/err_old)/np.log(float(nc_old)/float(nc)))
|
|
print '%4i | %8.2e | %6.4f | %6.4f' % (nc, err, err_old/err, order[-1])
|
|
err_old = err
|
|
nc_old = nc
|
|
|
|
self.assertTrue(len(np.where(np.array(order) > 0.9*self.expectedOrder)[0]) > np.floor(0.75*len(order)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|