Commit df972ad2 authored by Matthijs's avatar Matthijs
Browse files

matlab -> python translation IterateMonitor

parent 7ad2bb43
from numpy.linalg import norm
from numpy import sqrt
class IterateMonitor: class IterateMonitor:
def __init__(self,config): def __init__(self, config):
self.norm_data = config['norm_data'] self.norm_data = config['norm_data']
#this will be set after the first call of monitor # this will be set after the first call of monitor
self.u_monitor = None self.u_monitor = None
def monitor(u): def monitor(self, u):
#check if u_monitor is set, otherwise first call of monitor # check if u_monitor is set, otherwise first call of monitor
if u.monitor == None: if u.monitor == None:
#if first call, set change to high value # if first call, set change to high value
return 999 return 999
#this is the right calculation for change if u does not have blocks structure # this is the right calculation for change if u does not have blocks structure
if u.ndim < 3: if u.ndim < 3:
tmp_change= (norm(u-u_monitor, 'fro')/norm_data)**2 # What function are we trying to import here? tmp_change = (norm(u - self.u_monitor,
'fro') / self.norm_data) ** 2 # What function are we trying to import here?
elif u.ndim == 3: elif u.ndim == 3:
for j in range(u.shape[2]): for j in range(u.shape[2]):
tmp_change= tmp_change+ (norm(u[:,:,j]-u_monitor[:,:,j], 'fro')/norm_data)**2 tmp_change = tmp_change + (norm(u[:, :, j] - self.u_monitor[:, :, j], 'fro') / self.norm_data) ** 2
else: else:
for j in range(u.shape[3]): for j in range(u.shape[3]):
for k in range(u.shape[2]: for k in range(u.shape[2]):
tmp_change= tmp_change+ (norm(u[:,:,k,j]-u_monitor[:,:,k,j], 'fro')/norm_data)**2 tmp_change = tmp_change + (
norm(u[:, :, k, j] - self.u_monitor[:, :, k, j], 'fro') / self.norm_data) ** 2
change = sqrt(tmp_change) change = sqrt(tmp_change)
#hopefully copies to config # hopefully copies to config
self.u_monitor = u self.u_monitor = u
return change return change
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment