def computeKmsij(TInd, a, IPatch): patch = lod_periodic.PatchPeriodic(world, k, TInd) aPatch = lod_periodic.localizeCoefficient(patch, a, periodic=True) correctorsList = lod.computeBasisCorrectors(patch, IPatch, aPatch) csi = lod.computeBasisCoarseQuantities(patch, correctorsList, aPatch) return patch, correctorsList, csi.Kmsij, csi
def computeKmsij(TInd): patch = Patch(world, k, TInd) IPatch = lambda: interp.L2ProjectionPatchMatrix(patch, boundaryConditions) aPatch = lambda: coef.localizeCoefficient(patch, aFine_ref) correctorsList = lod.computeBasisCorrectors(patch, IPatch, aPatch) csi = lod.computeBasisCoarseQuantities(patch, correctorsList, aPatch) return patch, correctorsList, csi.Kmsij, csi
def real_computeKmsij(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) correctorsList = lod.computeBasisCorrectors(patch, IPatch, aPatch) csi = lod.computeBasisCoarseQuantities(patch, correctorsList, aPatch) return patch, correctorsList, csi.Kmsij, csi
def UpdateCorrectors(TInd): rPatch = lambda: lod_periodic.localizeCoefficient( patchT[TInd], aPert, periodic=True) correctorsList = lod.computeBasisCorrectors(patchT[TInd], IPatch, rPatch) csi = lod.computeBasisCoarseQuantities(patchT[TInd], correctorsList, rPatch) return patchT[TInd], correctorsList, csi.Kmsij
def test_computeFullDomain(self): NWorldCoarse = np.array([1, 1, 1], dtype='int64') NCoarseElement = np.array([4, 2, 3], dtype='int64') NWorldFine = NWorldCoarse*NCoarseElement np.random.seed(0) world = World(NWorldCoarse, NCoarseElement) d = np.size(NWorldCoarse) k = np.max(NWorldCoarse) IWorld = interp.nodalPatchMatrix(Patch(world, k, 0)) aWorld = np.exp(np.random.rand(world.NtFine)) elementpIndexMap = util.lowerLeftpIndexMap(np.ones_like(NWorldCoarse), NWorldCoarse) elementpIndexMapFine = util.lowerLeftpIndexMap(NCoarseElement, NWorldFine) coarsepBasis = util.linearpIndexBasis(NWorldCoarse) finepBasis = util.linearpIndexBasis(NWorldFine) correctors = np.zeros((world.NpFine, world.NpCoarse)) basis = np.zeros((world.NpFine, world.NpCoarse)) for iElementWorldCoarse in it.product(*[np.arange(n, dtype='int64') for n in NWorldCoarse]): iElementWorldCoarse = np.array(iElementWorldCoarse) TInd = util.convertpCoordIndexToLinearIndex(NWorldCoarse, iElementWorldCoarse) patch = Patch(world, k, TInd) correctorsList = lod.computeBasisCorrectors(patch, IWorld, aWorld) worldpIndices = np.dot(coarsepBasis, iElementWorldCoarse) + elementpIndexMap correctors[:,worldpIndices] += np.column_stack(correctorsList) worldpFineIndices = np.dot(finepBasis, iElementWorldCoarse*NCoarseElement) + elementpIndexMapFine basis[np.ix_(worldpFineIndices, worldpIndices)] = world.localBasis AGlob = fem.assemblePatchMatrix(NWorldFine, world.ALocFine, aWorld) alpha = np.random.rand(world.NpCoarse) vH = np.dot(basis, alpha) QvH = np.dot(correctors, alpha) # Check norm inequality self.assertTrue(np.dot(QvH.T, AGlob*QvH) <= np.dot(vH.T, AGlob*vH)) # Check that correctors are really fine functions self.assertTrue(np.isclose(np.linalg.norm(IWorld*correctors, ord=np.inf), 0)) v = np.random.rand(world.NpFine, world.NpCoarse) v[util.boundarypIndexMap(NWorldFine)] = 0 # The chosen interpolation operator doesn't ruin the boundary conditions. vf = v-np.dot(basis, IWorld*v) vf = vf/np.sqrt(np.sum(vf*(AGlob*vf), axis=0)) # Check orthogonality self.assertTrue(np.isclose(np.linalg.norm(np.dot(vf.T, AGlob*(correctors - basis)), ord=np.inf), 0))
def computeKmsij(TInd, aPatch, k, boundaryConditions): tic = time.perf_counter() patch = lod_periodic.PatchPeriodic(world, k, TInd) if dim == 1: IPatch = lambda: interp.nodalPatchMatrix(patch) else: IPatch = lambda: interp.L2ProjectionPatchMatrix( patch, boundaryConditions) correctorsList = lod.computeBasisCorrectors(patch, IPatch, aPatch) csi = lod.computeBasisCoarseQuantities(patch, correctorsList, aPatch) toc = time.perf_counter() return patch, correctorsList, csi.Kmsij, csi.muTPrime, toc - tic
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 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_computeSingleT(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 aPatch = np.ones(NtPatch) basisCorrectorsList = lod.computeBasisCorrectors(patch, IPatch, aPatch) correctorSum = reduce(np.add, basisCorrectorsList) self.assertTrue(np.allclose(correctorSum, 0)) csi = lod.computeBasisCoarseQuantities(patch, basisCorrectorsList, aPatch) # Test that the matrices have the constants in their null space #self.assertTrue(np.allclose(np.sum(ec.csi.LTPrimeij, axis=1), 0)) #self.assertTrue(np.allclose(np.sum(ec.csi.LTPrimeij, axis=2), 0)) self.assertTrue(np.allclose(np.sum(csi.Kij, axis=0), 0)) self.assertTrue(np.allclose(np.sum(csi.Kij, axis=1), 0)) self.assertTrue(np.allclose(np.sum(csi.Kmsij, axis=0), 0)) self.assertTrue(np.allclose(np.sum(csi.Kmsij, axis=1), 0)) # I had difficulties come up with test cases here. This test # verifies that most "energy" is in the element T. elementTIndex = util.convertpCoordIndexToLinearIndex(patch.NPatchCoarse-1, patch.iElementPatchCoarse) self.assertTrue(np.all(csi.muTPrime[elementTIndex] >= csi.muTPrime)) self.assertTrue(not np.all(csi.muTPrime[elementTIndex+1] >= csi.muTPrime))