Fipy
From charlesreid1
Contents
Installing Fipy
Installing FiPy with Pip
These instructions worked for Mountain Lion and Maverick (OS X 10.7 and 10.8).
Numpy
Keep it simple:
pip install numpy
Pysparse
Both pip and easy_install are broken for pysparse:
$ pip install pysparse Downloading/unpacking pysparse Could not find a version that satisfies the requirement pysparse (from versions: 1.1.1-dev, 1.2-dev, 1.2-dev202, 1.2-dev203, 1.2-dev213, 1.3-dev) Cleaning up... No distributions matching the version for pysparse Storing complete log in /Users/charles/.pip/pip.log
$ easy_install pysparse [...] 1 error generated. error: Setup script exited with error: Command "/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -g -O2 -DNDEBUG -g -O3 -DLENFUNC_OK=1 -DNO_ATLAS_INFO=3 -I/private/var/folders/nw/1yytq18n06db0v8r1pvxf5rc0000gn/T/easy_install-dlFghq/pysparse-1.3-dev/pysparse/sparse/src -I/var/folders/nw/1yytq18n06db0v8r1pvxf5rc0000gn/T/easy_install-dlFghq/pysparse-1.3-dev/pysparse/include -I/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/include -I/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/include -I/var/folders/nw/1yytq18n06db0v8r1pvxf5rc0000gn/T/easy_install-dlFghq/pysparse-1.3-dev/pysparse/include -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c /private/var/folders/nw/1yytq18n06db0v8r1pvxf5rc0000gn/T/easy_install-dlFghq/pysparse-1.3-dev/pysparse/sparse/src/spmatrixmodule.c -o build/temp.macosx-10.6-intel-2.7/private/var/folders/nw/1yytq18n06db0v8r1pvxf5rc0000gn/T/easy_install-dlFghq/pysparse-1.3-dev/pysparse/sparse/src/spmatrixmodule.o -msse3 -I/System/Library/Frameworks/vecLib.framework/Headers" failed with exit status 1
So I built it myself:
$ git clone git://pysparse.git.sourceforge.net/gitroot/pysparse/pysparse $ cd pysparse $ python setup.py build && python setup.py install
Fipy
Using pip:
$ pip install fipy
Installing Fipy from Source
I was able to mostly follow this guide on Matforge (the site that hosts Fipy source code): http://matforge.org/fipy/wiki/InstallFiPy/MacOSX/SnowLeopard
Here are the steps I took:
- Skipped virtualenv (because I manage my Python distribution intelligently/carefully, I didn't need to bother with this step. See my Python page for more information.)
- Already had Gfortran installed.
- Installed the latest Cmake
- Skipped FFTW
- Skipped SparseSuite
- I was able to skip both of these because my Numpy was working and using the system's LAPACK/BLAS without problems.
- Already had Numpy installed
- Already had Matplotlib installed
- Installed Pysparse from source
Once I had all of these dependencies installed, I was able to download and install Fipy. Note that as of this writing (December 2013), the instructions on http://matforge.org/fipy/wiki/InstallFiPy/MacOSX/SnowLeopard are out of date, because it specifies that you should check out a copy of the source code via SVN. In fact, Fipy now uses git.
Either download the latest release from this page: http://www.ctcms.nist.gov/fipy/download/ or by cloning a copy with git:
$ git clone git://code.matforge.org/nist/fipy.git
Using Fipy
Vector Equations in Fipy
Notes on using vector equations in Fipy:
Fipy Scripts
I have assembled notes on Fipy scripts solving a number of different types of problems:
Transient
- Fipy/Transient 0D - solves a transient 0D equation with a constant source term
- Fipy and Cantera/Reaction 0D - solves a transient 0D equation with a reaction source term calculated with Cantera
Diffusion
- Fipy/Diffusion 1D - solves a steady 1D diffusion problem
- Fipy/Transient Diffusion 1D - solves a transient 1D diffusion problem
- Fipy and Cantera/Diffusion 1D - solves a transient 1D diffusion problem with variable diffusivities, and uses Cantera to calculate the diffusivities as a function of composition
Convection
- Fipy/Convection 1D - solves a steady 1D convection problem
Fipy and Cantera
Some notes on how to use Fipy with Cantera:
Notes on Fipy Examples
Surface Adsorption Equation
One of the examples included with Fipy is a electrochemical level set example, which defines, among other things, a surface adsorption equation class. I have compiled some scattered notes on this class here:
Fipy/Surface Adsorption Equation
My application was somewhat different - I wasn't so much interested in the evolution of the interface, as much as I was interested in the evolution of adsorbed surface species on a stationary catalyst surface.
Fipy Errors
Pysparse TypeError Using Vector Equations
When you follow the installation procedure above, to install Pysparse and then Fipy, you may see some issues when you sweep vector equations in Fipy. Specifically, you will see TypeErrors coming from Pysparse, but with no explanation at all of what's causing them. For example, an output that looks like this:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) [...] /home/charles/pkg/transport/git/verification_aris/ConcentrationReactor.pyc in compute_solution(self) 238 239 # Solve --> 240 res = eqn1.sweep(var=C) 241 print "Residual (Rxn-Diff Eqn) = ",res 242 /usr/local/lib/python2.7/dist-packages/FiPy-3.1-py2.7.egg/fipy/terms/term.pyc in sweep(self, var, solver, boundaryConditions, dt, underRelaxation, residualFn, cacheResidual, cacheError) 236 solver = self._prepareLinearSystem(var=var, solver=solver, boundaryConditions=boundaryConditions, dt=dt) 237 solver._applyUnderRelaxation(underRelaxation=underRelaxation) --> 238 residual = solver._calcResidual(residualFn=residualFn) 239 240 if cacheResidual or cacheError: /usr/local/lib/python2.7/dist-packages/FiPy-3.1-py2.7.egg/fipy/solvers/solver.pyc in _calcResidual(self, residualFn) 146 return residualFn(self.var, self.matrix, self.RHSvector) 147 else: --> 148 return numerix.L2norm(self._calcResidualVector()) 149 150 def _calcRHSNorm(self): /usr/local/lib/python2.7/dist-packages/FiPy-3.1-py2.7.egg/fipy/solvers/solver.pyc in _calcResidualVector(self, residualFn) 138 return residualFn(self.var, self.matrix, self.RHSvector) 139 else: --> 140 Lx = self.matrix * numerix.array(self.var) 141 142 return Lx - self.RHSvector /usr/local/lib/python2.7/dist-packages/FiPy-3.1-py2.7.egg/fipy/matrices/pysparseMatrix.pyc in __mul__(self, other) 325 matrix=spmatrix.matrixmultiply(self.matrix, other.matrix)) 326 else: --> 327 return _PysparseMatrixFromShape.__mul__(self, other) 328 329 def asTrilinosMeshMatrix(self): /usr/local/lib/python2.7/dist-packages/FiPy-3.1-py2.7.egg/fipy/matrices/pysparseMatrix.pyc in __mul__(self, other) 167 return y 168 else: --> 169 raise TypeError 170 171 def __rmul__(self, other): TypeError:
The problem, ultimately, is that the "vector" that Pysparse is trying to turn into a Pysparse vector is not, in fact, a vector, but a square matrix.
Fortunately, the solution is simple, and involves the ravel()
function. This unfurls a square matrix into a single-column vector.
You will need to patch Fipy's class that wraps Pysparse matrices:
*** a/fipy/matrices/pysparseMatrix.py 2013-09-30 19:17:46.000000000 -0700 --- b/fipy/matrices/pysparseMatrix.py 2014-01-16 10:55:13.000000000 -0800 *************** class _PysparseMatrix(_SparseMatrix): *** 165,170 **** --- 165,175 ---- y = numerix.empty((self.matrix.shape[0],)) self.matrix.matvec(other, y) return y + elif shape[0]*shape[1] == N: + other = other.ravel() + y = numerix.empty((self.matrix.shape[0],)) + self.matrix.matvec(other, y) + return y else: raise TypeError
This patch file is available here: http://files.charlesmartinreid.com/pysparseMatrix.patch
Applying the Patch
If you've downloaded Fipy, go into the directory containing the files you downloaded and copy the patch to that location. You can then apply the patch by running the command:
$ patch -p1 < pysparseMatrix.patch
The -p1
strips off the "a" and "b" directory names in front of the paths to files in the patch.