Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[analyticalproblems] add text_problem #401

Merged
merged 2 commits into from Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/pymor/analyticalproblems/text.py
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
# This file is part of the pyMOR project (http://www.pymor.org).
# Copyright 2013-2017 pyMOR developers and contributors. All rights reserved.
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

from pymor.analyticalproblems.elliptic import StationaryProblem
from pymor.core.defaults import defaults
from pymor.domaindescriptions.basic import RectDomain
from pymor.functions.basic import ConstantFunction, LincombFunction
from pymor.functions.bitmap import BitmapFunction
from pymor.parameters.functionals import ProjectionParameterFunctional
from pymor.parameters.spaces import CubicParameterSpace


@defaults('font_name')
def text_problem(text='pyMOR', font_name=None):
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from tempfile import NamedTemporaryFile

font_list = [font_name] if font_name else ['DejaVuSansMono.ttf', 'VeraMono.ttf', 'UbuntuMono-R.ttf', 'Arial.ttf']
font = None
for filename in font_list:
try:
font = ImageFont.truetype(filename, 64) # load some font from file of given size
except (OSError, IOError):
pass
if font is None:
raise ValueError('Could not load TrueType font')

size = font.getsize(text) # compute width and height of rendered text
size = (size[0] + 20, size[1] + 20) # add a border of 10 pixels around the text

def make_bitmap_function(char_num): # we need to genereate a BitmapFunction for each character
img = Image.new('L', size) # create new Image object of given dimensions
d = ImageDraw.Draw(img) # create ImageDraw object for the given Image

# in order to position the character correctly, we first draw all characters from the first
# up to the wanted character
d.text((10, 10), text[:char_num + 1], font=font, fill=255)

# next we erase all previous character by drawing a black rectangle
if char_num > 0:
d.rectangle(((0, 0), (font.getsize(text[:char_num])[0] + 10, size[1])), fill=0, outline=0)

# open a new temporary file
with NamedTemporaryFile(suffix='.png') as f: # after leaving this 'with' block, the temporary
# file is automatically deleted
img.save(f, format='png')
return BitmapFunction(f.name, bounding_box=[(0, 0), size], range=[0., 1.])

# create BitmapFunctions for each character
dfs = [make_bitmap_function(n) for n in range(len(text))]

# create an indicator function for the background
background = ConstantFunction(1., 2) - LincombFunction(dfs, np.ones(len(dfs)))

# form the linear combination
dfs = [background] + dfs
coefficients = [1] + [ProjectionParameterFunctional('diffusion', (len(text),), (i,)) for i in range(len(text))]
diffusion = LincombFunction(dfs, coefficients)

return StationaryProblem(
domain=RectDomain(dfs[1].bounding_box, bottom='neumann'),
neumann_data=ConstantFunction(-1., 2),
diffusion=diffusion,
parameter_space=CubicParameterSpace(diffusion.parameter_type, 0.1, 1.)
)
1 change: 1 addition & 0 deletions src/pymor/basic.py
Expand Up @@ -24,6 +24,7 @@
from pymor.analyticalproblems.helmholtz import helmholtz_problem
from pymor.analyticalproblems.instationary import InstationaryProblem
from pymor.analyticalproblems.thermalblock import thermal_block_problem
from pymor.analyticalproblems.text import text_problem

from pymor.core.cache import clear_caches, enable_caching, disable_caching
from pymor.core.defaults import print_defaults, write_defaults_to_file, load_defaults_from_file, set_defaults
Expand Down
16 changes: 15 additions & 1 deletion src/pymordemos/thermalblock_simple.py
Expand Up @@ -9,7 +9,7 @@
thermalblock_simple.py [options] MODEL ALG SNAPSHOTS RBSIZE TEST

Arguments:
MODEL High-dimensional model (pymor, fenics, ngsolve).
MODEL High-dimensional model (pymor, fenics, ngsolve, pymor-text).

ALG The model reduction algorithm to use
(naive, greedy, adaptive_greedy, pod).
Expand All @@ -36,6 +36,7 @@
GRID_INTERVALS = 100 # pyMOR/FEniCS
FENICS_ORDER = 2
NGS_ORDER = 4
TEXT = 'pyMOR'


####################################################################################################
Expand Down Expand Up @@ -208,6 +209,17 @@ def discretize_ngsolve():
parameter_space=CubicParameterSpace(op.parameter_type, 0.1, 1.))


def discretize_pymor_text():

# setup analytical problem
problem = text_problem(TEXT)

# discretize using continuous finite elements
d, _ = discretize_stationary_cg(problem, diameter=1.)

return d


####################################################################################################
# Reduction algorithms #
####################################################################################################
Expand Down Expand Up @@ -288,6 +300,8 @@ def main():
d = discretize_fenics()
elif MODEL == 'ngsolve':
d = discretize_ngsolve()
elif MODEL == 'pymor-text':
d = discretize_pymor_text()
else:
raise NotImplementedError

Expand Down
1 change: 1 addition & 0 deletions src/pymortests/demos.py
Expand Up @@ -62,6 +62,7 @@
('thermalblock_simple', ['pymor', 'naive', 2, 10, 10]),
('thermalblock_simple', ['fenics', 'greedy', 2, 10, 10]),
('thermalblock_simple', ['ngsolve', 'greedy', 2, 10, 10]),
('thermalblock_simple', ['pymor-text', 'adaptive_greedy', -1, 3, 3]),
)

THERMALBLOCK_GUI_ARGS = (
Expand Down