TypeError when plotting a 2D diffusion (was: Re: Installation failure in Python 3 virtualenv)
Hi Daniel, Sorry for the somewhat long delay. I couldn't work on the problem for a while. On 2014-10-20 16:25, Daniel Wheeler wrote: On Sun, Oct 19, 2014 at 4:57 PM, Stefan Schwarzer sschwar...@sschwarzer.net wrote: I gave it a try and installed Anaconda3. When running `python setup.py install`, I still have/had some issues. For one, a few files have mixed indentation (mixed spaces and tabs) which Python 3 doesn't like: Sorry: TabError: inconsistent use of tabs and spaces in indentation (testBase.py, line 47) Sorry: TabError: inconsistent use of tabs and spaces in indentation (trilinosSolver.py, line 148) Sorry: TabError: inconsistent use of tabs and spaces in indentation (gistVectorViewer.py, line 70) I've never seen that before when installing with Python 3 in Anaconda. Which version of FiPy are you using? I saw this both in FiPy 3.1 and in the Git repository I fetched today. You don't see the problem in Python 2, but Python 3 complains if you mix tabs and spaces in one file. After fixing the files, I got that far: The required module(s) ['pysparse'] cannot be loaded. FiPy will not work properly until these modules are installed. -- The optional module(s) ['gist', 'mayavi'] cannot be loaded. FiPy will have improved capabilities if these modules are installed. It seems `pysparse` is only available for Python 2. Is this correct? Is it indeed needed? You don't need PySparse. It isn't available for Python 3 yet. The Scipy solvers are adequate to get started. None of the solvers are required packages, but you obviously need one of them. I suggest fixing the message then because it says `pysparse` is required. :-) Gist is a very old package that we need to remove from the dependencies. It's a viewer. You don't actually need any of the viewers to use FiPy. However to see stuff interactively while the simulation is running, it is good to have one of them. I would suggest using Matplotlib to get started. Mayavi is just optional. I've set up an Anaconda environment and installed FiPy from Git (I tried with FiPy 3.1 first), matplotlib 1.4.2 and NumPy 1.9.0. I tried to run the following code, mesh = fipy.Grid2D(dx=1.0, dy=1.0, nx=10, ny=10) initial_value = 0.0 concentration = fipy.CellVariable( mesh, concentration, value=initial_value) x, y = mesh.cellCenters concentration.setValue(10.0, where=(x3.0) (y3.0)) eq = fipy.TransientTerm() == fipy.DiffusionTerm(coeff=1.0) import ipdb; ipdb.set_trace() viewer = fipy.MatplotlibViewer(vars=concentration, xmin=0, xmax=1.0, ymin=0, ymax=1.0, datamin=0.0, datamax=10.0) # Solve. time_step = 0.01 steps = 1000 for step in range(steps): eq.solve(var=concentration, dt=time_step) viewer.plot() but it fails when setting up the viewer: /home/schwa/sd/python/diffusion/diffusion.py in _set_up(self) 88 eq = fipy.TransientTerm() == fipy.DiffusionTerm(coeff=1.0) 89 import ipdb; ipdb.set_trace() --- 90 viewer = fipy.MatplotlibViewer(vars=concentration, 91xmin=0, xmax=1.0, ymin=0, ymax=1.0, 92datamin=0.0, datamax=10.0) /home/schwa/sd/anaconda3/envs/diffusion/lib/python3.4/site-packages/FiPy-3.1-py3.4.egg/fipy/viewers/matplotlibViewer/__init__.py in MatplotlibViewer(vars, title, limits, cmap, colorbar, axes, **kwlimits) 115 try: 116 from fipy.viewers.matplotlibViewer.matplotlib2DGridViewer import Matplotlib2DGridViewer -- 117 return Matplotlib2DGridViewer(vars=vars, title=title, cmap=cmap, colorbar=colorbar, axes=axes, **kwlimits) 118 except MeshDimensionError: 119 try: /home/schwa/sd/anaconda3/envs/diffusion/lib/python3.4/site-packages/FiPy-3.1-py3.4.egg/fipy/viewers/matplotlibViewer/matplotlib2DGridViewer.py in __init__(self, vars, title, limits, cmap, colorbar, axes, figaspect, **kwlimits) 80 AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, 81 cmap=cmap, colorbar=colorbar, axes=axes, figaspect=figaspect, --- 82 **kwlimits) 83 84 self.image = self.axes.imshow(self._data, /home/schwa/sd/anaconda3/envs/diffusion/lib/python3.4/site-packages/FiPy-3.1-py3.4.egg/fipy/viewers/matplotlibViewer/matplotlibViewer.py in __init__(self, vars, title, figaspect, cmap, colorbar, axes, log, **kwlimits) 108 109 if colorbar: -- 110 self.colorbar = _ColorBar(viewer=self) 111 else: 112 self.colorbar = None /home/schwa/sd/anaconda3/envs/diffusion/lib/python3.4/site-packages/FiPy-3.1-py3.4.egg/fipy/viewers/matplotlibViewer/matplotlibViewer.py in __init__(self, viewer, vmin
Re: Specifying initial values on Grid2D mesh
Hi Daniel, On 2013-01-02 16:17, Daniel Wheeler wrote: On Mon, Dec 31, 2012 at 10:04 AM, Stefan Schwarzer sschwarzer@sschwarzer .net wrote: RuntimeWarning: invalid value encountered in double_scalars if (numerix.sqrt(numerix.sum(errorVector**2)) / error0) = self.tolerance I don't get the above warning. Which version of FiPy are you using and which solver? FiPy 3.0 with the SciPy solver. Versions are SciPy 0.10.1 and NumPy 1.6.2 in case it matters. Now I want to set different initial concentration values for different mesh points. How do I do this? If you want to set values based on position, then use mesh.cellCenters or mesh.x and mesh.y to get the position of each cell. The ordering corresponds to the ordering for CellVariable values (as suggested by Salomen ) Yes, that looks good. :-) Thanks, Salomon! :-) By reading some of the FiPy code in `meshVariable.py`, I found out that I can pass an array as the `value` argument of the `CellVariable` constructor, like this: You can do this, but you have no idea how the CellVariable values correspond to position. There is no particular ordering of values in FiPybase on geometric position. Maybe there should be something in the docs what the semantics of the `rank` and `elementShape` are. def test(): # Set up problem. mesh = fipy.Grid2D(dx=1.0, dy=1.0, nx=10, ny=10) initial_value = fipy.numerix.zeros((10, 10)) initial_value[:3, :3] = 5 CellVariables are flat arrays as far as geometry is concerned. The last index of a CellVariable always corresponds to the number of cells in the mesh. Thanks for the clarification. Won't work either. Try using Salomen's example. Does that meet your needs? Yes, that looks fine. P. S.: I just found http://article.gmane.org/gmane.comp.python.fipy/2803 Is this the answer to my question? This just assigns random values independent of location. I didn't mean it literally; I didn't want to use the random values. ;-) It is also concerned with interpolating between grids that are not necessarily aligned. Yes, I was assuming that. In my case setting mesh values directly, without interpolation, will most likely do it. Stefan ___ fipy mailing list fipy@nist.gov http://www.ctcms.nist.gov/fipy [ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]
Specifying initial values on Grid2D mesh
Hello, I'm making my first steps with FiPy. I studied Chemical Engineering a while back, but am mostly working as a software developer these days, mainly with Python. I want to formulate a 2D diffusion problem and started from example http://www.ctcms.nist.gov/fipy/examples/diffusion/generated/examples.diffusion.mesh20x20.html after having read the FiPy introduction chapter at http://www.ctcms.nist.gov/fipy/documentation/introduction.html . This code, very similar to the mesh20x20 example, works as expected: def test(): # Set up problem. mesh = fipy.Grid2D(dx=1.0, dy=1.0, nx=10, ny=10) initial_value = 2.0 concentration = fipy.CellVariable( mesh, concentration, value=initial_value) eq = fipy.TransientTerm() == fipy.DiffusionTerm(coeff=1.0) viewer = fipy.Viewer(vars=concentration, datamin=0.0, datamax=10.0) # Solve. time_step = 0.01 steps = 10 for step in range(steps): eq.solve(var=concentration, dt=time_step) viewer.plot() raw_input() apart from printing a warning RuntimeWarning: invalid value encountered in double_scalars if (numerix.sqrt(numerix.sum(errorVector**2)) / error0) = self.tolerance Obviously, the above problem isn't very interesting. I start with a concentration of 2.0 everywhere and due to the default boundary conditions, which don't allow any mass transfer at the boundaries, the concentration stays the same throughout the simulation. Now I want to set different initial concentration values for different mesh points. How do I do this? By reading some of the FiPy code in `meshVariable.py`, I found out that I can pass an array as the `value` argument of the `CellVariable` constructor, like this: def test(): # Set up problem. mesh = fipy.Grid2D(dx=1.0, dy=1.0, nx=10, ny=10) initial_value = fipy.numerix.zeros((10, 10)) initial_value[:3, :3] = 5 concentration = fipy.CellVariable( mesh, concentration, value=initial_value, rank=0) eq = fipy.TransientTerm() == fipy.DiffusionTerm(coeff=1.0) viewer = fipy.Viewer(vars=concentration, datamin=0.0, datamax=10.0) # Solve. time_step = 0.01 steps = 10 for step in range(steps): eq.solve(var=concentration, dt=time_step) viewer.plot() raw_input() However, this gives me Traceback (most recent call last): File fipy_experiment.py, line 45, in module test() File fipy_experiment.py, line 34, in test viewer = fipy.Viewer(vars=concentration, datamin=0.0, datamax=10.0) File /home/schwa/.virtualenvs/diffusion/lib/python2.7/site-packages/fipy/viewers/__init__.py, line 136, in Viewer raise ImportError, Failed to import a viewer: %s % str(errors) ImportError: Failed to import a viewer: ['matplotlib: The mesh must be a Mesh2D instance', 'gist: __call__() takes at least 2 arguments (1 given)', 'gnuplot: __call__() takes at least 2 arguments (1 given)', 'mayavi: No module named enthought.tvtk.api'] When I set the rank argument to 0 explicitly, as I understand from the `_MeshVariable.__init__` docstring, I still get the same traceback. (I understand the word element in the mentioned docstring as referring to the concentration value in my case, which is a scalar.) I also tried setting `elementShape` to `()` explicitly, but this gives me Traceback (most recent call last): File fipy_experiment.py, line 46, in module test() File fipy_experiment.py, line 33, in test elementshape=()) File /home/schwa/.virtualenvs/diffusion/lib/python2.7/site-packages/fipy/variables/cellVariable.py, line 69, in __init__ rank=rank, elementshape=elementshape, unit=unit) File /home/schwa/.virtualenvs/diffusion/lib/python2.7/site-packages/fipy/variables/meshVariable.py, line 133, in __init__ array=array, cached=cached) File /home/schwa/.virtualenvs/diffusion/lib/python2.7/site-packages/fipy/variables/variable.py, line 112, in __init__ self._setValueInternal(value=value, unit=unit, array=array) File /home/schwa/.virtualenvs/diffusion/lib/python2.7/site-packages/fipy/variables/variable.py, line 666, in _setValueInternal self._value = self._makeValue(value=value, unit=unit, array=array) File /home/schwa/.virtualenvs/diffusion/lib/python2.7/site-packages/fipy/variables/variable.py, line 698, in _makeValue array[:] = value ValueError: output operand requires a reduction, but reduction is not enabled I don't know what this is supposed to tell me. :-) In summary: How can I specify different scalar initial values for the points in the mesh? P. S.: I just found http://article.gmane.org/gmane.comp.python.fipy/2803 Is this the answer to my question? If yes, could this be made somewhat easier? For example, would it be feasible to wrap that functionality in some convenience API of FiPy? Stefan ___ fipy mailing list