def _calcValueIn(self, eps, P):
            alpha = self._getArray().copy()
            
	    inline._runInline("""
		if (fabs(P[i]) < eps) {
		    P[i] = eps;
		}
		
		alpha[i] = 0.5;
		
		if (P[i] > 10.) {
		    alpha[i] = (P[i] - 1.) / P[i];
		} else if (10. >= P[i] && P[i] > eps) {
		    double	tmp = (1. - P[i] / 10.);
		    double	tmpSqr = tmp * tmp;
		    alpha[i] = ((P[i] - 1.) + tmpSqr*tmpSqr*tmp) / P[i];
		} else if (-eps > P[i] && P[i] >= -10.) {
		    double	tmp = (1. + P[i] / 10.);
		    double	tmpSqr = tmp * tmp;
		    alpha[i] = (tmpSqr*tmpSqr*tmp - 1.) / P[i];
		} else if (P[i] < -10.) {
		    alpha[i] = -1. / P[i];
		}
	    """,
	    alpha = alpha, eps = eps, P = P,
	    ni = self.mesh._getNumberOfFaces()
	    )

            return self._makeValue(value = alpha)
Example #2
0
        def _calcValue(self):
            eps = self.eps
            P = self.P.numericValue
            alpha = self._array.copy()

            inline._runInline("""
                if (fabs(P[i]) < eps) {
                    P[i] = eps;
                }

                alpha[i] = 0.5;

                if (P[i] > 10.) {
                    alpha[i] = (P[i] - 1.) / P[i];
                } else if (10. >= P[i] && P[i] > eps) {
                    double	tmp = (1. - P[i] / 10.);
                    double	tmpSqr = tmp * tmp;
                    alpha[i] = ((P[i] - 1.) + tmpSqr*tmpSqr*tmp) / P[i];
                } else if (-eps > P[i] && P[i] >= -10.) {
                    double	tmp = (1. + P[i] / 10.);
                    double	tmpSqr = tmp * tmp;
                    alpha[i] = (tmpSqr*tmpSqr*tmp - 1.) / P[i];
                } else if (P[i] < -10.) {
                    alpha[i] = -1. / P[i];
                }
            """,
            alpha = alpha, eps = eps, P = P,
            ni = len(P.flat)
            )

            return self._makeValue(value = alpha)
Example #3
0
        def _areaProjections(self):
            areaProjections = numerix.zeros((2, self.numberOfFaces), 'd')

            inline._runInline(
                """
                              if (i < nx) {
                                  areaProjections[i + 1 * ni] = faceAreas[i] * faceNormals[i + 1 * ni];
                              } else if (i < Nhor) {
                                  areaProjections[i + 1 * ni] = faceAreas[i] * faceNormals[i + 1 * ni];
                              } else if ( (i - Nhor) % (nx + 1) == 0 ) {
                                  areaProjections[i + 0 * ni] = faceAreas[i] * faceNormals[i + 0 * ni];
                              } else {
                                  areaProjections[i + 0 * ni] = faceAreas[i] * faceNormals[i + 0 * ni];
                              }
                              """,
                dx=float(self.dx),  # horrible hack to get around
                dy=float(
                    self.dy),  # http://www.scipy.org/scipy/scipy/ticket/496
                nx=self.nx,
                Nhor=self.numberOfHorizontalFaces,
                areaProjections=areaProjections,
                ni=self.numberOfFaces,
                faceNormals=self.faceNormals,
                faceAreas=self._faceAreas)

            return areaProjections
