This instance will be upgraded to Heptapod 0.26.0rc1 on 2021-10-26 at 14:30 UTC+2 (a few minutes of down time)

Commit 891b5c76 authored by Pierre Augier's avatar Pierre Augier
Browse files

Merge + use fluiddoc.mathmacro.

......@@ -114,7 +114,7 @@ extensions = [
'sphinx.ext.mathjax',
'sphinx.ext.viewcode', 'sphinx.ext.autosummary',
'numpydoc',
# 'mathmacro',
'fluiddoc.mathmacro',
# 'breathe'
]
......
import numpy as np
from fluidfft import import_fft_class
from fluidfft.fft2d.operators import OperatorsPseudoSpectral2D
nx = ny = 100
lx = ly = 2 * np.pi
# Create an FFT object
o = FFTClass(nx, ny)
# And an operator object
oper = OperatorsPseudoSpectral2D(nx, ny, lx, ly, fft='fft2d.with_fftw2d')
u = np.sin(oper.XX + oper.YY)
u_fft = o.fft(u)
u_fft = oper.fft(u)
px_u_fft, py_u_fft = oper.gradfft_from_fft(u_fft)
px_u = o.ifft(px_u_fft)
py_u = o.ifft(py_u_fft)
px_u = oper.ifft(px_u_fft)
py_u = oper.ifft(py_u_fft)
grad_u = (px_u, py_u)
"""Sphinx extension provide a new directive *mathmacro*.
This extension has to be added after the other math extension since it
redefined the math directive and the math role. For example, like this
(in the conf.py file)::
extensions = [
'sphinx.ext.autodoc', 'sphinx.ext.doctest',
'sphinx.ext.mathjax',
'sphinx.ext.viewcode', 'sphinx.ext.autosummary',
'numpydoc',
'mathmacro']
"""
from __future__ import print_function
import re
from docutils.parsers.rst.directives.misc import Replace
from sphinx.ext.mathbase import MathDirective
from sphinx.ext.mathbase import math_role
def multiple_replacer(replace_dict):
"""Return a function replacing doing multiple replacements.
The produced function replace `replace_dict.keys()` by
`replace_dict.values`, respectively.
"""
def replacement_function(match):
s = match.group(0)
end = s[-1]
if re.match(r'[\W_]', end):
return replace_dict[s[:-1]]+end
else:
return replace_dict[s]
pattern = "|".join([re.escape(k)+r'[\W_]'
for k in replace_dict.keys()])
pattern = re.compile(pattern, re.M)
return lambda string: pattern.sub(replacement_function, string)
def multiple_replace(string, replace_dict):
mreplace = multiple_replacer(replace_dict)
return mreplace(string)
class MathMacro(Replace):
"""Directive defining a math macro."""
def run(self):
if not hasattr(self.state.document, 'math_macros'):
self.state.document.math_macros = {}
latex_key = '\\'+self.state.parent.rawsource.split('|')[1]
self.state.document.math_macros[latex_key] = ''.join(self.content)
self.content[0] = ':math:`'+self.content[0]
self.content[-1] = self.content[-1]+'`'
return super(MathMacro, self).run()
class NewMathDirective(MathDirective):
"""New math block directive parsing the latex code."""
def run(self):
try:
math_macros = self.state.document.math_macros
except AttributeError:
pass
else:
if math_macros:
multiple_replace = multiple_replacer(math_macros)
for i, c in enumerate(self.content):
self.content[i] = multiple_replace(c)
for i, a in enumerate(self.arguments):
self.arguments[i] = multiple_replace(a)
return super(NewMathDirective, self).run()
def new_math_role(role, rawtext, text, lineno, inliner,
options={}, content=[]):
"""New math role parsing the latex code."""
try:
math_macros = inliner.document.math_macros
except AttributeError:
pass
else:
if math_macros:
rawtext = multiple_replace(rawtext, math_macros)
text = rawtext.split('`')[1]
return math_role(role, rawtext, text, lineno, inliner,
options=options, content=content)
def setup(app):
app.add_role('math', new_math_role)
app.add_directive('math', NewMathDirective)
app.add_directive('mathmacro', MathMacro)
......@@ -58,7 +58,6 @@ can_import = _d['can_import']
can_import_cython = can_import('cython')
can_import_mpi4py = can_import('mpi4py', '2.0.0')
can_import_pythran = can_import('pythran')
if can_import_cython:
from Cython.Compiler.Main import \
......@@ -104,23 +103,21 @@ path_lib = 'build/lib.' + '-'.join(
[platform.system().lower(), platform.machine(), short_version])
def check_deps():
def check_and_print(pkg='', result=None):
if result is None:
result = can_import(pkg)
def check_and_print(pkg='', result=None, line_above=False, line_below=False):
if result is None:
result = can_import(pkg)
print('{} installed: '.format(pkg).rjust(25) + repr(result))
if line_above:
print('*' * 50)
print('{} installed: '.format(pkg).rjust(25) + repr(result))
if line_below:
print('*' * 50)
print('*' * 50)
check_and_print('numpy')
check_and_print('mpi4py', can_import_mpi4py)
check_and_print('cython', can_import_cython)
check_and_print('pythran', can_import_pythran)
check_and_print('mako')
print('*' * 50)
check_deps()
check_and_print('numpy', line_above=True)
check_and_print('mpi4py', can_import_mpi4py)
check_and_print('cython', can_import_cython)
check_and_print('mako', line_below=True)
def modification_date(filename):
......@@ -503,11 +500,13 @@ def make_extensions(extensions,
def make_pythran_extensions(modules):
# building with pythran
try:
from pythran.dist import PythranExtension
except ImportError:
pass
can_import_pythran = can_import('pythran')
check_and_print('pythran', can_import_pythran, True, True)
if not can_import_pythran:
print('Pythran extensions will not built: ', modules)
return []
from pythran.dist import PythranExtension
develop = sys.argv[-1] == 'develop'
extensions = []
......@@ -522,7 +521,9 @@ def make_pythran_extensions(modules):
pext = PythranExtension(mod, [py_file],)
pext.include_dirs.append(np.get_include())
# bug pythran extension...
pext.extra_compile_args.extend(['-O3', '-march=native'])
compile_arch = os.getenv('CARCH', 'native')
pext.extra_compile_args.extend(['-O3',
'-march={}'.format(compile_arch)])
# pext.extra_compile_args.append('-fopenmp')
# pext.extra_link_args.extend([])
extensions.append(pext)
......
......@@ -24,7 +24,7 @@ from numpy.__config__ import get_info
from src_cy.make_files_with_mako import make_pyx_files
from purepymake import (
Extension, make_extensions, monkeypatch_parallel_build,
make_pythran_extensions, can_import_pythran)
make_pythran_extensions)
try:
from config import parse_config
......@@ -215,10 +215,9 @@ ext_modules = make_extensions(
ext_modules, include_dirs=include_dirs,
lib_flags_dict=lib_flags_dict, lib_dirs_dict=lib_dirs_dict)
if can_import_pythran:
ext_modules.extend(make_pythran_extensions(
['fluidfft.fft2d.util_pythran',
'fluidfft.fft3d.util_pythran']))
ext_modules.extend(make_pythran_extensions(
['fluidfft.fft2d.util_pythran',
'fluidfft.fft3d.util_pythran']))
# from purepymake import can_import_cython
# if can_import_cython:
......
......@@ -29,4 +29,15 @@ include_dir =
library_dir =
[environ]
## Uncomment and specify the following options to modify compilation of
## extensions.
## To modify compiler used to build Cython extensions:
# MPICXX =
# LDSHARED =
## To modify target architecture while building Pythran extensions
## Useful when cross-compiling. See whether it is required by comparing:
## gcc -march=native -Q --help=target
## gcc -march=$CARCH -Q --help=target
# CARCH =
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment