def computeIndicators(TInd): print('.', end='', flush=True) aPatch = lambda: coef.localizeCoefficient(patchT[TInd], aFine_ref) rPatch = lambda: coef.localizeCoefficient(patchT[TInd], a_Fine_to_be_approximated) epsCoarse = lod.computeErrorIndicatorCoarseFromCoefficients( patchT[TInd], csiT[TInd].muTPrime, aPatch, rPatch) # New for E_ft f_ref_patch = f_ref[util.extractElementFine( world.NWorldCoarse, world.NCoarseElement, patchT[TInd].iElementWorldCoarse, extractElements=False)] f_patch = f_trans[util.extractElementFine(world.NWorldCoarse, world.NCoarseElement, patchT[TInd].iElementWorldCoarse, extractElements=False)] E_f = lod.computeEftErrorIndicatorCoarse(patchT[TInd], cetaTPrimeT[TInd], etaTT[TInd], aPatch, rPatch, f_ref_patch, f_patch) return epsCoarse, E_f
def compute_element_corrector(self, b_patch, a_patch, IPatch, ARhsList): '''Compute the fine correctors over the patch. Compute the correctors a(Q_T \lambda_x, z)_{U_K(T)} + \tau b(Q_T \lambda_x, z)_{U_K(T)} = a(\lambda_x, z)_T + \tau b(\lambda_x, z)_T ''' numRhs = len(ARhsList) world = self.world NCoarseElement = world.NCoarseElement NPatchCoarse = self.NPatchCoarse NPatchFine = NPatchCoarse * NCoarseElement NtFine = np.prod(NPatchFine) NpFine = np.prod(NPatchFine + 1) b_patch = b_patch.aFine a_patch = a_patch.aFine assert (np.size(a_patch) == NtFine) iElementPatchCoarse = self.iElementPatchCoarse elementFinetIndexMap = util.extractElementFine(NPatchCoarse, NCoarseElement, iElementPatchCoarse, extractElements=True) elementFinepIndexMap = util.extractElementFine(NPatchCoarse, NCoarseElement, iElementPatchCoarse, extractElements=False) SElementFull = fem.assemblePatchMatrix(NCoarseElement, world.ALocFine, b_patch[elementFinetIndexMap]) KElementFull = fem.assemblePatchMatrix(NCoarseElement, world.ALocFine, a_patch[elementFinetIndexMap]) SPatchFull = fem.assemblePatchMatrix(NPatchFine, world.ALocFine, b_patch) KPatchFull = fem.assemblePatchMatrix(NPatchFine, world.ALocFine, a_patch) bPatchFullList = [] for rhsIndex in range(numRhs): bPatchFull = np.zeros(NpFine) bPatchFull[ elementFinepIndexMap] += SElementFull * ARhsList[rhsIndex] bPatchFull[ elementFinepIndexMap] += KElementFull * ARhsList[rhsIndex] bPatchFullList.append(bPatchFull) correctorsList = ritzProjectionToFinePatchWithGivenSaddleSolver( world, self.iPatchWorldCoarse, NPatchCoarse, SPatchFull + KPatchFull, bPatchFullList, IPatch, self.saddleSolver) return correctorsList
def test_testCsi_muTPrime(self): # 3D world NWorldCoarse = np.array([6, 5, 4]) NCoarseElement = np.array([5, 2, 3]) world = World(NWorldCoarse, NCoarseElement) # Full patch TInd = 0 k = 6 patch = Patch(world, k, TInd) # Let functions = [x1] def computeFunctions(): pc = util.pCoordinates(world.NWorldFine) x1 = pc[:,0] x2 = pc[:,1] return [x1] elementFinepIndexMap = util.extractElementFine(NWorldCoarse, NCoarseElement, 0*NCoarseElement, extractElements=False) elementFinetIndexMap = util.extractElementFine(NWorldCoarse, NCoarseElement, 0*NCoarseElement, extractElements=True) # Let lambdas = functions lambdasList = [f[elementFinepIndexMap] for f in computeFunctions()] ## Case # aPatch = 1 # Let corrector Q = functions # Expect: muTPrime for first element T is 0, the others 1 correctorsList = computeFunctions() aPatch = np.ones(world.NpFine) csi = lod.computeCoarseQuantities(patch, lambdasList, correctorsList, aPatch) self.assertAlmostEqual(np.sum(csi.muTPrime), 6*5*4-1) self.assertAlmostEqual(csi.muTPrime[0], 0) ## Case # aPatch = 1 # Let corrector Q = 2*functions # Expect: muTPrime is 1 for first element and 4 for all others correctorsList = [2*f for f in computeFunctions()] aPatch = np.ones(world.NpFine) csi = lod.computeCoarseQuantities(patch, lambdasList, correctorsList, aPatch) self.assertAlmostEqual(np.sum(csi.muTPrime), 4*(6*5*4-1)+1) self.assertAlmostEqual(csi.muTPrime[0], 1)
def test_testCsi(self): NWorldCoarse = np.array([4, 5, 6]) NCoarseElement = np.array([5, 2, 3]) world = World(NWorldCoarse, NCoarseElement) d = np.size(NWorldCoarse) k = 1 iElementWorldCoarse = np.array([2, 1, 2]) ec = lod.elementCorrector(world, k, iElementWorldCoarse) IPatch = interp.L2ProjectionPatchMatrix(ec.iPatchWorldCoarse, ec.NPatchCoarse, NWorldCoarse, NCoarseElement) NtPatch = np.prod(ec.NPatchCoarse*NCoarseElement) np.random.seed(1) aPatch = np.random.rand(NtPatch) coefficientPatch = coef.coefficientFine(ec.NPatchCoarse, NCoarseElement, aPatch) ec.computeCorrectors(coefficientPatch, IPatch) ec.computeCoarseQuantities() TFinetIndexMap = util.extractElementFine(ec.NPatchCoarse, NCoarseElement, ec.iElementPatchCoarse, extractElements=True) TFinepIndexMap = util.extractElementFine(ec.NPatchCoarse, NCoarseElement, ec.iElementPatchCoarse, extractElements=False) TCoarsepIndexMap = util.extractElementFine(ec.NPatchCoarse, np.ones_like(NCoarseElement), ec.iElementPatchCoarse, extractElements=False) APatchFine = fem.assemblePatchMatrix(ec.NPatchCoarse*NCoarseElement, world.ALocFine, aPatch) AElementFine = fem.assemblePatchMatrix(NCoarseElement, world.ALocFine, aPatch[TFinetIndexMap]) basisPatch = fem.assembleProlongationMatrix(ec.NPatchCoarse, NCoarseElement) correctorsPatch = np.column_stack(ec.fsi.correctorsList) localBasis = world.localBasis KmsijShouldBe = -basisPatch.T*(APatchFine*(correctorsPatch)) KmsijShouldBe[TCoarsepIndexMap,:] += np.dot(localBasis.T, AElementFine*localBasis) self.assertTrue(np.isclose(np.max(np.abs(ec.csi.Kmsij-KmsijShouldBe)), 0))
def test_testCsi_Kmsij(self): NWorldCoarse = np.array([4, 5, 6]) NCoarseElement = np.array([5, 2, 3]) world = World(NWorldCoarse, NCoarseElement) d = np.size(NWorldCoarse) k = 1 iElementWorldCoarse = np.array([2, 1, 2]) TInd = util.convertpCoordIndexToLinearIndex(NWorldCoarse, iElementWorldCoarse) patch = Patch(world, k, TInd) IPatch = interp.L2ProjectionPatchMatrix(patch) NtPatch = patch.NtFine np.random.seed(1) aPatch = np.random.rand(NtPatch) basisCorrectorsList = lod.computeBasisCorrectors(patch, IPatch, aPatch) csi = lod.computeBasisCoarseQuantities(patch, basisCorrectorsList, aPatch) TFinetIndexMap = util.extractElementFine(patch.NPatchCoarse, NCoarseElement, patch.iElementPatchCoarse, extractElements=True) TFinepIndexMap = util.extractElementFine(patch.NPatchCoarse, NCoarseElement, patch.iElementPatchCoarse, extractElements=False) TCoarsepIndexMap = util.extractElementFine(patch.NPatchCoarse, np.ones_like(NCoarseElement), patch.iElementPatchCoarse, extractElements=False) APatchFine = fem.assemblePatchMatrix(patch.NPatchFine, world.ALocFine, aPatch) AElementFine = fem.assemblePatchMatrix(NCoarseElement, world.ALocFine, aPatch[TFinetIndexMap]) basisPatch = fem.assembleProlongationMatrix(patch.NPatchCoarse, NCoarseElement) correctorsPatch = np.column_stack(basisCorrectorsList) localBasis = world.localBasis KmsijShouldBe = -basisPatch.T*(APatchFine*(correctorsPatch)) KmsijShouldBe[TCoarsepIndexMap,:] += np.dot(localBasis.T, AElementFine*localBasis) self.assertTrue(np.isclose(np.max(np.abs(csi.Kmsij-KmsijShouldBe)), 0))
def checkerboardI(ii): coeff = alpha * np.ones(NtFine) #find out which indices on fine grid correspond to element ii on epsilon grid elementIndex = util.convertpLinearIndexToCoordIndex(Nepsilon - 1, ii)[:] indices = util.extractElementFine(Nepsilon, NFineElement // NepsilonElement, elementIndex) coeff[indices] = beta return coeff
def computeRmsi(TInd): patch = Patch(world, k, TInd) IPatch = lambda: interp.L2ProjectionPatchMatrix(patch, boundaryConditions) aPatch = lambda: coef.localizeCoefficient(patch, aFine_ref) MRhsList = [f_ref[util.extractElementFine(world.NWorldCoarse, world.NCoarseElement, patch.iElementWorldCoarse, extractElements=False)]]; correctorRhs = lod.computeElementCorrector(patch, IPatch, aPatch, None, MRhsList)[0] Rmsi = lod.computeRhsCoarseQuantities(patch, correctorRhs, aPatch) return patch, correctorRhs, Rmsi
def real_computeRmsi(TInd): print('.', end='', flush=True) patch = Patch(world, k, TInd) IPatch = lambda: interp.L2ProjectionPatchMatrix(patch, boundaryConditions) aPatch = lambda: coef.localizeCoefficient(patch, a_Fine_to_be_approximated) MRhsList = [f_ref[util.extractElementFine(world.NWorldCoarse, world.NCoarseElement, patch.iElementWorldCoarse, extractElements=False)]]; correctorRhs = lod.computeElementCorrector(patch, IPatch, aPatch, None, MRhsList)[0] Rmsi, cetaTPrime = lod.computeRhsCoarseQuantities(patch, correctorRhs, aPatch, True) return patch, correctorRhs, Rmsi, cetaTPrime
def computeRmsi(TInd): print('.', end='', flush=True) patch = Patch(world, k, TInd) IPatch = lambda: interp.L2ProjectionPatchMatrix(patch, boundaryConditions) aPatch = lambda: coef.localizeCoefficient(patch, aFine_ref) MRhsList = [ f_ref[util.extractElementFine(world.NWorldCoarse, world.NCoarseElement, patch.iElementWorldCoarse, extractElements=False)] ] correctorRhs = lod.computeElementCorrector(patch, IPatch, aPatch, None, MRhsList)[0] Rmsi, cetaTPrime = lod.computeRhsCoarseQuantities(patch, correctorRhs, aPatch, True) eft_patch = Patch(world, 1, TInd) a_eft_Patch = lambda: coef.localizeCoefficient(eft_patch, aFine_ref) etaT = lod.computeSupremumForEf(eft_patch, a_eft_Patch) return patch, correctorRhs, Rmsi, cetaTPrime, etaT
def UpdateCorrectors(self, TInd): # print(" UPDATING {}".format(TInd)) patch = Patch(self.world, self.k, TInd) IPatch = lambda: interp.L2ProjectionPatchMatrix( patch, self.boundaryConditions) rPatch = lambda: coef.localizeCoefficient( patch, self.a_Fine_to_be_approximated) MRhsList = [ self.f_trans[util.extractElementFine(self.world.NWorldCoarse, self.world.NCoarseElement, patch.iElementWorldCoarse, extractElements=False)] ] correctorsList = lod.computeBasisCorrectors(patch, IPatch, rPatch) csi = lod.computeBasisCoarseQuantities(patch, correctorsList, rPatch) correctorRhs = lod.computeElementCorrector(patch, IPatch, rPatch, None, MRhsList)[0] Rmsij = lod.computeRhsCoarseQuantities(patch, correctorRhs, rPatch) return patch, correctorsList, csi.Kmsij, Rmsij, correctorRhs
def test_computeErrorIndicatorCoarseFromCoefficients(self): ## Setup # 2D, variables x0 and x1 NCoarse = np.array([4, 3]) NCoarseElement = np.array([2, 3]) world = World(NCoarse, NCoarseElement) patch = Patch(world, 4, 0) NFine = NCoarse*NCoarseElement NtCoarse = world.NtCoarse # muTPrime = 1, ..., NtCoarse muTPrime = np.arange(NtCoarse) + 1 ## Case # aOld = aNew = 1 # Expect: 0 error indicator aOld = np.ones(world.NtFine, dtype=np.float64) aNew = aOld self.assertAlmostEqual(lod.computeErrorIndicatorCoarseFromCoefficients(patch, muTPrime, aOld, aNew), 0) #### Same test for Matrix valued #### Aeye = np.tile(np.eye(2), [np.prod(NFine), 1, 1]) aNew = np.einsum('tji, t -> tji', Aeye, aNew) self.assertAlmostEqual(lod.computeErrorIndicatorCoarseFromCoefficients(patch, muTPrime, aOld, aNew), 0) ## Case # aOld = 1 # aNew = 10 # Expect: sqrt(1/10 * 1/10*(10-1)**2*1 * (NtCoarse)*(NtCoarse+1)/2) aOld = np.ones(world.NtFine, dtype=np.float64) aNew = 10*aOld self.assertAlmostEqual(lod.computeErrorIndicatorCoarseFromCoefficients(patch, muTPrime, aOld, aNew), np.sqrt(1/10 * 1/10*(10-1)**2*1 * (NtCoarse)*(NtCoarse+1)/2)) #### Same test for Matrix valued #### aNew = np.einsum('tji, t -> tji', Aeye, aNew) aOld = np.einsum('tji, t-> tji', Aeye, aOld) self.assertAlmostEqual(lod.computeErrorIndicatorCoarseFromCoefficients(patch, muTPrime, aOld, aNew), np.sqrt(1/10 * 1/10*(10-1)**2*1 * (NtCoarse)*(NtCoarse+1)/2)) ## Case # aOld = 1 # aNew = 1 except in TInd=2 (where muTPrime == 3), where it is 10 # Expect: sqrt(1 * 1/10*(10-1)**2*1 * 3) aOld = np.ones(world.NtFine, dtype=np.float64) aNew = np.ones(world.NtFine, dtype=np.float64) elementFinetIndexMap = util.extractElementFine(NCoarse, NCoarseElement, np.array([2, 0]), extractElements=True) aNew[elementFinetIndexMap] = 10 self.assertAlmostEqual(lod.computeErrorIndicatorCoarseFromCoefficients(patch, muTPrime, aOld, aNew), np.sqrt(1 * 1/10*(10-1)**2*1 * 3)) #### Same test for Matrix valued #### aOld = np.einsum('tji, t-> tji', Aeye, aOld) self.assertAlmostEqual(lod.computeErrorIndicatorCoarseFromCoefficients(patch, muTPrime, aOld, aNew), np.sqrt(1 * 1/10*(10-1)**2*1 * 3))
def test_computeErrorIndicatorFine_zeroT(self): ## Setup # 2D, variables x0 and x1 NCoarse = np.array([4, 3]) NCoarseElement = np.array([2, 3]) world = World(NCoarse, NCoarseElement) patch = Patch(world, 4, 0) NFine = NCoarse*NCoarseElement # Let functions = [x1, 2*x2] def computeFunctions(): pc = util.pCoordinates(NFine) x1 = pc[:,0] x2 = pc[:,1] return [x1, 2*x2] # Mock corrector Q = functions correctorsList = computeFunctions() elementFinepIndexMap = util.extractElementFine(NCoarse, NCoarseElement, 0*NCoarseElement, extractElements=False) elementFinetIndexMap = util.extractElementFine(NCoarse, NCoarseElement, 0*NCoarseElement, extractElements=True) # Let lambdas = functions too lambdasList = [f[elementFinepIndexMap] for f in computeFunctions()] ## Case # AOld = ANew = scalar 1 # Expect: Error indicator should be zero aOld = np.ones(world.NtFine, dtype=np.float64) aNew = aOld self.assertEqual(lod.computeErrorIndicatorFine(patch, lambdasList, correctorsList, aOld, aNew), 0) ## Case # AOld = scalar 1 # ANew = scalar 10 # Expect: Error indicator is sqrt of integral over 11 elements with value (10-1)**2/10**2 aOld = np.ones(world.NtFine, dtype=np.float64) aNew = 10*aOld self.assertAlmostEqual(lod.computeErrorIndicatorFine(patch, lambdasList, correctorsList, aOld, aNew), np.sqrt(11*(10-1)**2/10**2)) ## Case # AOld = scalar 1 # ANew = scalar 10 except in T where ANew = 1000 # Expect: Error indicator is like in previous case, but /10 aOld = np.ones(world.NtFine, dtype=np.float64) aNew = 10*aOld aNew[elementFinetIndexMap] = 1000 self.assertAlmostEqual(lod.computeErrorIndicatorFine(patch, lambdasList, correctorsList, aOld, aNew), 0.1*np.sqrt(11*(10-1)**2/10**2))
def computeElementCorrector_helmholtz(patch, IPatch, aPatch, kPatch, k2Patch, ARhsList=None, MRhsList=None, saddleSolver=None): '''Compute the fine correctors over a patch. Compute the correctors B( Q_T_j, vf)_{U_K(T)} = B( ARhs_j, vf)_{T} + (MRhs_j, vf)_{T} where B is the sesquilinear form associated with the linear Helmholtz eq. ''' while callable(IPatch): IPatch = IPatch() while callable(aPatch): aPatch = aPatch() while callable(kPatch): kPatch = kPatch() while callable(k2Patch): k2Patch = k2Patch() assert (ARhsList is not None or MRhsList is not None) numRhs = None if ARhsList is not None: assert (numRhs is None or numRhs == len(ARhsList)) numRhs = len(ARhsList) if MRhsList is not None: assert (numRhs is None or numRhs == len(MRhsList)) numRhs = len(MRhsList) world = patch.world NCoarseElement = world.NCoarseElement NPatchCoarse = patch.NPatchCoarse d = np.size(NCoarseElement) NPatchFine = NPatchCoarse * NCoarseElement NtFine = np.prod(NPatchFine) NpFineCoarseElement = np.prod(NCoarseElement + 1) NpCoarse = np.prod(NPatchCoarse + 1) NpFine = np.prod(NPatchFine + 1) assert (aPatch.shape[0] == NtFine) assert (aPatch.ndim == 1 or aPatch.ndim == 3) assert (kPatch.ndim == 1) assert (k2Patch.ndim == 1) if aPatch.ndim == 1: ALocFine = world.ALocFine elif aPatch.ndim == 3: ALocFine = world.ALocMatrixFine MLocFine = world.MLocFine BdLocFine = fem.localBoundaryMassMatrixGetter(NCoarseElement * world.NWorldCoarse) iElementPatchCoarse = patch.iElementPatchCoarse elementFinetIndexMap = util.extractElementFine(NPatchCoarse, NCoarseElement, iElementPatchCoarse, extractElements=True) elementFinepIndexMap = util.extractElementFine(NPatchCoarse, NCoarseElement, iElementPatchCoarse, extractElements=False) # global boundary? bdMapWorld = world.boundaryConditions == 1 # on element bdMapElement = np.zeros([d, 2], dtype='bool') inheritElement0 = patch.iElementWorldCoarse == 0 inheritElement1 = (patch.iElementWorldCoarse + np.ones(d)) == world.NWorldCoarse bdMapElement[inheritElement0, 0] = bdMapWorld[inheritElement0, 0] bdMapElement[inheritElement1, 1] = bdMapWorld[inheritElement1, 1] # on patch inherit0 = patch.iPatchWorldCoarse == 0 inherit1 = (patch.iPatchWorldCoarse + NPatchCoarse) == world.NWorldCoarse bdMapPatch = np.zeros([d, 2], dtype='bool') bdMapPatch[inherit0, 0] = bdMapWorld[inherit0, 0] bdMapPatch[inherit1, 1] = bdMapWorld[inherit1, 1] if ARhsList is not None: AElementFull = fem.assemblePatchMatrix(NCoarseElement, ALocFine, aPatch[elementFinetIndexMap]) k2MElementFull = fem.assemblePatchMatrix(NCoarseElement, MLocFine, k2Patch[elementFinetIndexMap]) kBdElementFull = fem.assemblePatchBoundaryMatrix( NCoarseElement, BdLocFine, kPatch[elementFinetIndexMap], bdMapElement) if MRhsList is not None: MElementFull = fem.assemblePatchMatrix(NCoarseElement, MLocFine) APatchFull = fem.assemblePatchMatrix(NPatchFine, ALocFine, aPatch) k2MPatchFull = fem.assemblePatchMatrix(NPatchFine, MLocFine, k2Patch) kBdPatchFull = fem.assemblePatchBoundaryMatrix(NPatchFine, BdLocFine, kPatch, bdMapPatch) SPatchFull = APatchFull - k2MPatchFull + 1j * kBdPatchFull bPatchFullList = [] for rhsIndex in range(numRhs): bPatchFull = np.zeros(NpFine, dtype='complex128') if ARhsList is not None: bPatchFull[elementFinepIndexMap] += ( AElementFull - k2MElementFull + 1j * kBdElementFull) * ARhsList[rhsIndex] if MRhsList is not None: bPatchFull[ elementFinepIndexMap] += MElementFull * MRhsList[rhsIndex] bPatchFullList.append(bPatchFull) correctorsList = ritzProjectionToFinePatch(patch, SPatchFull, bPatchFullList, IPatch, saddleSolver) return correctorsList