Example #4
0
        def _explicitBuildMatrixInline_(self, oldArray, id1, id2, b, coeffMatrix, mesh, interiorFaces, dt, weight):

            oldArrayId1, oldArrayId2 = self._getOldAdjacentValues(oldArray, id1, id2, dt)
            coeff = numerix.array(self._getGeomCoeff(oldArray))
            Nfac = mesh.numberOfFaces

            cell1Diag = numerix.zeros((Nfac,), 'd')
            cell1Diag[:] = weight['cell 1 diag']
            cell1OffDiag = numerix.zeros((Nfac,), 'd')
            cell1OffDiag[:] = weight['cell 1 offdiag']
            cell2Diag = numerix.zeros((Nfac,), 'd')
            cell2Diag[:] = weight['cell 2 diag']
            cell2OffDiag = numerix.zeros((Nfac,), 'd')
            cell2OffDiag[:] = weight['cell 2 offdiag']

            inline._runInline("""
                long int faceID = faceIDs[i];
                long int cellID1 = id1[i];
                long int cellID2 = id2[i];

                b[cellID1] += -coeff[faceID] * (cell1Diag[faceID] * oldArrayId1[i] + cell1OffDiag[faceID] * oldArrayId2[i]);
                b[cellID2] += -coeff[faceID] * (cell2Diag[faceID] * oldArrayId2[i] + cell2OffDiag[faceID] * oldArrayId1[i]);
            """, oldArrayId1 = numerix.array(oldArrayId1),
                oldArrayId2 = numerix.array(oldArrayId2),
                id1 = id1,
                id2 = id2,
                b = b,
                cell1Diag = cell1Diag,
                cell1OffDiag = cell1OffDiag,
                cell2Diag = cell2Diag,
                cell2OffDiag = cell2OffDiag,
                coeff = coeff,
                faceIDs = interiorFaces,
                ni = len(interiorFaces))
Example #5
0
    def _calcValueInline(self):
        
        NCells = self.mesh.numberOfCells
        ids = self.mesh.cellFaceIDs

        val = self._array.copy()

        inline._runInline("""
        int i;

        for(i = 0; i < numberOfCells; i++)
          {
          int j;
          value[i] = 0.;
          for(j = 0; j < numberOfCellFaces; j++)
            {
              // cellFaceIDs can be masked, which caused subtle and 
              // unreproduceable problems on OS X (who knows why not elsewhere)
              long id = ids[i + j * numberOfCells];
              if (id >= 0) { 
                  value[i] += orientations[i + j * numberOfCells] * faceVariable[id];
              }
            }
            value[i] = value[i] / cellVolume[i];
          }
          """,
                          numberOfCellFaces = self.mesh._maxFacesPerCell,
                          numberOfCells = NCells,
                          faceVariable = self.faceVariable.numericValue,
                          ids = numerix.array(ids),
                          value = val,
                          orientations = numerix.array(self.mesh._cellToFaceOrientations),
                          cellVolume = numerix.array(self.mesh.cellVolumes))
        
        return self._makeValue(value = val)
Example #6
0
    def _explicitBuildMatrixIn(self, oldArray, id1, id2, b, weightedStencilCoeff, mesh, interiorFaces, dt, weight):

        oldArrayId1, oldArrayId2 = self._getOldAdjacentValues(oldArray, id1, id2, dt)
        coeff = numerix.array(self._getGeomCoeff(mesh))
        Nfac = mesh._getNumberOfFaces()

        cell1Diag = numerix.zeros((Nfac,),'d')
        cell1Diag[:] = weight['cell 1 diag']
        cell1OffDiag = numerix.zeros((Nfac,),'d')
        cell1OffDiag[:] = weight['cell 1 offdiag']
        cell2Diag = numerix.zeros((Nfac,),'d')
        cell2Diag[:] = weight['cell 2 diag']
        cell2OffDiag = numerix.zeros((Nfac,),'d')
        cell2OffDiag[:] = weight['cell 2 offdiag']
        
        inline._runInline("""
            long int faceID = faceIDs[i];
            long int cellID1 = id1[i];
            long int cellID2 = id2[i];
            
            b[cellID1] += -coeff[faceID] * (cell1Diag[faceID] * oldArrayId1[i] + cell1OffDiag[faceID] * oldArrayId2[i]);
            b[cellID2] += -coeff[faceID] * (cell2Diag[faceID] * oldArrayId2[i] + cell2OffDiag[faceID] * oldArrayId1[i]);
        """,oldArrayId1 = numerix.array(oldArrayId1),
            oldArrayId2 = numerix.array(oldArrayId2),
            id1 = id1,
            id2 = id2,
            b = b,
            cell1Diag = cell1Diag,
            cell1OffDiag = cell1OffDiag,
            cell2Diag = cell2Diag,
            cell2OffDiag = cell2OffDiag,
            coeff = coeff,
            faceIDs = interiorFaces,
            ni = len(interiorFaces))
