File gen_fieldops.py

namespace gen_fieldops

Code-generator for arithmetic operators on Field2Ds/Field3Ds

This uses the jinja template in gen_fieldops.jinja to generate code
for the arithmetic operators, and prints to stdout.

The `Field` class provides some helper functions for determining how to
pass a variable by reference or pointer, and how to name arguments in
function signatures. This allows us to push some logic into the
templates themselves.

Functions

smart_open(filename filename, mode mode = 'r')

Open stdin or stdout using a contextmanager

From: http://stackoverflow.com/a/29824059/2043465

returnType(f1 f1, f2 f2)

Determine a suitable return type, by seeing which field is 'larger'.

Variables

gen_fieldops.operators= OrderedDict([ ('*', 'multiplication'), ('/', 'division'), ('+', 'addition'), ('-', 'subtraction'),])
string gen_fieldops.header= """// This file is autogenerated - see gen_fieldops.py#include <bout/mesh.hxx>#include <bout/region.hxx>#include <field2d.hxx>#include <field3d.hxx>#include <globals.hxx>#include <interpolation.hxx>"""
gen_fieldops.parser = argparse.ArgumentParser(description="Generate code for the Field arithmetic operators")
default
help
action
False
dest
gen_fieldops.args = parser.parse_args()
string gen_fieldops.index_var = 'index'
string gen_fieldops.jz_var = 'jz'
string gen_fieldops.mixed_base_ind_var = "base_ind"
string gen_fieldops.region_name = '"RGN_ALL"'
string gen_fieldops.region_loop = 'BOUT_FOR_SERIAL'
gen_fieldops.field3D = Field('Field3D', ['x', 'y', 'z'], index_var=index_var, jz_var = jz_var, mixed_base_ind_var = mixed_base_ind_var)
gen_fieldops.field2D = Field('Field2D', ['x', 'y'], index_var=index_var, jz_var = jz_var, mixed_base_ind_var = mixed_base_ind_var)
gen_fieldops.fieldPerp = Field('FieldPerp', ['x', 'z'], index_var=index_var, jz_var = jz_var, mixed_base_ind_var = mixed_base_ind_var)
gen_fieldops.boutreal = Field('BoutReal', [], index_var=index_var, jz_var = jz_var, mixed_base_ind_var = mixed_base_ind_var)
list gen_fieldops.fields = [field3D, field2D, fieldPerp, boutreal]
gen_fieldops.env= jinja2.Environment(loader=jinja2.FileSystemLoader('.'), trim_blocks=True)
gen_fieldops.template = env.get_template("gen_fieldops.jinja")
gen_fieldops.rhs = copy(rhs)
gen_fieldops.lhs = copy(lhs)
gen_fieldops.out = returnType(rhs, lhs)
name
dictionary gen_fieldops.template_args= { 'operator': operator, 'operator_name': operator_name, # 'out': out, 'lhs': lhs, 'rhs': rhs, # 'region_loop': region_loop, 'region_name': region_name, # 'index_var': index_var, 'mixed_base_ind': mixed_base_ind_var, 'jz_var': jz_var, }
class Field

Abstracts over BoutReals and Field2D/3D/Perps

Provides some helper functions for writing function signatures and
passing data

Public Functions

__init__(self self, field_type field_type, dimensions dimensions, name name = None, index_var index_var = None, jz_var jz_var = 'jz', mixed_base_ind_var mixed_base_ind_var = 'base_ind')
passByReference(self self)

Returns "Type& name", except if field_type is BoutReal,
in which case just returns "Type name"

index(self self)

Returns "[{index_var}]", except if field_type is BoutReal,
in which case just returns ""

mixed_index(self self)

Returns "[{index_var} + {jz_var}]" if field_type is Field3D,
self.index if Field2D or just returns "" for BoutReal

base_index(self self)

Returns "[{mixed_base_ind_var}]" if field_type is Field3D, Field2D or FieldPerp
or just returns "" for BoutReal

__eq__(self self, other other)
__ne__(self self, other other)
__repr__(self self)
__str__(self self)

Public Members

field_type
dimensions
name
index_var
jz_var
mixed_base_ind_var
region_type