smbinterp/interp/tools.py

79 lines
2.2 KiB
Python
Raw Normal View History

import os
2010-10-23 12:49:15 -07:00
import logging
2010-10-23 12:49:15 -07:00
import logging.handlers
import inspect
import numpy as np
2010-10-23 12:49:15 -07:00
def get_logger(filename, level = logging.DEBUG, logger_name = 'pymoab', size = 2048, backupCount = 10):
"""
This is a simple wrapper around a few sane
defaults using Python's logging functionality.
An explaination of the optional parameters:
filename : the filename
level : one of either debug, info, warn, or error
logger_name : if one needs multiple logfiles, one must name them.
size : the size in bytes of the logfile before roll (defaults to 2MB)
backupCount : number of rolled logs to keep around (defaults to 10)
"""
2009-12-27 09:48:27 -08:00
2010-10-23 12:49:15 -07:00
logger = logging.getLogger(logger_name)
logger.setLevel(level)
my_format = logging.Formatter('%(asctime)s %(levelname)s (%(process)d) %(filename)s %(funcName)s:%(lineno)d %(message)s')
handler = logging.handlers.RotatingFileHandler(
filename, maxBytes = size * 1024, backupCount = backupCount)
handler.setFormatter(my_format)
logger.addHandler(handler)
2010-10-23 12:49:15 -07:00
return logger
log = get_logger(filename = '/tmp/interp.log', level = logging.DEBUG, size = 10240)
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
def exact_func(X):
"""
the exact function used from baker's article (for testing)
"""
x ,y = X
answer = 1.0 + x*x + y*y # np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
log.debug(answer)
return answer
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]
answer = np.power((np.sin(x * np.pi / 2.0) * np.sin(y * np.pi / 2.0) * np.sin(z * np.pi / 2.0)), 2)
log.debug(answer)
return answer
def improved_answer(answer, exact, verbose=False):
if not answer['error']:
return True
2010-10-29 11:42:05 -07:00
log.debug('qlin: %s' % answer['qlin'])
log.debug('error: %s' % answer['error'])
log.debug('final: %s' % answer['final'])
log.debug('exact: %s' % exact)
if np.abs(answer['final'] - exact) <= np.abs(answer['qlin'] - exact):
2010-10-29 11:42:05 -07:00
log.debug(":) improved result")
return True
else:
2010-10-29 11:42:05 -07:00
log.debug(":( damaged result")
return False