Example #7
0
        def _calcValue(self):
            eps = self.eps
            P = self.P.numericValue
            alpha = self._array.copy()

            inline._runInline("""
                if (fabs(P[i]) < eps) {
                    P[i] = eps;
                }

                alpha[i] = 0.5;

                if (P[i] > 10.) {
                    alpha[i] = (P[i] - 1.) / P[i];
                } else if (10. >= P[i] && P[i] > eps) {
                    double	tmp = (1. - P[i] / 10.);
                    double	tmpSqr = tmp * tmp;
                    alpha[i] = ((P[i] - 1.) + tmpSqr*tmpSqr*tmp) / P[i];
                } else if (-eps > P[i] && P[i] >= -10.) {
                    double	tmp = (1. + P[i] / 10.);
                    double	tmpSqr = tmp * tmp;
                    alpha[i] = (tmpSqr*tmpSqr*tmp - 1.) / P[i];
                } else if (P[i] < -10.) {
                    alpha[i] = -1. / P[i];
                }
            """,
                              alpha=alpha,
                              eps=eps,
                              P=P,
                              ni=len(P.flat))

            return self._makeValue(value=alpha)
Example #8
0
def _putAddIn(vector, ids, additionVector):
    from fipy.tools import inline
    inline._runInline("""
        int ID = ids[i];
	vector[ID] += additionVector[i];
    """,
    vector=vector, ids=ids, additionVector=numerix.array(additionVector),
    ni = len(ids.flat))
Example #9
0
 def putAdd(vector, ids, additionVector):
     """ This is a temporary replacement for Numeric.put as it was not doing
     what we thought it was doing.
     """
     inline._runInline("""
                           int ID = ids[i];
                           vector[ID] += additionVector[i];
                       """,
                       vector=vector, 
                       ids=ids, 
                       additionVector=numerix.array(additionVector),
     ni = len(ids.flat))
 def putAdd(vector, ids, additionVector):
     """ This is a temporary replacement for Numeric.put as it was not doing
     what we thought it was doing.
     """
     inline._runInline("""
                           int ID = ids[i];
                           vector[ID] += additionVector[i];
                       """,
                       vector=vector,
                       ids=ids,
                       additionVector=numerix.array(additionVector),
                       ni=len(ids.flat))
Example #11
0
    def sqrtDot(a1, a2):
        """Return array of square roots of vector dot-products
        for arrays a1 and a2 of vectors v1 and v2

        Usually used with v1==v2 to return magnitude of v1.
        """
        from fipy.tools.dimensions import physicalField
        unit1 = unit2 = physicalField._unity

        def dimensionlessUnmasked(a):
            unit = physicalField._unity
            mask = False
            if _isPhysical(a):
                unit = a.inBaseUnits().unit
                a = a.numericValue
            if MA.isMaskedArray(a):
                mask = a.mask
                a = a.filled(fill_value=1)
            if not a.flags['C_CONTIGUOUS']:
                a = a.copy('C')
            return (a, unit, mask)

        a1, unit1, mask1 = dimensionlessUnmasked(a1)
        a2, unit2, mask2 = dimensionlessUnmasked(a2)

        NJ, ni = NUMERIX.shape(a1)
        result1 = NUMERIX.zeros((ni, ), 'd')

        inline._runInline("""
            int j;
            result1[i] = 0.;
            for (j = 0; j < NJ; j++)
            {
                result1[i] += a1[i + j * ni] * a2[i + j * ni];
            }
            result1[i] = sqrt(result1[i]);
        """,
                          result1=result1,
                          a1=a1,
                          a2=a2,
                          ni=ni,
                          NJ=NJ)

        if NUMERIX.any(mask1) or NUMERIX.any(mask2):
            result1 = MA.array(result1, mask=NUMERIX.logical_or(mask1, mask2))

        if unit1 != physicalField._unity or unit2 != physicalField._unity:
            from fipy.tools.dimensions.physicalField import PhysicalField
            result1 = PhysicalField(value=result, unit=(unit1 * unit2)**0.5)

        return result1
Example #12
0
 def  _calcValue_(self, alpha, id1, id2):
     val = self._array.copy()
     
     inline._runInline(self.modIn + """
     int ID1 = id1[i];
     int ID2 = id2[i];
     double cell2 = var[ID2];
     val[i] = mod(cell2 - var[ID1]) * alpha[i] + var[ID1];
     """,var = self.var.numericValue,
         val = val, 
         alpha = alpha,
         id1 = id1, id2 = id2,
         ni = self.mesh.numberOfFaces)
         
     return self._makeValue(value = val)
