← Back to team overview

dolfin team mailing list archive

Reduce clutter in dolfin python namespace.

 

The attached patches (one for each of dolfin, ffc, viper) reduce the number of
attributes from 546 to 137. The ugliest bit is the swig import, which I ended
up cherry-picking from in __init__.py. All the swigged functions are still
available through dolfin.dolfin.

A few comments:

[Anders]
> If you want to make a patch (hg export) or bundle, that would be
> excellent. Remove as much as you can, as long as it's possible to
> run the demos.

I have tested those that I can (I don't have petsc, slepc, scotch, gts). Two of
them fail, but I don't think it's my fault:

- convection-diffusion fails with some Vector/GenericVector confusion.

- periodic
    Traceback (most recent call last):
    File "demo.py", line 68, in ?
      u = pde.solve()
    File "/home/jbh/src/fenics/dolfin/site-packages/dolfin/assemble.py", line 193, in solve
      cpp_DirichletBC.apply(bc, A, b, self.dof_maps.sub(1), compiled_form)
    TypeError: unbound method apply() must be called with cpp_DirichletBC instance as first argument (got PeriodicBC instance instead)

> Sounds good. Be very aggressive when pruning the namespace. We can
> always add back the things that are missing one by one.

I took the opportunistic approach: fix the simple majority first, makes it
easier to get an overview and do the rest (which requires more thought or work)
in smaller increments later. Still, after reading this comment I went back and
commented out (in __init__) the swig classes that weren't being used by demos,
but haven't done the same for ffc, assembler etc.

> I'm not sure we need to modularize the namespace, so let's keep it
> flat until it's really necessary. My reason for wanting to keep it
> flat is:
>
> 1. There aren't (or shouldn't be) that many things in the namespace
> anyway. Basically, we have a few classes:
>
>     Vector, Matrix, Mesh, FiniteElement, Function, DirichletBC
>
> and a few free functions:
>
>     assemble, solve, plot

I made one submodule (logger). Feel free to remove it, it's not used by any
current code. But I do think that to make it as simple as above, it requires
splitting up the namespace (quadrature, graph, mesh, linalg, etc). That doesn't
mean that the most common stuff (like Mesh) cannot also be present at
top-level.

[Joachim]
> - A number of functions seem to be "leaked" from c++. Examples: ios, ios_base,
>   ios_base_sync_with_stdio, ios_base_xalloc, iostream, istream, istringstream,
>   endl_cb_ptr, ends, ends_cb_ptr.

These are now removed from the namespace, but they are still generated
(unnecessarily?) by swig with resulting longer compile time etc. Should be
filtered higher up?

[Joachim]
> - Some non-obvious names. Examples: dolfin_{add,get,set} which could perhaps be
>   called {add,get,set}_parameter. I was actually looking for this functionality
>   but didn't find it before compiling this list :)

I didn't touch this, since they have the same names in c++.

-j.

# HG changeset patch
# User "Joachim B Haga <jobh@xxxxxxxxx>"
# Date 1206879405 -7200
# Node ID f55c4da693e090c8a0bac52f23dcf7eb488bc469
# Parent  cb9afa68c89cfc8492a25ed849586b1679eebdcd
Reduce clutter in dolfin namespace.

diff -r cb9afa68c89c -r f55c4da693e0 demo/pde/poisson1D/python/demo.py
--- a/demo/pde/poisson1D/python/demo.py	Sat Mar 29 16:12:04 2008 +0100
+++ b/demo/pde/poisson1D/python/demo.py	Sun Mar 30 14:16:45 2008 +0200
@@ -51,8 +51,8 @@ pde = LinearPDE(a, L, mesh, bc)
 pde = LinearPDE(a, L, mesh, bc)
 u = pde.solve()
 
-# Viper can't plot in 1D yet
-# plot(u)
+# Plot solution
+plot(u)
 
 # Save solution to file
 file = File("poisson.pvd")
