Exemple #1
0
    def test_convertpCoordinateToIndex(self):
        N = np.array([100])
        coord = np.array([44])
        self.assertTrue(util.convertpCoordinateToIndex(N, coord) == 44)

        N = np.array([100, 200])
        coord = np.array([44, 55])
        self.assertTrue(util.convertpCoordinateToIndex(N, coord) == 44+55*101)
Exemple #2
0
    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])
        ec = lod.elementCorrector(world, k, iElementWorldCoarse)
        IPatch = interp.nodalPatchMatrix(ec.iPatchWorldCoarse, ec.NPatchCoarse, NWorldCoarse, NCoarseElement)

        NtPatch = np.prod(ec.NPatchCoarse*NCoarseElement)
        coefficientPatch = coef.coefficientFine(ec.NPatchCoarse, NCoarseElement, np.ones(NtPatch))
        ec.computeCorrectors(coefficientPatch, IPatch)

        correctorSum = reduce(np.add, ec.fsi.correctorsList)
        self.assertTrue(np.allclose(correctorSum, 0))

        ec.computeCoarseQuantities()
        # 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(ec.csi.Kij, axis=0), 0))
        self.assertTrue(np.allclose(np.sum(ec.csi.Kij, axis=1), 0))
        self.assertTrue(np.allclose(np.sum(ec.csi.Kmsij, axis=0), 0))
        self.assertTrue(np.allclose(np.sum(ec.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.convertpCoordinateToIndex(ec.NPatchCoarse-1, ec.iElementPatchCoarse)
        self.assertTrue(np.all(ec.csi.muTPrime[elementTIndex] >= ec.csi.muTPrime))
        self.assertTrue(not np.all(ec.csi.muTPrime[elementTIndex+1] >= ec.csi.muTPrime))
        ec.clearFineQuantities()
Exemple #3
0
    def test_tCoordinates(self):
        NWorld = np.array([5])
        xt = util.tCoordinates(NWorld)
        self.assertTrue(np.isclose(np.max(np.abs(xt.T - np.array([1., 3., 5., 7., 9.])/10)), 0))

        NWorld = np.array([9, 9, 9, 9])
        xt = util.tCoordinates(NWorld)
        ind = util.convertpCoordinateToIndex(NWorld-1, [7, 3, 6, 0])
        self.assertTrue(np.allclose(xt[ind] - np.array([15., 7., 13., 1.])/18., 0))
Exemple #4
0
    def test_pCoordinates(self):
        NWorld = np.array([5])
        xp = util.pCoordinates(NWorld)
        self.assertTrue(np.allclose(xp.T - np.array([0., 1., 2., 3., 4., 5.])/5, 0))

        NWorld = np.array([9, 9, 9, 9])
        xp = util.pCoordinates(NWorld)
        ind = util.convertpCoordinateToIndex(NWorld, [7, 3, 6, 0])
        self.assertTrue(np.allclose(xp[ind] - np.array([7., 3., 6., 0.])/9., 0))
Exemple #5
0
    def computeCorrection(self, ARhsFull=None, MRhsFull=None):
        assert (self.ecList is not None)
        assert (self.origincoef is not None)

        world = self.world
        NCoarseElement = world.NCoarseElement
        NWorldCoarse = world.NWorldCoarse
        NWorldFine = NWorldCoarse * NCoarseElement

        NpFine = np.prod(NWorldFine + 1)

        coefficient = self.origincoef
        IPatchGenerator = self.IPatchGenerator

        localBasis = world.localBasis

        TpIndexMap = util.lowerLeftpIndexMap(NCoarseElement, NWorldFine)
        TpStartIndices = util.pIndexMap(NWorldCoarse - 1, NWorldFine,
                                        NCoarseElement)

        uFine = np.zeros(NpFine)

        NtCoarse = np.prod(world.NWorldCoarse)
        for TInd in range(NtCoarse):
            if self.printLevel > 0:
                print(str(TInd) + ' / ' + str(NtCoarse))

            ecT = self.ecList[TInd]

            coefficientPatch = coefficient.localize(ecT.iPatchWorldCoarse,
                                                    ecT.NPatchCoarse)
            IPatch = IPatchGenerator(ecT.iPatchWorldCoarse, ecT.NPatchCoarse)

            if ARhsFull is not None:
                ARhsList = [ARhsFull[TpStartIndices[TInd] + TpIndexMap]]
            else:
                ARhsList = None

            if MRhsFull is not None:
                MRhsList = [MRhsFull[TpStartIndices[TInd] + TpIndexMap]]
            else:
                MRhsList = None

            correctorT = ecT.computeElementCorrector(coefficientPatch, IPatch,
                                                     ARhsList, MRhsList)[0]

            NPatchFine = ecT.NPatchCoarse * NCoarseElement
            iPatchWorldFine = ecT.iPatchWorldCoarse * NCoarseElement
            patchpIndexMap = util.lowerLeftpIndexMap(NPatchFine, NWorldFine)
            patchpStartIndex = util.convertpCoordinateToIndex(
                NWorldFine, iPatchWorldFine)

            uFine[patchpStartIndex + patchpIndexMap] += correctorT

        return uFine
Exemple #6
0
    def assembleBasisCorrectors(self):
        '''
        Constructs {Q\lambda_x}_x by the sum Q\lambda_x = \sum_T Q_T\lambda_x
        '''

        if self.basis_correctors is not None:
            return self.basis_correctors

        assert (self.ecList is not None)

        world = self.world
        NWorldCoarse = world.NWorldCoarse
        NCoarseElement = world.NCoarseElement
        NWorldFine = NWorldCoarse * NCoarseElement

        NtCoarse = np.prod(NWorldCoarse)
        NpCoarse = np.prod(NWorldCoarse + 1)
        NpFine = np.prod(NWorldFine + 1)

        TpIndexMap = util.lowerLeftpIndexMap(np.ones_like(NWorldCoarse),
                                             NWorldCoarse)
        TpStartIndices = util.lowerLeftpIndexMap(NWorldCoarse - 1,
                                                 NWorldCoarse)

        cols = []
        rows = []
        data = []
        ecList = self.ecList
        for TInd in range(NtCoarse):
            ecT = ecList[TInd]
            assert (ecT is not None)
            assert (hasattr(ecT, 'fsi'))

            NPatchFine = ecT.NPatchCoarse * NCoarseElement
            iPatchWorldFine = ecT.iPatchWorldCoarse * NCoarseElement

            patchpIndexMap = util.lowerLeftpIndexMap(NPatchFine, NWorldFine)
            patchpStartIndex = util.convertpCoordinateToIndex(
                NWorldFine, iPatchWorldFine)

            colsT = TpStartIndices[TInd] + TpIndexMap
            rowsT = patchpStartIndex + patchpIndexMap
            dataT = np.hstack(ecT.fsi.correctorsList)

            cols.extend(np.repeat(colsT, np.size(rowsT)))
            rows.extend(np.tile(rowsT, np.size(colsT)))
            data.extend(dataT)

        basis_correctors = sparse.csc_matrix((data, (rows, cols)),
                                             shape=(NpFine, NpCoarse))

        self.basis_correctors = basis_correctors
        return basis_correctors
Exemple #7
0
    def compute_localized_node_correction(self, b_patch, a_patch, IPatch, ms_basis, prev_fs_sol, node_index):
        '''Compute the fine correctors over the node based patch.

        Compute the correctors, for all z \in V^f(U(\omega_{x,k})):

        a(\phi_x, z) + \tau b(\phi_x, z) = a(\lambda_x, z) + \tau b(\lambda_x, z)
        '''

        world = self.world
        NCoarseElement = world.NCoarseElement
        NPatchCoarse = self.NPatchCoarse
        iPatchWorldCoarse = self.iPatchWorldCoarse

        NPatchFine = NPatchCoarse * NCoarseElement
        NtFine = np.prod(NPatchFine)
        NpFine = np.prod(NPatchFine + 1)

        iPatchWorldFine = iPatchWorldCoarse * NCoarseElement
        patchpIndexMap = util.lowerLeftpIndexMap(NPatchFine, world.NWorldFine)
        patchpStartIndex = util.convertpCoordinateToIndex(world.NWorldFine, iPatchWorldFine)

        patch_indices = patchpStartIndex + patchpIndexMap

        b_patch = b_patch.aFine
        a_patch = a_patch.aFine

        assert (np.size(b_patch) == NtFine)

        S_patch = fem.assemblePatchMatrix(NPatchFine, world.ALocFine, b_patch)
        K_patch = fem.assemblePatchMatrix(NPatchFine, world.ALocFine, a_patch)
        bPatchFull = np.zeros(NpFine)

        if prev_fs_sol is None:
            bPatchFull -= S_patch * ms_basis.toarray()[:, node_index][patch_indices]
        if prev_fs_sol is not None:
            bPatchFull += K_patch * prev_fs_sol.toarray()[:, node_index][patch_indices]

        fs_patch_solution = ritzProjectionToFinePatchWithGivenSaddleSolver(world,
                                                                        self.iPatchWorldCoarse,
                                                                        NPatchCoarse,
                                                                        S_patch + K_patch,
                                                                        [bPatchFull],
                                                                        IPatch,
                                                                        self.saddleSolver)

        fs_solution = np.zeros(world.NpFine)
        fs_solution[patch_indices] += fs_patch_solution[0]

        return fs_solution
Exemple #8
0
    def test_computeErrorIndicator(self):
        NWorldCoarse = np.array([7, 7], dtype='int64')
        NCoarseElement = np.array([10, 10], dtype='int64')
        NWorldFine = NWorldCoarse*NCoarseElement
        NpWorldFine = np.prod(NWorldFine+1)
        NpWorldCoarse = np.prod(NWorldCoarse+1)
        NtWorldFine = np.prod(NWorldCoarse*NCoarseElement)
        NtWorldCoarse = np.prod(NWorldCoarse)

        np.random.seed(0)

        world = World(NWorldCoarse, NCoarseElement)
        d = np.size(NWorldCoarse)
        aBase = np.exp(np.random.rand(NtWorldFine))
        k = np.max(NWorldCoarse)
        iElementWorldCoarse = np.array([3,3])

        rCoarseFirst = 1+3*np.random.rand(NtWorldCoarse)
        coefFirst = coef.coefficientCoarseFactor(NWorldCoarse, NCoarseElement, aBase, rCoarseFirst)
        ec = lod.elementCorrector(world, k, iElementWorldCoarse)
        IPatch = interp.L2ProjectionPatchMatrix(ec.iPatchWorldCoarse, ec.NPatchCoarse, NWorldCoarse, NCoarseElement)
        ec.computeCorrectors(coefFirst, IPatch)
        ec.computeCoarseQuantities()

        # If both rCoarseFirst and rCoarseSecond are equal, the error indicator should be zero
        rCoarseSecond = np.array(rCoarseFirst)
        self.assertTrue(np.isclose(ec.computeErrorIndicator(rCoarseSecond), 0))

        coefSecond = coef.coefficientCoarseFactor(NWorldCoarse, NCoarseElement, aBase, rCoarseSecond)
        self.assertTrue(np.isclose(ec.computeErrorIndicatorFine(coefSecond), 0))
        
        # If rCoarseSecond is not rCoarseFirst, the error indicator should not be zero
        rCoarseSecond = 2*np.array(rCoarseFirst)
        self.assertTrue(ec.computeErrorIndicator(rCoarseSecond) >= 0.1)

        coefSecond = coef.coefficientCoarseFactor(NWorldCoarse, NCoarseElement, aBase, rCoarseSecond)
        self.assertTrue(ec.computeErrorIndicatorFine(coefSecond) >= 0.1)

        # Fine should be smaller than coarse estimate
        self.assertTrue(ec.computeErrorIndicatorFine(coefSecond) < ec.computeErrorIndicator(rCoarseSecond))

        # If rCoarseSecond is different in the element itself, the error
        # indicator should be large
        elementCoarseIndex = util.convertpCoordinateToIndex(NWorldCoarse-1, iElementWorldCoarse)
        rCoarseSecond = np.array(rCoarseFirst)
        rCoarseSecond[elementCoarseIndex] *= 2
        saveForNextTest = ec.computeErrorIndicator(rCoarseSecond)
        self.assertTrue(saveForNextTest >= 0.1)

        coefSecond = coef.coefficientCoarseFactor(NWorldCoarse, NCoarseElement, aBase, rCoarseSecond)
        fineResult = ec.computeErrorIndicatorFine(coefSecond)
        self.assertTrue(fineResult >= 0.1)
        self.assertTrue(ec.computeErrorIndicatorFine(coefSecond) < ec.computeErrorIndicator(rCoarseSecond))

        # A difference in the perifery should be smaller than in the center
        rCoarseSecond = np.array(rCoarseFirst)
        rCoarseSecond[0] *= 2
        self.assertTrue(saveForNextTest > ec.computeErrorIndicator(rCoarseSecond))

        # Again, but closer
        rCoarseSecond = np.array(rCoarseFirst)
        rCoarseSecond[elementCoarseIndex-1] *= 2
        self.assertTrue(saveForNextTest > ec.computeErrorIndicator(rCoarseSecond))