Example #13
0
        def  _calcValue_(self, alpha, id1, id2):
            val = self._array.copy()

            inline._runInline(self.modIn + """
            int ID1 = id1[i];
            int ID2 = id2[i];
            double cell2 = var[ID2];
            val[i] = mod(cell2 - var[ID1]) * alpha[i] + var[ID1];
            """, var = self.var.numericValue,
                val = val,
                alpha = alpha,
                id1 = id1, id2 = id2,
                ni = self.mesh.numberOfFaces)

            return self._makeValue(value = val)
        def _calcValueIn(self, P):
            alpha = self._getArray().copy()
            inline._runInline("""
                alpha[i] = 0.5;
                
                if (P[i] > 0.) {
                    alpha[i] = 1.;
                } else {
                    alpha[i] = 0.;
                }
            """,
            alpha = alpha, P = P,
            ni = self.mesh._getNumberOfFaces()
            )

            return self._makeValue(value = alpha)
        def _calcValue(self):
            P  = self.P.numericValue
            alpha = self._array.copy()

            inline._runInline("""
                alpha[i] = 0.5;

                if (P[i] > 0.) {
                    alpha[i] = 1.;
                } else {
                    alpha[i] = 0.;
                }
            """,
            alpha=alpha, P=P,
            ni = len(P.flat))

            return self._makeValue(value=alpha)
Example #16
0
File: grid2D.py Project: regmi/fipy
    def _createCellsIn(self):
        cellFaceIDs = numerix.zeros((4, self.nx * self.ny))
        
        inline._runInline("""
            int ID = j * ni + i;
            int NCELLS = ni * nj;
            cellFaceIDs[ID + 0 * NCELLS] = ID;
            cellFaceIDs[ID + 2 * NCELLS] = cellFaceIDs[ID + 0 * NCELLS] + ni;
            cellFaceIDs[ID + 3 * NCELLS] = horizontalFaces + ID + j;
            cellFaceIDs[ID + 1 * NCELLS] = cellFaceIDs[ID + 3 * NCELLS] + 1;
        """,
        horizontalFaces=self.numberOfHorizontalFaces,
        cellFaceIDs=cellFaceIDs,
        ni=self.nx,
        nj=self.ny)

        return cellFaceIDs
Example #17
0
    def sqrtDot(a1, a2):
        """Return array of square roots of vector dot-products
        for arrays a1 and a2 of vectors v1 and v2

        Usually used with v1==v2 to return magnitude of v1.
        """
        from fipy.tools.dimensions import physicalField
        unit1 = unit2 = physicalField._unity

        def dimensionlessUnmasked(a):
            unit = physicalField._unity
            mask = False
            if _isPhysical(a):
                unit = a.inBaseUnits().unit
                a = a.numericValue
            if MA.isMaskedArray(a):
                mask = a.mask
                a = a.filled(fill_value=1)
            if not a.flags['C_CONTIGUOUS']:
                a = a.copy('C')
            return (a, unit, mask)

        a1, unit1, mask1 = dimensionlessUnmasked(a1)
        a2, unit2, mask2 = dimensionlessUnmasked(a2)

        NJ, ni = NUMERIX.shape(a1)
        result1 = NUMERIX.zeros((ni,), 'd')

        inline._runInline("""
            int j;
            result1[i] = 0.;
            for (j = 0; j < NJ; j++)
            {
                result1[i] += a1[i + j * ni] * a2[i + j * ni];
            }
            result1[i] = sqrt(result1[i]);
        """, result1=result1, a1=a1, a2=a2, ni=ni, NJ=NJ)

        if NUMERIX.any(mask1) or NUMERIX.any(mask2):
            result1 = MA.array(result1, mask=NUMERIX.logical_or(mask1, mask2))

        if unit1 != physicalField._unity or unit2 != physicalField._unity:
            from fipy.tools.dimensions.physicalField import PhysicalField
            result1 = PhysicalField(value=result, unit=(unit1 * unit2)**0.5)

        return result1
Example #18
0
        def _calcValue(self):
            P = self.P.numericValue
            alpha = self._array.copy()

            inline._runInline("""
                alpha[i] = 0.5;

                if (P[i] > 0.) {
                    alpha[i] = 1.;
                } else {
                    alpha[i] = 0.;
                }
            """,
                              alpha=alpha,
                              P=P,
                              ni=len(P.flat))

            return self._makeValue(value=alpha)
