This issue has now been fixed on trunk and the version-2_0 branch. The
ticket was filed here:
<http://matforge.org/fipy/ticket/195>
and the changeset is:
<http://matforge.org/fipy/changeset/3194>
Note that this bug could potentially have had bad effects on results
on nonuniform and unstructured meshes.
Cheers
On Fri, Jul 31, 2009 at 5:53 PM, Daniel
Wheeler<[email protected]> wrote:
> Hi William,
>
> You have found a bug! A fairly heinous one at that. So, it appears
> that arithmetic face values were back to front in terms of the
> assumption about the face to cell distance ratio. The fix for this
> issue is this:
>
> $ svn diff
> Index: fipy/variables/arithmeticCellToFaceVariable.py
> ===================================================================
> --- fipy/variables/arithmeticCellToFaceVariable.py (revision 3186)
> +++ fipy/variables/arithmeticCellToFaceVariable.py (working copy)
> @@ -42,7 +42,7 @@
> def _calcValuePy(self, alpha, id1, id2):
> cell1 = numerix.take(self.var, id1, axis=-1)
> cell2 = numerix.take(self.var, id2, axis=-1)
> - return (cell1 - cell2) * alpha + cell2
> + return (cell2 - cell1) * alpha + cell1
>
> def _calcValueIn(self, alpha, id1, id2):
> val = self._getArray().copy()
>
> All, the tests appear to pass when I make this change other than two
> tests that are manifestly broken due to our assumption about the
> correct arithmetic face average. I am not sure how we have gone along
> with this particular problem for so long, but evidently we have. Most
> of our own work is with regular grids and hence we obviously haven't
> noticed it. Anyway, I'll correct this in version 2 and on trunk and do
> a release of version 2.0.3 fairly soon. Many thanks for raising this
> issue,much appreciated. I'll let you know when I've fixed the new
> version and fixed trunk.
>
> PS You might want to update your version of fipy. It appears you are
> using a pre 2.0 version. Definitely recommend upgrading, but you might
> want to wait for version 2.0.3.
>
> Cheers
>
> On Wed, Jul 29, 2009 at 11:19 PM, William Gathright<[email protected]> wrote:
>>
>> Hello,
>> I was calculating gradients over a nonuniform mesh when I noticed that
>> the gradient that Fipy calculates is not what I expect. I have
>> constructed the simplest example I could think of below to show this
>> behavior. In this example, I calculate the gradient of a
>> monotonically increasing variable in 1D. I expected to see a gradient
>> of 1 on all interior cells. Understanding that Fipy expects zero-flux
>> boundary conditions on all variables, I expected to see a gradient of
>> 0.5 for the boundary cells. These are exactly the results from using
>> the uniform grid.
>>
>> When the gradient is calculated using the non-uniform grid spacing it
>> does not come back with these results. Instead, the gradient is
>> slightly larger than 1 except at the mid-point. In this example, the
>> mid-point is special because it is where the grid spacing switches
>> from getting smaller to getting larger. At this point, Fipy
>> calculates a gradient that is much too large (11% in this case).
>>
>> I did not see any bug reports about such a problem in the archives.
>> This was run on a Windows Vista machine, Intel Centrino Duo2
>> processor, Fipy 1.2, and Python 2.4.
>>
>> Is there an alternate way of specifying a nonuniform grid spacing to
>> avoid this unexpected behavior?
>>
>> Thanks for your help,
>> Will Gathright
>>
>> #------------ Begin Example Code -------------------------
>> from fipy import *
>>
>> #The uniform grid will not cause the strange behavior
>> #dx = Variable("1 nm") * numerix.cumproduct(numerix.concatenate(([1]*100,)))
>>
>> #The variable grid seems to change the way that FiPy calculates the gradient
>> #This grid spacing gets smaller and smaller for 10 points, then larger
>> for 10 points
>> dx = Variable("1 nm") *
>> numerix.cumproduct(numerix.concatenate(([0.9]*10, [1/.9]*10)))
>> L = numerix.sum(dx)
>> dx = dx / L
>> mesh = Grid1D(dx=dx)
>>
>> A=CellVariable(mesh=mesh, name='A', hasOld=1)
>>
>> x = mesh.getCellCenters()[...,0]
>> A.setValue(x)
>>
>> print A.getGrad().getValue()
>>
>> tsv=TSVViewer(vars=[A, A.getGrad()])
>> tsv.plot('getgrad_test.txt')
>>
>>
>
>
>
> --
> Daniel Wheeler
>
--
Daniel Wheeler