Procházet zdrojové kódy

Added iteration and error output

Snow před 8 roky
rodič
revize
e7df28281b
2 změnil soubory, kde provedl 16 přidání a 13 odebrání
  1. 8 8
      hdpg1d/adaptation.py
  2. 8 5
      hdpg1d/postprocess.py

+ 8 - 8
hdpg1d/adaptation.py

@@ -70,9 +70,9 @@ class hdpg1d(object):
         plt.plot(self.mesh, uNode, 'C3.')
         plt.xlabel('$x$', fontsize=17)
         plt.ylabel('$u$', fontsize=17)
-        plt.axis([-0.05, 1.05, 0, 1.3])
+        # plt.axis([-0.05, 1.05, 0, 1.3])
         plt.grid()
-        plt.pause(1e-1)
+        plt.pause(5e-1)
         plt.clf()
 
     def meshAdapt(self, index):
@@ -146,7 +146,6 @@ class hdpg1d(object):
 
         # diffusion constant
         kappa = self.kappa
-
         z_q, z_u, z_hat = np.zeros(p * numEle), \
             np.zeros(p *
                      numEle), np.zeros(numEle - 1)
@@ -286,13 +285,15 @@ class hdpg1d(object):
         return np.abs(np.sum(R) + np.sum(R_g)), refine_index + 1
 
     def adaptive(self):
-        tol = 1e-12
+        tol = 1e-10
         estError = 10
         counter = 0
-        ceilCounter = 100
+        ceilCounter = 50
         trueErrorList = [[], []]
         estErrorList = [[], []]
-        while estError > tol or counter > ceilCounter:
+        while estError > tol and counter < ceilCounter:
+            print("Iteration {}. Target function error {:.3e}.".format(
+                counter, estError))
             # solve
             u, uFace = self.solveLocal()
             adjoint, adjointFace = self.solveAdjoint()
@@ -302,8 +303,7 @@ class hdpg1d(object):
                 self.plotU(counter)
             # record error
             trueErrorList[0].append(self.numEle)
-            trueErrorList[1].append(np.abs(
-                u[self.numEle * self.numBasisFuncs - 1] - np.sqrt(self.kappa)))
+            trueErrorList[1].append(u[self.numEle * self.numBasisFuncs - 1])
             estError, index = self.residual(u, uFace, adjoint, adjointFace)
             estErrorList[0].append(self.numEle)
             estErrorList[1].append(estError)

+ 8 - 5
hdpg1d/postprocess.py

@@ -8,14 +8,15 @@ import numpy as np
 class utils(object):
     def __init__(self, solution):
         self.solution = solution
-        exactNumEle = 200
+        exactNumEle = 500
         exactBasisFuncs = 5
         self.solution.coeff.numEle = exactNumEle
         self.solution.coeff.pOrder = exactBasisFuncs - 1
         self.solution.mesh = np.linspace(0, 1, exactNumEle + 1)
-        # approximate the exact solution
-        self.exactSol = self.solution.solveLocal(
-        )[0][exactNumEle * exactBasisFuncs - 1]
+        # approximate the exact solution for general problems
+        # self.exactSol = self.solution.solveLocal()[0][exactNumEle * exactBasisFuncs - 1]
+        # for the reaction diffusion test problem, we know the exact solution
+        self.exactSol = np.sqrt(self.solution.kappa)
 
     def errorL2(self):
         errorL2 = 0.
@@ -28,7 +29,8 @@ class utils(object):
         return errorL2
 
     def uniConv(self):
-        numBasisFuncs = np.arange(2, 3)
+        numBasisFuncs = np.arange(
+            self.solution.numBasisFuncs, self.solution.numBasisFuncs + 1)
         numEle = 2**np.arange(1, 9)
         uniError = np.zeros((numEle.size, numBasisFuncs.size))
         for i in range(numBasisFuncs.size):
@@ -42,6 +44,7 @@ class utils(object):
         """Plot the uniform and adaptive convergence history"""
         plt.figure(2)
         trueErrorList = self.solution.trueErrorList
+        trueErrorList[1] = np.abs(trueErrorList[1] - self.exactSol)
         estErrorList = self.solution.estErrorList
         plt.loglog(trueErrorList[0],
                    trueErrorList[1], '-ro')