Example #19
0
        def faceCellIDs(self):
            faceCellIDs = numerix.zeros((2, self.numberOfFaces), 'l')
            mask = numerix.zeros((2, self.numberOfFaces), 'l')

            inline._runInline("""
                int ID = j * ni + i;
                int rowlength = ni * nj + Nhor + nj;

                faceCellIDs[ID + 0 * rowlength] = ID - ni;
                faceCellIDs[ID + 1 * rowlength] = ID;

                faceCellIDs[ID + Nhor + j + 0 * rowlength] = ID - 1;
                faceCellIDs[ID + Nhor + j + 1 * rowlength] = ID;

                if (j == 0) {
                    faceCellIDs[ID + 0 * rowlength] = ID;
                    mask[ID + 1 * rowlength] = 1;
                }

                if (j == nj - 1) {
                    faceCellIDs[ID + ni + 0 * rowlength] = ID;
                    mask[ID + ni + 1 * rowlength] = 1;
                }

                if (i == 0) {
                    faceCellIDs[ID + Nhor + j + 0 * rowlength] = ID;
                    mask[ID + Nhor + j + 1 * rowlength] = 1;
                }

                if ( i == ni - 1 ) {
                    faceCellIDs[ID + Nhor + j + 1 + 0 * rowlength] = ID;
                    mask[ID + Nhor + j + 1 + 1 * rowlength] = 1;
                }
            """,
            Nhor=self.numberOfHorizontalFaces,
            mask=mask,
            faceCellIDs=faceCellIDs,
            ni=self.nx,
            nj=self.ny)

            return MA.masked_where(mask, faceCellIDs)
Example #20
0
        def faceCellIDs(self):
            faceCellIDs = numerix.zeros((2, self.numberOfFaces), 'l')
            mask = numerix.zeros((2, self.numberOfFaces), 'l')

            inline._runInline("""
                int ID = j * ni + i;
                int rowlength = ni * nj + Nhor + nj;

                faceCellIDs[ID + 0 * rowlength] = ID - ni;
                faceCellIDs[ID + 1 * rowlength] = ID;

                faceCellIDs[ID + Nhor + j + 0 * rowlength] = ID - 1;
                faceCellIDs[ID + Nhor + j + 1 * rowlength] = ID;

                if (j == 0) {
                    faceCellIDs[ID + 0 * rowlength] = ID;
                    mask[ID + 1 * rowlength] = 1;
                }

                if (j == nj - 1) {
                    faceCellIDs[ID + ni + 0 * rowlength] = ID;
                    mask[ID + ni + 1 * rowlength] = 1;
                }

                if (i == 0) {
                    faceCellIDs[ID + Nhor + j + 0 * rowlength] = ID;
                    mask[ID + Nhor + j + 1 * rowlength] = 1;
                }

                if ( i == ni - 1 ) {
                    faceCellIDs[ID + Nhor + j + 1 + 0 * rowlength] = ID;
                    mask[ID + Nhor + j + 1 + 1 * rowlength] = 1;
                }
            """,
                              Nhor=self.numberOfHorizontalFaces,
                              mask=mask,
                              faceCellIDs=faceCellIDs,
                              ni=self.nx,
                              nj=self.ny)

            return MA.masked_where(mask, faceCellIDs)
Example #21
0
    def _buildMatrixIn(self, L, oldArray, b, dt, coeffVectors):
        N = oldArray.getMesh().getNumberOfCells()
        updatePyArray = numerix.zeros((N),'d')

        inline._runInline("""
            b[i] += oldArray[i] * oldCoeff[i] / dt;
            b[i] += bCoeff[i];
            updatePyArray[i] += newCoeff[i] / dt;
            updatePyArray[i] += diagCoeff[i];
        """,b=b,
            oldArray=oldArray.getNumericValue(),
##            oldArray=numerix.array(oldArray),
            oldCoeff=numerix.array(coeffVectors['old value']),
            bCoeff=numerix.array(coeffVectors['b vector']),
            newCoeff=numerix.array(coeffVectors['new value']),
            diagCoeff=numerix.array(coeffVectors['diagonal']),
            updatePyArray=updatePyArray,
            ni=len(updatePyArray),
            dt=dt)

        L.addAtDiagonal(updatePyArray)
