On Feb 23, 2011, at 10:01 AM, Julien Derr wrote:
> I guess I understand where is the problem in my last post: I am a little bit > lost in the classes > I am just modifying the simpletrenchSystem.py example file. I would like the > extension velocity to be proportional to the gradient of the metalVar field. > > in the example, depositionRateVariable is of type > >>><class 'fipy.variables.binaryOperatorVariable.binOp'> > so it is not already a CellVariable Actually depositionRateVariable is a CellVariable. It is immediately a class of binOp, but it derives from CellVariable. A binOp is a constructed class that results from a binary operation involving a FiPy Variable. It is constructed such that its base class is whatever it needs to be: >>> A = Variable(3.) >>> AA = A * 4. >>> type(AA) <class 'fipy.variables.binaryOperatorVariable.binOp'> >>> AA._getArithmeticBaseClass() <class 'fipy.variables.variable.Variable'> >>> AA.__class__.__bases__[0].__bases__ (<class 'fipy.variables.variable.Variable'>,) >>> m = Grid2D(nx=2, ny=2) >>> x, y = m.getCellCenters() >>> B = CellVariable(mesh=m, value=x*y) >>> BB = B * 4. >>> type(BB) <class 'fipy.variables.binaryOperatorVariable.binOp'> >>> BB._getArithmeticBaseClass() <class 'fipy.variables.cellVariable.CellVariable'> >>> BB.__class__.__bases__[0].__bases__[0].__bases__[0].__bases__ (<class 'fipy.variables.cellVariable.CellVariable'>,) >>> X, Y = m.getFaceCenters() >>> >>> C = FaceVariable(mesh=m, value=X*Y) >>> CC = C * 4. >>> type(CC) <class 'fipy.variables.binaryOperatorVariable.binOp'> >>> CC._getArithmeticBaseClass() <class 'fipy.variables.faceVariable.FaceVariable'> >>> CC.__class__.__bases__[0].__bases__[0].__bases__ (<class 'fipy.variables.faceVariable.FaceVariable'>,) Generally you don't need to worry about this, but I can see why you found this situation confusing. > naively I wrote something like > >>>depositionRateVariable=metalVar.getFaceGrad() > > but now depositionRateVariable is a different type ??? that's why I get an > error : > >>> self._value[:] = value > >>>ValueError: shape mismatch: objects cannot be broadcast to a single shape > > > what is the right syntax to define depositionRateCariable proportional to the > gradient of metalVar, but to get the correct class ? In this case, I believe depositionRateVariable needs to be a rank 0 CellVariable, so >>> depositionRateVariable=metalVar.getGrad().getMag() should get what you want. If phi is a CellVariable, phi.getFaceGrad() returns a 2nd order discretization of the gradient, i.e., a rank 1 FaceVariable. phi.getGrad() returns a 1st order discretization of the gradient, i.e., a rank 1 CellVariable. v.getMag() returns the magnitude of the vector v, i.e., it converts rank 1 to rank 0. >>> B.getFaceGrad()._getArithmeticBaseClass() <class 'fipy.variables.faceVariable.FaceVariable'> >>> B.getFaceGrad().getRank() 1 >>> B.getFaceGrad().getMag()._getArithmeticBaseClass() <class 'fipy.variables.faceVariable.FaceVariable'> >>> B.getFaceGrad().getMag().getRank() 0 >>> B.getGrad()._getArithmeticBaseClass() <class 'fipy.variables.cellVariable.CellVariable'> >>> B.getGrad().getRank() 1 >>> B.getGrad().getMag()._getArithmeticBaseClass() <class 'fipy.variables.cellVariable.CellVariable'> >>> B.getGrad().getMag().getRank() 0 Dan Wheeler can speak to the details of this example as far as what modifications are appropriate and what are beyond the scope of the example.
