Skip to content

Commit

Permalink
Split write_box method -> info_box + write_box
Browse files Browse the repository at this point in the history
Allows modification of template variables
Ensure grid_info order using OrderedDict
  • Loading branch information
ashwinvis committed Aug 3, 2020
1 parent 2fae4b3 commit 87ab5e7
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions src/snek5000/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import inspect
import math
import sys
from collections import OrderedDict
from math import pi

from .util import docstring_params
Expand Down Expand Up @@ -93,7 +94,7 @@ class Operators:

@staticmethod
def _complete_params_with_default(params):
"""This static method is used to complete the *params* container.
"""This static method is used to complete the *params.oper* container.
"""
attribs = {
"nx": 8,
Expand Down Expand Up @@ -374,16 +375,12 @@ def produce_long_str_describing_oper(self, oper_method="Base"):
string += f"- {key} = {value}\n"
return f"Nek5000 operator:\n{string}"

def write_box(self, template, fp=sys.stdout, comments=""):
"""Write the .box file which is input for the ``genbox`` meshing
tool.
def info_box(self, comments=""):
"""Gather information for writing a box file.
Parameters
----------
template : jinja2.environment.Template
Template instance like :code:`abl.templates.box`
fp : io.TextIOWrapper
File handler to write to
Returns
-------
dict
"""
params = self.params
Expand All @@ -399,7 +396,6 @@ def write_box(self, template, fp=sys.stdout, comments=""):
Note that the character bcs _must_ have 3 spaces.
"""

def _str_grid(*args):
fmt = "{:.4f} {:.4f} {:.4f}"
args = (float(value) for value in args)
Expand Down Expand Up @@ -431,13 +427,47 @@ def _str_grid(*args):
bc.ljust(3) for bc in boundary
),
}
options = {
info = {
"comments": comments,
"dim": str(-params.oper.dim),
"grid_info": grid_info,
"nb_fields": str(self.nb_fields), # scalars + velocity
}
return info

def write_box(self, template, fp=sys.stdout, comments=""):
"""Write the .box file which is input for the ``genbox`` meshing
tool.
Parameters
----------
template : jinja2.environment.Template
Template instance like :code:`abl.templates.box`
fp : io.TextIOWrapper
File handler to write to
"""
options = self.info_box(comments)

# A hack to ensure that the rows are ordered properly. Deduced from the
# keys of the dictionary grid_info
#
# nelx nely nelz
# x0 x1 ratio
# y0 y1 ratio
# z0 z1 ratio
# BCs: (cbx0, cbx1, cby0, cby1, cbz0, cbz1)

grid_info = options['grid_info']
ordered_keys = sorted(
grid_info.keys(),
key=lambda k: {'n': 0, 'x': 1, 'y': 2, 'z': 3, 'B': 4}[k.strip()[0]]
)
options["grid_info"] = OrderedDict([
(key, grid_info[key]) for key in ordered_keys
])

# Write the box file
output = template.render(**options)
fp.write(output)

Expand Down

0 comments on commit 87ab5e7

Please sign in to comment.