Example #22
0
        def createCells(nx, ny, numFaces, numHorizFaces, numVertCols):
            """
            cells = (f1, f2, f3, f4) going anticlock wise.
            f1 etc. refer to the faces
            """ 
            cellFaceIDs = numerix.zeros((4, nx * ny), 'l')
            
            inline._runInline("""
                int ID = j * ni + i;
                int NCELLS = ni * nj;
                cellFaceIDs[ID + 0 * NCELLS] = ID;
                cellFaceIDs[ID + 2 * NCELLS] = cellFaceIDs[ID + 0 * NCELLS] + ni;
                cellFaceIDs[ID + 3 * NCELLS] = horizontalFaces + ID + j;
                cellFaceIDs[ID + 1 * NCELLS] = cellFaceIDs[ID + 3 * NCELLS] + 1;
            """,
            horizontalFaces=numHorizFaces,
            cellFaceIDs=cellFaceIDs,
            ni=nx,
            nj=ny)

            return cellFaceIDs
Example #23
0
        def createCells(nx, ny, numFaces, numHorizFaces, numVertCols):
            """
            cells = (f1, f2, f3, f4) going anticlock wise.
            f1 etc. refer to the faces
            """
            cellFaceIDs = numerix.zeros((4, nx * ny), 'l')

            inline._runInline("""
                int ID = j * ni + i;
                int NCELLS = ni * nj;
                cellFaceIDs[ID + 0 * NCELLS] = ID;
                cellFaceIDs[ID + 2 * NCELLS] = cellFaceIDs[ID + 0 * NCELLS] + ni;
                cellFaceIDs[ID + 3 * NCELLS] = horizontalFaces + ID + j;
                cellFaceIDs[ID + 1 * NCELLS] = cellFaceIDs[ID + 3 * NCELLS] + 1;
            """,
                              horizontalFaces=numHorizFaces,
                              cellFaceIDs=cellFaceIDs,
                              ni=nx,
                              nj=ny)

            return cellFaceIDs
Example #24
0
    def _getAreaProjectionsIn(self):
        areaProjections = numerix.zeros((2, self.numberOfFaces), 'd')

        inline._runInline("""
            if (i < nx) {
                areaProjections[i + 1 * ni] = -dx;
            } else if (i < Nhor) {
                areaProjections[i + 1 * ni] = dx;
            } else if ( (i - Nhor) % (nx + 1) == 0 ) {
                areaProjections[i + 0 * ni] = -dy;
            } else {
                areaProjections[i + 0 * ni] = dy;
           }
        """,
        dx = float(self.dx), # horrible hack to get around
        dy = float(self.dy), # http://www.scipy.org/scipy/scipy/ticket/496
        nx = self.nx,
        Nhor = self.numberOfHorizontalFaces,
        areaProjections = areaProjections,
        ni = self.numberOfFaces)

        return areaProjections
Example #25
0
        def _adjacentCellIDs(self):
            faceCellIDs0 =  numerix.zeros(self.numberOfFaces, 'l')
            faceCellIDs1 =  numerix.zeros(self.numberOfFaces, 'l')

            inline._runInline("""
                int ID = j * ni + i;

                faceCellIDs0[ID] = ID - ni;
                faceCellIDs1[ID] = ID;

                faceCellIDs0[ID + Nhor + j] = ID - 1;
                faceCellIDs1[ID + Nhor + j] = ID;

                if (j == 0) {
                    faceCellIDs0[ID] = ID;
                }

                if (j == nj - 1) {
                    faceCellIDs0[ID + ni] = ID;
                    faceCellIDs1[ID + ni] = ID;
                }

                if (i == 0) {
                    faceCellIDs0[ID + Nhor + j] = ID;
                }

                if ( i == ni - 1 ) {
                    faceCellIDs0[ID + Nhor + j + 1] = ID;
                    faceCellIDs1[ID + Nhor + j + 1] = ID;
                }

            """,
            Nhor=self.numberOfHorizontalFaces,
            faceCellIDs0=faceCellIDs0,
            faceCellIDs1=faceCellIDs1,
            ni=self.nx,
            nj=self.ny)

            return (faceCellIDs0, faceCellIDs1)
Example #26
0
        def _adjacentCellIDs(self):
            faceCellIDs0 = numerix.zeros(self.numberOfFaces, 'l')
            faceCellIDs1 = numerix.zeros(self.numberOfFaces, 'l')

            inline._runInline("""
                int ID = j * ni + i;

                faceCellIDs0[ID] = ID - ni;
                faceCellIDs1[ID] = ID;

                faceCellIDs0[ID + Nhor + j] = ID - 1;
                faceCellIDs1[ID + Nhor + j] = ID;

                if (j == 0) {
                    faceCellIDs0[ID] = ID;
                }

                if (j == nj - 1) {
                    faceCellIDs0[ID + ni] = ID;
                    faceCellIDs1[ID + ni] = ID;
                }

                if (i == 0) {
                    faceCellIDs0[ID + Nhor + j] = ID;
                }

                if ( i == ni - 1 ) {
                    faceCellIDs0[ID + Nhor + j + 1] = ID;
                    faceCellIDs1[ID + Nhor + j + 1] = ID;
                }

            """,
                              Nhor=self.numberOfHorizontalFaces,
                              faceCellIDs0=faceCellIDs0,
                              faceCellIDs1=faceCellIDs1,
                              ni=self.nx,
                              nj=self.ny)

            return (faceCellIDs0, faceCellIDs1)
        def _calcValue_(self, alpha, id1, id2):
            val = self._array.copy()

            inline._runInline("""
                int ID1 = id1[i];
                int ID2 = id2[i];
                double	cell1 = var[ID1];
                double	cell2 = var[ID2];

                if (cell1 < 0 || cell2 < 0) {
                    val[i] = 0;
                } else {
                    val[i] = diffusionCoeff;
                }
            """,
                              var=numerix.array(self.var),
                              val=val,
                              id1=id1,
                              id2=id2,
                              diffusionCoeff=self.diffusionCoeff,
                              ni=self.mesh.numberOfFaces)

            return self._makeValue(value=val)
Example #28
0
    def _buildMatrixInline_(self, L, oldArray, b, dt, coeffVectors):
        oldArray = oldArray.value.ravel()
        N = len(oldArray)
        updatePyArray = numerix.zeros((N),'d')

        dt = self._checkDt(dt)

        inline._runInline("""
            b[i] += oldArray[i] * oldCoeff[i] / dt;
            b[i] += bCoeff[i];
            updatePyArray[i] += newCoeff[i] / dt;
            updatePyArray[i] += diagCoeff[i];
        """,b=b,
            oldArray=oldArray,
            oldCoeff=coeffVectors['old value'].ravel(),
            bCoeff=coeffVectors['b vector'].ravel(),
            newCoeff=coeffVectors['new value'].ravel(),
            diagCoeff=coeffVectors['diagonal'].ravel(),
            updatePyArray=updatePyArray,
            ni=len(updatePyArray),
            dt=dt)

        L.addAtDiagonal(updatePyArray)
Example #29
0
    def _buildMatrixInline_(self, L, oldArray, b, dt, coeffVectors):
        oldArray = oldArray.value.ravel()
        N = len(oldArray)
        updatePyArray = numerix.zeros((N), 'd')

        dt = self._checkDt(dt)

        inline._runInline("""
            b[i] += oldArray[i] * oldCoeff[i] / dt;
            b[i] += bCoeff[i];
            updatePyArray[i] += newCoeff[i] / dt;
            updatePyArray[i] += diagCoeff[i];
        """, b=b,
            oldArray=oldArray,
            oldCoeff=coeffVectors['old value'].ravel(),
            bCoeff=coeffVectors['b vector'].ravel(),
            newCoeff=coeffVectors['new value'].ravel(),
            diagCoeff=coeffVectors['diagonal'].ravel(),
            updatePyArray=updatePyArray,
            ni=len(updatePyArray),
            dt=dt)

        L.addAtDiagonal(updatePyArray)
        def _calcValue_(self, alpha, id1, id2):
            val = self._array.copy()
            
            inline._runInline("""
                int ID1 = id1[i];
                int ID2 = id2[i];
                double	cell1 = var[ID1];
                double	cell2 = var[ID2];

                if (cell1 < 0 || cell2 < 0) {
                    val[i] = 0;
                } else {
                    val[i] = diffusionCoeff;
                }
            """,
            var = numerix.array(self.var),
            val = val,
            id1 = id1, id2 = id2,
            diffusionCoeff = self.diffusionCoeff,
            ni = self.mesh.numberOfFaces
            )
     
            return self._makeValue(value = val)
    def _calcValueInline(self):

        NCells = self.mesh.numberOfCells
        ids = self.mesh.cellFaceIDs

        val = self._array.copy()

        inline._runInline("""
        int i;

        for(i = 0; i < numberOfCells; i++)
          {
          int j;
          value[i] = 0.;
          for(j = 0; j < numberOfCellFaces; j++)
            {
              // cellFaceIDs can be masked, which caused subtle and 
              // unreproduceable problems on OS X (who knows why not elsewhere)
              long id = ids[i + j * numberOfCells];
              if (id >= 0) { 
                  value[i] += orientations[i + j * numberOfCells] * faceVariable[id];
              }
            }
            value[i] = value[i] / cellVolume[i];
          }
          """,
                          numberOfCellFaces=self.mesh._maxFacesPerCell,
                          numberOfCells=NCells,
                          faceVariable=self.faceVariable.numericValue,
                          ids=numerix.array(ids),
                          value=val,
                          orientations=numerix.array(
                              self.mesh._cellToFaceOrientations),
                          cellVolume=numerix.array(self.mesh.cellVolumes))

        return self._makeValue(value=val)
Example #32
0
    def _execInline(self, comment=None):
        """
        Gets the stack from _getCstring() which calls _getRepresentation()
        
            >>> (Variable((1,2,3,4)) * Variable((5,6,7,8)))._getCstring()
            '(var0[i] * var1[i])'
            >>> (Variable(((1,2),(3,4))) * Variable(((5,6),(7,8))))._getCstring()
            '(var0[i + j * ni] * var1[i + j * ni])'
            >>> (Variable((1,2)) * Variable((5,6)) * Variable((7,8)))._getCstring()
            '((var00[i] * var01[i]) * var1[i])'

        The following test was implemented due to a problem with
        contiguous arrays.  The `mesh.getCellCenters()[1]` command
        introduces a non-contiguous array into the `Variable` and this
        causes the inline routine to return senseless results.
        
            >>> from fipy import Grid2D, CellVariable
            >>> mesh = Grid2D(dx=1., dy=1., nx=2, ny=2)
            >>> var = CellVariable(mesh=mesh, value=0.)
            >>> Y =  mesh.getCellCenters()[1]
            >>> var.setValue(Y + 1.0)
            >>> print var - Y
            [ 1.  1.  1.  1.]
        """
    
        from fipy.tools import inline
        argDict = {}
        string = self._getCstring(argDict=argDict, freshen=True) + ';'
        
        try:
            shape = self.opShape
        except AttributeError:
            shape = self.shape

        dimensions = len(shape)
            
        if dimensions == 0:
            string = 'result[0] = ' + string
            dim = ()
        else:
            string = 'result' + self._getCIndexString(shape) + ' = ' + string
            ni = self.opShape[-1]
            argDict['ni'] = ni
            if dimensions == 1:
                dim = (ni)
            else:
                nj = self.opShape[-2]
                argDict['nj'] = nj
                if dimensions == 2:
                    dim =(nj,ni)
                elif dimensions == 3:
                    nk = self.opShape[-3]
                    dim = (nk,nj,ni)
                    argDict['nk'] = nk
                else:
                    raise DimensionError, 'Impossible Dimensions'

        ## Following section makes sure that the result array has a
        ## valid typecode. If self.value is None then a typecode is
        ## assigned to the Variable by running the calculation without
        ## inlining. The non-inlined result is thus used the first
        ## time through.

        
        if self.value is None and not hasattr(self, 'typecode'):
            self.canInline = False
            argDict['result'] = self.getValue()
            self.canInline = True
            self.typecode = numerix.obj2sctype(argDict['result'])
        else:
            if self.value is None:
                if self.getsctype() == numerix.bool_:
                    argDict['result'] = numerix.empty(dim, numerix.int8)
                else:
                    argDict['result'] = numerix.empty(dim, self.getsctype())
            else:
                argDict['result'] = self.value

            resultShape = argDict['result'].shape

            if resultShape == ():
                argDict['result'] = numerix.reshape(argDict['result'], (1,))

            inline._runInline(string, converters=None, comment=comment, **argDict)

            if resultShape == ():
                argDict['result'] = numerix.reshape(argDict['result'], resultShape)

        return argDict['result']