예제 #1
0
    def test_examples(self):
        NFrom = np.array([1, 1, 1])
        NTo = np.array([1, 9, 1])
        NStep = np.array([1, 1, 1])
        self.assertTrue(np.all(util.pIndexMap(NFrom, NTo, NStep) == [0, 1, 2, 3, 20, 21, 22, 23]))

        NFrom = np.array([1, 1, 1])
        NTo = np.array([9, 1, 1])
        NStep = np.array([9, 1, 1])
        self.assertTrue(np.all(util.pIndexMap(NFrom, NTo, NStep) == [0, 9, 10, 19, 20, 29, 30, 39]))
예제 #2
0
    def test_trivials1d(self):
        # 1D
        NFrom = np.array([1])
        NTo = np.array([1])
        NStep = np.array([1])
        self.assertTrue(np.all(util.pIndexMap(NFrom, NTo, NStep) == [0, 1]))

        NFrom = np.array([0])
        NTo = np.array([1])
        NStep = np.array([1])
        self.assertTrue(np.all(util.pIndexMap(NFrom, NTo, NStep) == [0]))
예제 #3
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
예제 #4
0
def projectSaturation(NWorldCoarse, NCoarseElement, sFine):
    NWorldFine = NWorldCoarse * NCoarseElement

    TIndexMap = util.lowerLeftpIndexMap(NCoarseElement - 1, NWorldFine - 1)
    TStartIndices = util.pIndexMap(NWorldCoarse - 1, NWorldFine - 1,
                                   NCoarseElement)

    NtCoarse = np.prod(NWorldCoarse)
    sCoarse = np.zeros(NtCoarse)
    for TInd in range(NtCoarse):
        sCoarse[TInd] = np.mean(sFine[TStartIndices[TInd] + TIndexMap])

    return sCoarse
예제 #5
0
def computeErrorIndicatorCoarse_helmholtz(patch, muTPrime, aPatchOld,
                                          aPatchNew):
    ''' Compute the coarse error idicator E(T) with explicit value of AOld and ANew.

    This requires muTPrime from CSI and the new and old coefficient.
    '''

    while callable(muTPrime):
        muTPrime = muTPrime()

    while callable(aPatchOld):
        aPatchOld = aPatchOld()

    while callable(aPatchNew):
        aPatchNew = aPatchNew()

    aOld = aPatchOld
    aNew = aPatchNew

    world = patch.world
    NPatchCoarse = patch.NPatchCoarse
    NCoarseElement = world.NCoarseElement
    NPatchFine = NPatchCoarse * NCoarseElement
    iElementPatchCoarse = patch.iElementPatchCoarse

    elementCoarseIndex = util.convertpCoordIndexToLinearIndex(
        NPatchCoarse - 1, iElementPatchCoarse)

    TPrimeFinetStartIndices = util.pIndexMap(NPatchCoarse - 1, NPatchFine - 1,
                                             NCoarseElement)
    TPrimeFinetIndexMap = util.lowerLeftpIndexMap(NCoarseElement - 1,
                                                  NPatchFine - 1)

    TPrimeIndices = np.add.outer(TPrimeFinetStartIndices, TPrimeFinetIndexMap)
    aTPrime = aNew[TPrimeIndices]
    aOldTPrime = aOld[TPrimeIndices]

    deltaMaxTPrime = np.max(np.abs(aTPrime - aOldTPrime), axis=1)

    epsilonTSquare = np.sum((deltaMaxTPrime**2) * muTPrime)

    return np.sqrt(epsilonTSquare)
예제 #6
0
def performTPrimeLoop_helmholtz(patch, lambdasList, correctorsList, aPatch,
                                kPatch, k2Patch, accumulate):

    while callable(aPatch):
        aPatch = aPatch()

    while callable(kPatch):
        kPatch = kPatch()

    while callable(k2Patch):
        k2Patch = k2Patch()

    world = patch.world
    NCoarseElement = world.NCoarseElement
    NPatchCoarse = patch.NPatchCoarse
    NPatchFine = NPatchCoarse * NCoarseElement

    NTPrime = np.prod(NPatchCoarse)
    NpPatchCoarse = np.prod(NPatchCoarse + 1)

    d = np.size(NPatchCoarse)

    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)

    lambdas = np.column_stack(lambdasList)
    numLambdas = len(lambdasList)

    TPrimeCoarsepStartIndices = util.lowerLeftpIndexMap(
        NPatchCoarse - 1, NPatchCoarse)
    TPrimeCoarsepIndexMap = util.lowerLeftpIndexMap(np.ones_like(NPatchCoarse),
                                                    NPatchCoarse)

    TPrimeFinetStartIndices = util.pIndexMap(NPatchCoarse - 1, NPatchFine - 1,
                                             NCoarseElement)
    TPrimeFinetIndexMap = util.lowerLeftpIndexMap(NCoarseElement - 1,
                                                  NPatchFine - 1)

    TPrimeFinepStartIndices = util.pIndexMap(NPatchCoarse - 1, NPatchFine,
                                             NCoarseElement)
    TPrimeFinepIndexMap = util.lowerLeftpIndexMap(NCoarseElement, NPatchFine)

    TInd = util.convertpCoordIndexToLinearIndex(NPatchCoarse - 1,
                                                patch.iElementPatchCoarse)

    QPatch = np.column_stack(correctorsList)

    # global boundary
    bdMapWorld = world.boundaryConditions == 1

    for (TPrimeInd,
         TPrimeCoarsepStartIndex,
         TPrimeFinetStartIndex,
         TPrimeFinepStartIndex) \
         in zip(np.arange(NTPrime),
                TPrimeCoarsepStartIndices,
                TPrimeFinetStartIndices,
                TPrimeFinepStartIndices):

        aTPrime = aPatch[TPrimeFinetStartIndex + TPrimeFinetIndexMap]
        kTPrime = kPatch[TPrimeFinetStartIndex + TPrimeFinetIndexMap]
        k2TPrime = k2Patch[TPrimeFinetStartIndex + TPrimeFinetIndexMap]
        KTPrime = fem.assemblePatchMatrix(NCoarseElement, ALocFine, aTPrime)
        MTPrime = fem.assemblePatchMatrix(NCoarseElement, MLocFine, k2TPrime)
        L2TPrime = fem.assemblePatchMatrix(NCoarseElement, MLocFine)

        # boundary on element
        bdMapElement = np.zeros([d, 2], dtype='bool')

        iElementTPrime = patch.iPatchWorldCoarse + util.convertpLinearIndexToCoordIndex(
            NPatchCoarse - 1, TPrimeInd)

        inheritElement0 = iElementTPrime == 0
        inheritElement1 = (iElementTPrime + np.ones(d)) == world.NWorldCoarse

        bdMapElement[inheritElement0, 0] = bdMapWorld[inheritElement0, 0]
        bdMapElement[inheritElement1, 1] = bdMapWorld[inheritElement1, 1]

        BdTPrime = fem.assemblePatchBoundaryMatrix(NCoarseElement, BdLocFine,
                                                   kTPrime, bdMapElement)
        P = lambdas
        Q = QPatch[TPrimeFinepStartIndex + TPrimeFinepIndexMap, :]
        _KTPrimeij = np.dot(P.T, KTPrime * Q)
        TPrimei = TPrimeCoarsepStartIndex + TPrimeCoarsepIndexMap

        _MTPrimeij = np.dot(P.T, MTPrime * Q)
        _BdTPrimeij = np.dot(P.T, BdTPrime * Q)

        CTPrimeij = np.dot(Q.T, L2TPrime * Q)
        BTPrimeij = np.dot(P.T, L2TPrime * Q)

        accumulate(TPrimeInd, TPrimei, P, Q, KTPrime, _KTPrimeij, MTPrime,
                   BdTPrime, _MTPrimeij, _BdTPrimeij, L2TPrime, CTPrimeij,
                   BTPrimeij)
예제 #7
0
 def test_trivials3d(self):
     # 3D
     NFrom = np.array([1, 1, 1])
     NTo = np.array([2, 2, 2])
     NStep = np.array([0, 0, 0])
     self.assertTrue(np.all(util.pIndexMap(NFrom, NTo, NStep) == 0))