diff -r cb9afa68c89c -r f55c4da693e0 site-packages/dolfin/__init__.py
--- a/site-packages/dolfin/__init__.py	Sat Mar 29 16:12:04 2008 +0100
+++ b/site-packages/dolfin/__init__.py	Sun Mar 30 14:16:45 2008 +0200
@@ -1,9 +1,50 @@
 # Import DOLFIN wrapper module as well as additional utility entities
 
+from ffc import *
 from constants import *
 from assemble import *
 from norm import *
 from plot import *
-from dolfin import *
+
+import dolfin
+from dolfin import dolfin_get, dolfin_add, dolfin_set, tic, toc, tocd
+#from dolfin import GenericTensor, GenericMatrix, GenericVector
+from dolfin import Matrix, Vector, Scalar
+#from dolfin import LinearSolver, KrylovSolver, LUSolver, GMRES, LU
+from dolfin import solve, residual
+#from dolfin import ElementLibrary, InvMeshSize
+#from dolfin import Graph, GraphEditor, GraphPartition
+#from dolfin import UndirectedClique, DirectedClique
+from dolfin import File
+#from dolfin import Lagrange, Legendre
+#from dolfin import Quadrature, Variable
+from dolfin import GaussianQuadrature, GaussQuadrature
+from dolfin import RadauQuadrature, LobattoQuadrature
+from dolfin import Mesh, MeshEditor, MeshFunction
+#from dolfin import MeshEntity, MeshTopology, MeshGeometry
+#from dolfin import MeshConnectivity, MPIMeshCommunicator, BoundaryMesh
+from dolfin import Vertex, Edge, Face, Facet, Cell, Point
+from dolfin import vertices, edges, faces, facets, cells
+from dolfin import SubDomain
+from dolfin import UnitCube, UnitInterval, UnitSquare
+#from dolfin import IntersectionDetector, DofMap, DofMapSet
+from dolfin import SubSystem
+from dolfin import BoundaryCondition, PeriodicBC
+#from dolfin import Form, Assembler
+#from dolfin import MatrixFactory
+from dolfin import NewtonSolver, NonlinearProblem
+from dolfin import TimeDependent, ODE, ComplexODE, Homotopy
+
+try: # optional package petsc
+    from dolfin import PETScMatrix, PETScVector
+except:
+    pass
+
+try: # optional package slepc
+    from dolfin import SLEPcEigenValueSolver
+except:
+    pass
 
 from numpy import sin, cos, exp
+
+import logger
diff -r cb9afa68c89c -r f55c4da693e0 site-packages/dolfin/assemble.py
--- a/site-packages/dolfin/assemble.py	Sat Mar 29 16:12:04 2008 +0100
+++ b/site-packages/dolfin/assemble.py	Sun Mar 30 14:16:45 2008 +0200
@@ -12,6 +12,9 @@ __date__ = "2007-08-15 -- 2008-02-11"
 __date__ = "2007-08-15 -- 2008-02-11"
 __copyright__ = "Copyright (C) 2007-2008 Anders Logg"
 __license__  = "GNU LGPL Version 2.1"
+
+__all__ = ['assemble', 'Function', 'FacetNormal', 'MeshSize', 'AvgMeshSize',
+           'LinearPDE', 'DirichletBC']
 
 from ffc import *
 from dolfin import *
diff -r cb9afa68c89c -r f55c4da693e0 site-packages/dolfin/logger.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/site-packages/dolfin/logger.py	Sun Mar 30 14:16:45 2008 +0200
@@ -0,0 +1,1 @@
+from dolfin import warning, error, message, begin, end, debug, Event, Progress
diff -r cb9afa68c89c -r f55c4da693e0 site-packages/dolfin/norm.py
--- a/site-packages/dolfin/norm.py	Sat Mar 29 16:12:04 2008 +0100
+++ b/site-packages/dolfin/norm.py	Sun Mar 30 14:16:45 2008 +0200
@@ -5,6 +5,8 @@ __date__ = "2008-03-19 -- 2008-03-19"
 __date__ = "2008-03-19 -- 2008-03-19"
 __copyright__ = "Copyright (C) 2008 Anders Logg"
 __license__  = "GNU LGPL Version 2.1"
