smbinterp/lib/baker/tools.py

85 lines
2.1 KiB
Python

import os
import logging
import inspect
import numpy as np
class smbLog(object):
interpolator = "%s ==> %s"
def __init__(self, level = logging.DEBUG):
logging.basicConfig(
level = level,
format = '%(asctime)s %(levelname)s %(message)s',
filename = os.path.join(os.sep, 'tmp', 'baker.lol'),
)
self.log = logging.getLogger()
def debug(self, message = None):
msg = smbLog.interpolator % (inspect.stack()[1][3], message)
self.log.debug(msg)
def info(self, message = None):
msg = smbLog.interpolator % (inspect.stack()[1][3], message)
self.log.info(msg)
def warn(self, message = None):
msg = smbLog.interpolator % (inspect.stack()[1][3], message)
self.log.warn(msg)
def error(self, message = None):
msg = smbLog.interpolator % (inspect.stack()[1][3], message)
self.log.error(msg)
smblog = smbLog(logging.DEBUG)
class smberror(Exception):
"""
this is a silly little exception subclass
"""
def __init__(self, val):
self.value = val
def __str__(self):
return repr(self.value)
def rms(errors):
"""
root mean square calculation
"""
r = 0.0
for i in errors:
r += np.power(i, 2)
r = np.sqrt(r / len(errors))
return r
def exact_func(X):
"""
the exact function used from baker's article (for testing)
"""
x = X[0]
y = X[0]
return np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
def exact_func_3D(X):
"""
the exact function (3D) used from baker's article (for testing)
"""
x = X[0]
y = X[1]
z = X[2]
return np.power((np.sin(x * np.pi / 2.0) * np.sin(y * np.pi / 2.0) * np.sin(z * np.pi / 2.0)), 2)
def improved_answer(answer, exact, verbose=False):
if not answer['error']:
return True
smblog.debug('error: %s' % answer['error'])
smblog.debug('qlin: %s' % answer['qlin'])
smblog.debug('final: %s' % answer['final'])
smblog.debug('exact: %s' % exact)
if np.abs(answer['final'] - exact) <= np.abs(answer['qlin'] - exact):
smblog.debug(":) improved result")
return True
else:
smblog.debug(":( damaged result")
return False