/usr/lib/python2.7/dist-packages/dolfin/compilemodules/subdomains.py is in python-dolfin 1.3.0+dfsg-2.
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | "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)
from dolfin.cpp import deprecation
import dolfin.cpp as cpp
__all__ = ["compile_subdomains", "CompiledSubDomain"]
_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(cpparg, 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(cpparg, str)
# Extract code fragments from the expr and defaults
fragments, members = expression_to_code_fragments(\
[cpparg], ["x", "on_boundary", "DOLFIN_EPS"])
# Generate code for inside()
insidecode = " return %s;" % cpparg
# Generate code for map()
#mapcode = "..."
# Connect the code fragments using the function template code
fragments["inside"] = insidecode
fragments["classname"] = classname
#fragments["map"] = mapcode
code = _subdomain_template % fragments
return code, members
def compile_subdomain_code(code, classname):
# Complete the code
code = math_header + \
"""
namespace dolfin
{
%s
}
""" % code
# Compile the extension module
compiled_module = compile_extension_module(code)
# Get compiled class
return getattr(compiled_module, classname)
def CompiledSubDomain(cppcode, **kwargs):
"""
Compile a C++ string expression into a
:py:class:`SubDomain <dolfin.cpp.SubDomain>` instance.
*Arguments*
cppcode
a string containing an expression in C++ syntax.
If the string contains a name, it is assumed to be a scalar
variable name, and is added as a public member of the generated
subdomain. All such members need a default initial value.
If the string contains a class name it is interpreted as a
complete implementations of subclasses of :py:class:`SubDomain
<dolfin.cpp.SubDomain>`.
*Examples of usage*
.. code-block:: python
left = CompiledSubDomain("near(x[0], 0) && on_boundary")
right = CompiledSubDomain("near(x[1], 1) && on_boundary")
center = CompiledSubDomain("near(x[1], c)", c = 0.5)
"""
if not isinstance(cppcode, str):
raise TypeError("expected a 'str'")
if isinstance(cppcode, str) and "class" in cppcode and \
"SubDomain" in cppcode:
members = []
classname = re.findall(r"class[ ]+([\w]+).*", code)[0]
code = cppcode
else:
classname = "CompiledSubDomain" + hashlib.md5(cppcode).hexdigest()
code, members = expression_to_subdomain(cppcode, classname)
SubDomainClass = compile_subdomain_code(code, classname)
# Check passed default arguments
not_allowed = [n for n in dir(cpp.SubDomain) if n[0] !="_"]
not_allowed += ["cppcode"]
if not all(member in kwargs for member in members):
missing = []
for member in members:
if member not in kwargs:
missing.append(member)
missing = ", ".join("'%s'" % miss for miss in missing)
raise RuntimeError("expected a default value to all member "\
"variables in the SubDomain. Missing: %s." % missing)
for name in kwargs.keys():
if name in not_allowed:
raise RuntimeError("Parameter name: '%s' is not allowed. It is "\
"part of the interface of SubDomain" % name)
if not (all(isinstance(value, (int, float)) \
for value in kwargs.values())):
raise TypeError("expected default arguments for member variables "\
"to be scalars.")
# Store compile arguments for possible later use
SubDomainClass.cppcode = cppcode
# Instantiate CompiledSubDomain
subdomain = SubDomainClass()
# Set default variables
for member, value in kwargs.items():
setattr(subdomain, member, value)
return subdomain
def compile_subdomains(cppcode):
"""
Compile C++ string expressions into SubDomain instances.
*Arguments*
expressions
a string or a list of strings containing expressions in C++ syntax.
NOTE: This function is deprecated. Use CompiledSubDomain instead.
If expressions is a `str`, it is interpreted as a C++ string with
complete implementations of subclasses of 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.
*Examples of usage*
.. code-block:: python
left = compile_subdomains("x[0] == 0")
right = compile_subdomains("x[1] == 1")
or
.. code-block:: python
bc = compile_subdomains(["x[0] == 0", "x[1] == 1"])
"""
deprecation("compile_subdomains", "1.3.0", \
"compiled_subdomains has been renamed to CompiledSubDomain.")
# If passing a list we compile each SubDomain on its own
if isinstance(cppcode, list):
return [CompiledSubDomain(code_str) for code_str in cppcode]
return CompiledSubDomain(cppcode)
|