+
+__all__ = ['norm']
 
 from assemble import assemble
 from ffc import dot, grad, div, curl, dx
# HG changeset patch
# User "Joachim B Haga <jobh@xxxxxxxxx>"
# Date 1206878601 -7200
# Node ID 38ee98ce8631cb46bd85bc99171918bad5fb54b1
# Parent  e7a1ca641486c424e91b813267ce7953a1f99f3a
Reduce clutter in main ffc namespace.

diff -r e7a1ca641486 -r 38ee98ce8631 src/ffc/__init__.py
--- a/src/ffc/__init__.py	Tue Mar 25 12:00:30 2008 +0100
+++ b/src/ffc/__init__.py	Sun Mar 30 14:03:21 2008 +0200
@@ -22,10 +22,11 @@ except:
 
 # Import finite elements and dof map
 from ffc.fem.finiteelement import FiniteElement
-from ffc.fem.vectorelement import *
+from ffc.fem.vectorelement import VectorElement, VectorQuadratureElement
 from ffc.fem.mixedelement import MixedElement
 from ffc.fem.mixedfunctions import TestFunctions, TrialFunctions, Functions
 from ffc.fem.dofmap import DofMap
+from ffc.fem.projection import Projection
 
 # Import compiler
 from ffc.compiler.compiler import compile
@@ -39,4 +40,4 @@ from ffc.compiler.language.builtins impo
 from ffc.compiler.language.builtins import *
 
 # Import constants
-from ffc.common.constants import *
+#from ffc.common.constants import *
diff -r e7a1ca641486 -r 38ee98ce8631 src/ffc/compiler/language/operators.py
--- a/src/ffc/compiler/language/operators.py	Tue Mar 25 12:00:30 2008 +0100
+++ b/src/ffc/compiler/language/operators.py	Sun Mar 30 14:03:21 2008 +0200
@@ -5,6 +5,10 @@ __date__ = "2005-09-07 -- 2007-12-30"
 __date__ = "2005-09-07 -- 2007-12-30"
 __copyright__ = "Copyright (C) 2005-2007 Anders Logg"
 __license__  = "GNU GPL version 3 or any later version"
+
+__all__ = ['Identity', 'value_rank', 'vec', 'dot', 'cross', 'trace', 'transp',
+           'mult', 'outer', 'D', 'grad', 'div', 'rot', 'curl', 'mean', 'avg',
+           'jump', 'sqrt', 'modulus', 'lhs', 'rhs']
 
 # Modified by Ola Skavhaug, 2005
 # Modified by Dag Lindbo, 2006
# HG changeset patch
# User "Joachim B Haga <jobh@xxxxxxxxx>"
# Date 1206878801 -7200
# Node ID ea089fb991441eaf96bebb98df7cb1309d9838fe
# Parent  33a776fbc3ff4c3da4ee3846a8f32c5a5a11d5d8
Change in dolfin import of MeshFunction*.

Viper is the only user of the MeshFunctionUInt etc names
(as opposed to MeshFunction("uint") etc). Those are now
removed fram toplevel dolfin namespace. Change import so
that these are now imported from the "private" namespace
dolfin.dolfin.

diff -r 33a776fbc3ff -r ea089fb99144 src/viper/viper_dolfin.py
--- a/src/viper/viper_dolfin.py	Fri Mar 28 14:09:44 2008 +0100
+++ b/src/viper/viper_dolfin.py	Sun Mar 30 14:06:41 2008 +0200
@@ -21,9 +21,9 @@ Citation: %s
 """ % (__copyright__, __license__, __cite__)
 
 from viper import Viper as ViperBase
-from dolfin import Mesh, MeshFunctionInt, MeshFunctionUInt, MeshFunctionBool, MeshFunctionReal
-from dolfin import cpp_Function as Function
 import dolfin
+from dolfin.dolfin import Mesh, MeshFunctionInt, MeshFunctionUInt, MeshFunctionBool, MeshFunctionReal
+from dolfin.dolfin import cpp_Function as Function
 import numpy
 import vtk
 

Follow ups