Commit d24427fb authored by Lars Stietz's avatar Lars Stietz
Browse files

sweep_Mat function implemented

parent a750b6df
...@@ -48,46 +48,54 @@ def asmSlowMat(dt): ...@@ -48,46 +48,54 @@ def asmSlowMat(dt):
Q = Q + np.diag(dt[i]*np.ones(sz-i),-i) Q = Q + np.diag(dt[i]*np.ones(sz-i),-i)
return Q return Q
# function for SDC sweep in one iteration step
def sweep(u,d_tau,Smat,Qmat,f): def sweep(u,d_tau,Smat,Qmat,f):
"""
adjustment for strt/nd=False still needed
"""
num_nodes = len(u) num_nodes = len(u)
u_old = deepcopy(u) u_old = deepcopy(u)
for m in range(1,num_nodes-1): for m in range(1,num_nodes-1):
u[m] = 1/(1-f(1)*d_tau[m])*(u[m-1]-d_tau[m]*f(u_old[m]) + np.dot(Smat[m+1,1:],f(u_old))) u[m] = 1/(1-f(1)*d_tau[m])*(u[m-1]-d_tau[m]*f(u_old[m]) + np.dot(Smat[m+1,1:],f(u_old)))
u[num_nodes] = u[0] + np.dot(Qmat[-1,1:],u) u[num_nodes-1] = u[0] + np.dot(Qmat[-1,1:],f(u))
return u return u
# function for SDC sweep with matrices
def sweepMat(u,d_tau,Qmat,Qs,f):
u_old = deepcopy(u)
u = np.linalg.solve(np.eye(np.shape(Qs)[0])-f(1)*Qs,u[0]-f(1)*Qs@u_old+f(1)*Qmat[1:,1:]@u_old)
u[-1] = u[0] + np.dot(Qmat[-1,1:],f(u))
return u
# function to calculate the residual
def res(u, Qmat,u_0): def res(u, Qmat,u_0):
"""
adjustment for strt/nd=False still needed
"""
r = u_0 + Qmat[1:,1:]@f(u)-u r = u_0 + Qmat[1:,1:]@f(u)-u
return r return r
solving = 1 #1 solves straight forward, 1 looks at accuracy, 2 compares matrix approach to straight forward solving = 1 # 0<->accuracy, 1<->solving, 2<->matrix sweep comparison
if solving == 1: if solving == 1:
# SDC straight forward approach # SDC straight forward approach
""" k_max = 100
res = np.zeros(num_nodes) res_2 = 2
k_max = 20
res_max = np.amax(u[0] +coll.Qmat[2 :,:]@u)
res_it = np.empty(0) res_it = np.empty(0)
j = 0 j = 0
error = 1 Q_slow = asmSlowMat(coll.delta_m)
error_it = np.empty(0) #while res_2 > 1e-14:
""" for it in range(k_max):
for it in range(250): #u = sweep(u,coll.delta_m,coll.Smat,coll.Qmat,f)
#while res_max>1e-15 or error>1e-15: u = sweepMat(u,coll.delta_m,coll.Qmat,Q_slow,f)
sweep(u,coll.delta_m,coll.Smat,coll.Qmat,f)
r = res(u.real,coll.Qmat,u_0) r = res(u.real,coll.Qmat,u_0)
""" res_2 = np.linalg.norm(r)
for i in range(num_nodes-1): res_it = np.append(res_it,res_2)
res[i] = u[0].real + np.dot(coll.Qmat[i][1:-1],f(u[1:-1]).real)-u[i].real
res_max = np.amax(abs(res))
error = np.linalg.norm(u_old.real-u.real)
error_it = np.append(error_it,error)
res_it = np.append(res_it,res_max)
"""
plt.figure(1) plt.figure(1)
...@@ -100,8 +108,8 @@ if solving == 1: ...@@ -100,8 +108,8 @@ if solving == 1:
plt.legend() plt.legend()
plt.figure(2) plt.figure(2)
plt.title('Update ans Residual') plt.title('Update ans Residual')
plt.semilogy(error_it,label= 'update difference (2-Norm)') #plt.semilogy(error_it,label= 'update difference (2-Norm)')
plt.semilogy(r,label='residual') plt.semilogy(res_it,label='residual')
plt.xlabel('iteration step') plt.xlabel('iteration step')
plt.legend() plt.legend()
...@@ -111,9 +119,7 @@ elif solving == 0: ...@@ -111,9 +119,7 @@ elif solving == 0:
kons = np.zeros(it) kons = np.zeros(it)
for j in range(it): for j in range(it):
u_old = deepcopy(u) u = sweep(u,coll.delta_m,coll.Smat,coll.Qmat,f)
for i in range(num_nodes):
u[i+1] = 1/(1-l*coll.delta_m[i])*(u[i]-coll.delta_m[i]*f(u_old[i+1])+np.dot(coll.Smat[i+1][1:],f(u_old[1:])))
kons[j] = np.log(abs((y(coll.nodes[-1])-u[-1])))/np.log(np.amax(coll.delta_m)) kons[j] = np.log(abs((y(coll.nodes[-1])-u[-1])))/np.log(np.amax(coll.delta_m))
if j>0: if j>0:
print(kons[j]-kons[j-1]) print(kons[j]-kons[j-1])
...@@ -151,11 +157,3 @@ elif solving == 2: ...@@ -151,11 +157,3 @@ elif solving == 2:
plt.legend() plt.legend()
plt.show() plt.show()
\ No newline at end of file
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