2010-04-24 17:26:44 -07:00
|
|
|
import os
|
|
|
|
import logging
|
2010-04-29 22:29:35 -07:00
|
|
|
import inspect
|
|
|
|
import numpy as np
|
2010-04-24 17:26:44 -07:00
|
|
|
|
|
|
|
|
2010-04-29 22:29:35 -07:00
|
|
|
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)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2010-04-29 22:29:35 -07:00
|
|
|
def error(self, message = None):
|
|
|
|
msg = smbLog.interpolator % (inspect.stack()[1][3], message)
|
|
|
|
self.log.error(msg)
|
|
|
|
|
|
|
|
|
|
|
|
smblog = smbLog(logging.DEBUG)
|
2010-04-24 17:26:44 -07:00
|
|
|
|
2010-01-30 11:15:00 -08:00
|
|
|
class smberror(Exception):
|
2010-03-08 12:05:42 -08:00
|
|
|
"""
|
|
|
|
this is a silly little exception subclass
|
|
|
|
"""
|
2010-01-30 11:15:00 -08:00
|
|
|
def __init__(self, val):
|
|
|
|
self.value = val
|
|
|
|
def __str__(self):
|
|
|
|
return repr(self.value)
|
|
|
|
|
2009-12-27 09:48:27 -08:00
|
|
|
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
|
2010-01-30 11:15:00 -08:00
|
|
|
|
2010-03-20 16:10:02 -07:00
|
|
|
def exact_func(X):
|
2010-03-08 12:05:42 -08:00
|
|
|
"""
|
|
|
|
the exact function used from baker's article (for testing)
|
|
|
|
"""
|
2010-03-20 16:10:02 -07:00
|
|
|
x = X[0]
|
|
|
|
y = X[0]
|
2010-01-30 11:15:00 -08:00
|
|
|
return np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
|
2010-03-18 22:18:59 -07:00
|
|
|
|
|
|
|
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)
|
2010-04-23 08:58:30 -07:00
|
|
|
|
2010-04-23 15:29:31 -07:00
|
|
|
def improved_answer(answer, exact, verbose=False):
|
2010-04-29 22:29:35 -07:00
|
|
|
if not answer['error']:
|
|
|
|
return True
|
|
|
|
smblog.debug('error: %s' % answer['error'])
|
2010-04-30 10:56:01 -07:00
|
|
|
smblog.debug('qlin: %s' % answer['qlin'])
|
2010-04-29 22:29:35 -07:00
|
|
|
smblog.debug('final: %s' % answer['final'])
|
2010-04-30 10:56:01 -07:00
|
|
|
smblog.debug('exact: %s' % exact)
|
2010-04-23 08:58:30 -07:00
|
|
|
|
2010-04-30 10:56:01 -07:00
|
|
|
if np.abs(answer['final'] - exact) <= np.abs(answer['qlin'] - exact):
|
2010-04-29 22:29:35 -07:00
|
|
|
smblog.debug(":) improved result")
|
2010-04-23 15:29:31 -07:00
|
|
|
return True
|
2010-04-23 08:58:30 -07:00
|
|
|
else:
|
2010-04-29 22:29:35 -07:00
|
|
|
smblog.debug(":( damaged result")
|
2010-04-23 15:29:31 -07:00
|
|
|
return False
|