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:
def __init__(self,config):
def __init__(self, config):
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
def monitor(u):
#check if u_monitor is set, otherwise first call of monitor
def monitor(self, u):
# check if u_monitor is set, otherwise first call of monitor
if u.monitor == None:
#if first call, set change to high value
# if first call, set change to high value
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:
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:
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:
for j in range(u.shape[3]):
for k in range(u.shape[2]:
tmp_change= tmp_change+ (norm(u[:,:,k,j]-u_monitor[:,:,k,j], 'fro')/norm_data)**2
for k in range(u.shape[2]):
tmp_change = tmp_change + (
norm(u[:, :, k, j] - self.u_monitor[:, :, k, j], 'fro') / self.norm_data) ** 2
change = sqrt(tmp_change)
#hopefully copies to config
# hopefully copies to config
self.u_monitor = u
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