/usr/share/pyshared/dolfin/compilemodules/subdomains.py is in python-dolfin 1.0.0-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | "This module provides functionality for compilation of strings as dolfin SubDomains."
# Copyright (C) 2008-2008 Martin Sandve Alnes
#
# This file is part of DOLFIN.
#
# DOLFIN is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DOLFIN is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#
# First added: 2008-07-01
# Last changed: 2011-04-18
import re
import os
import hashlib
import instant
# Import local compile_extension_module
from dolfin.compilemodules.compilemodule import (compile_extension_module,
expression_to_code_fragments,
math_header)
__all__ = ["compile_subdomains",]
_map_args = ["x", "y"]
_subdomain_template = """
class %(classname)s: public SubDomain
{
public:
%(members)s
%(classname)s()
{
%(constructor)s
}
/// Return true for points inside the sub domain
bool inside(const Array<double>& x, bool on_boundary) const
{
%(inside)s
}
};
"""
# TODO: Support implementation of map as well
"""
/// Map coordinate x in domain H to coordinate y in domain G (used for periodic boundary conditions)
void map(const Array<double>& x, Array<double>& y) const
{
%(map)s
}
"""
def expression_to_subdomain(expr, classname):
"""
Generate code for a :py:class:`SubDomain <dolfin.cpp.SubDomain>`
subclass for a single expression.
"""
# Assure we have a simple string expression
assert isinstance(expr, str)
# Extract code fragments from the expr and defaults
fragments, members = expression_to_code_fragments(\
[expr], ["x", "on_boundary","DOLFIN_EPS"])
# Generate code for inside()
insidecode = " return %s;" % expr
# Generate code for map()
#mapcode = "..."
# Connect the code fragments using the function template code
fragments["classname"] = classname
fragments["inside"] = insidecode
#fragments["map"] = mapcode
code = _subdomain_template % fragments
return code, members
def compile_subdomain_code(code, classnames = None):
# Autodetect classnames:
_classnames = re.findall(r"class[ ]+([\w]+).*", code)
# Just a little assertion for safety:
if classnames is None:
classnames = _classnames
else:
assert all(a == b for (a,b) in zip(classnames, _classnames))
# Complete the code
code = math_header + \
"""
namespace dolfin
{
""" + code + \
"""
}
"""
# Compile the extension module
compiled_module = compile_extension_module(\
code,
dolfin_module_import=["common","mesh","function"])
# Construct instances of the compiled subdomain classes
subdomains = [getattr(compiled_module, name)() for name in classnames]
return subdomains
def compile_subdomains(expressions):
"""
Compile C++ string expressions into
:py:class:`SubDomain <dolfin.cpp.SubDomain>` instances.
*Arguments*
expressions
a string or a list of strings containing expressions
in C++ syntax.
If ``expressions`` is a 'str', it is interpreted as a C++ string
with complete implementations of subclasses of
:py:class:`SubDomain <dolfin.cpp.SubDomain>`.
The compiled subdomains returned will be in the same order
as they are defined in this code.
If it is a list, each item of the list is interpreted as
a logical 'inside' expression, and the compiled subdomains
returned will be in the same order as they occur in this list.
If an expression string contains a name, it is assumed to
be a scalar variable name, and is added as a public member
of the generated subdomain.
The exceptions are set in the
variable dolfin.compile_subdomains._builtins.
*Examples of usage*
.. code-block:: python
left = compile_subdomains("x[0] == 0")
right = compile_subdomains("x[1] == 1")
or equivalently using a list of strings
.. code-block:: python
bc = compile_subdomains(["x[0] == 0", "x[1] == 1"])
"""
#, which contains:
# %s
#""" % "\n".join(" " + b for b in _builtins)
if not isinstance(expressions, (list, str)):
raise TypeError, "expected a list of 'str' or a 'str'"
if isinstance(expressions, str):
expressions = [expressions]
if not all(isinstance(expr, str) for expr in expressions):
raise TypeError, "expected a list of 'str' or a 'str'"
all_code = []
all_members = []
classnames = []
for i, expr in enumerate(expressions):
classname = "SubDomain_" + hashlib.md5(expr).hexdigest()
code, members = expression_to_subdomain(expr, classname)
all_code.append(code)
all_members.append(members)
classnames.append(classname)
subdomains = compile_subdomain_code("\n".join(all_code), classnames)
# FIXME: Use all_members to add some handling of defaults
if len(subdomains) == 1:
return subdomains[0]
return subdomains
if __name__ == "__main__":
subdomains = compile_subdomains(["x[0] >= 1.0-DOLFIN_EPS", "on_boundary && x[1] < xlen+DOLFIN_EPS"])
print subdomains
|