Skip to content

Commit

Permalink
Merge branch 'vidartf/topic-ufl2unicode'
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Sandve Alnæs committed Sep 13, 2017
2 parents 4fbc66b + b0caf92 commit c4511ed
Show file tree
Hide file tree
Showing 5 changed files with 810 additions and 2 deletions.
2 changes: 1 addition & 1 deletion demo/Poisson.ufl
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ u = TrialFunction(element)
v = TestFunction(element)
f = Coefficient(element)

a = dot(grad(v), grad(u))*dx(degree=1)
a = inner(grad(v), grad(u))*dx(degree=1)
L = v*f*dx(degree=2)
11 changes: 10 additions & 1 deletion scripts/ufl-convert
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from pprint import pprint
from ufl.algorithms import tree_format, compute_form_data
from ufl.formatting.ufl2dot import ufl2dot
from ufl.formatting.ufl2latex import forms2latexdocument
from ufl.formatting.ufl2unicode import form2unicode
from ufl.algorithms.formfiles import load_ufl_file

# --- Utilities
Expand Down Expand Up @@ -62,7 +63,7 @@ option_list = [ \
opt("labeling", "l", "str", "repr", "Set to 'repr' or 'compact' for different naming of graph nodes."),
opt("compile", "c", "int", 0, "'Compile' forms: apply expression transformations like in a quadrature based form compilation. Only used for latex formatting."),
# Output formats:
opt("format", "f", "str", "", "Rendering format (str, repr, tree, dot, latex)."),
opt("format", "f", "str", "", "Rendering format (str, repr, tree, dot, latex, unicode)."),
opt("filetype", "t", "str", "", "Output file type (txt, py, dot, tex, ps, pdf, png)."),
]

Expand Down Expand Up @@ -105,6 +106,12 @@ for arg in args:
format = "tex"
if format == "tex":
rendered = forms2latexdocument(forms, uflfilename, compile=options.compile)
elif format == "unicode":
data = []
for form, form_data in zip(forms, form_datas):
tmp = form2unicode(form, form_data)
data.append(tmp)
rendered = "\n\n".join(data)
elif format in ("str", "repr", "tree"):
data = []
for i, fd in enumerate(form_datas):
Expand Down Expand Up @@ -154,6 +161,8 @@ for arg in args:
filetype = "dot"
elif format == "tex":
filetype = "tex"
elif format == "unicode":
filetype = "txt"

# Guess that the filetype is the ext, usually the case
ext = filetype
Expand Down
61 changes: 61 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import pytest

import os
import glob

import ufl
from ufl import as_ufl, inner, dx
from ufl.algorithms import compute_form_data
from ufl.algorithms.formfiles import load_ufl_file



class Tester:
Expand Down Expand Up @@ -56,9 +61,65 @@ def assertEqualValues(self, A, B):
def self():
return Tester()


def testspath():
return os.path.abspath(os.path.dirname(__file__))


def filespath():
return os.path.abspath(os.path.join(testspath(), "../demo"))


class UFLTestDataBase(object):
def __init__(self):
self.filespath = filespath()
self.cache = {}
self.names = glob.glob(os.path.join(self.filespath, "*.ufl"))
# Filter out files startign with udnerscore
self.names = [n for n in self.names
if not os.path.basename(n).startswith('_')]

def __len__(self):
return len(self.names)

def __iter__(self):
return self.keys()

def __hasitem__(self, name):
return names in self.names

def keys(self):
return iter(self.names)

def values(self):
return (self[name] for name in self.names)

def items(self):
return ((name, self[name]) for name in self.names)

def __getitem__(self, name):
if isinstance(name, int):
name = self.names[name]
# Cache file reads
content = self.cache.get(name)
if content is None:
content = load_ufl_file(name)
self.cache[name] = content
return content

_db = UFLTestDataBase()


def _example_paths():
return _db.values()

_all_cells = [ufl.interval, ufl.triangle, ufl.tetrahedron]


@pytest.fixture(params=_all_cells)
def cell(request):
return request.param

@pytest.fixture(params=_example_paths())
def example_files(request):
return request.param
36 changes: 36 additions & 0 deletions test/test_unicode_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

from six import text_type

from ufl.algorithms import compute_form_data
from ufl.formatting.ufl2unicode import form2unicode


def valid_forms(forms_list):
forms = []
form_datas = []
for f in forms_list:
fd = None
try:
fd = compute_form_data(f)
except:
fd = None
if fd is not None:
forms.append(f)
form_datas.append(fd)
return forms, form_datas


def test_convert_examples(example_files):
# Get example forms that can be analysed
forms, form_datas = valid_forms(example_files.forms)
if not forms:
return

# Mainly tests for execution without errors
data = []
for form, form_data in zip(forms, form_datas):
tmp = form2unicode(form, form_data)
data.append(tmp)
rendered = u"\n\n".join(data)
assert isinstance(rendered, text_type)
assert len(rendered)

0 comments on commit c4511ed

Please sign in to comment.