File gen_fieldops.py¶
- module 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, mode='r')¶
Open stdin or stdout using a contextmanager From: http://stackoverflow.com/a/29824059/2043465
- returnType(f1, f2)¶
Determine a suitable return type, by seeing which field is 'larger'.
Variables
- operators = OrderedDict([("*", "multiplication"),("/", "division"),("+", "addition"),("-", "subtraction"),])¶
- 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>"""¶
- parser = argparse.ArgumentParser(description="Generate code for theFieldarithmetic operators")¶
- default¶
- help¶
- action¶
- dest¶
- args = parser.parse_args()¶
- index_var = "index"¶
- jz_var = "jz"¶
- mixed_base_ind_var = "base_ind"¶
- region_name = '"RGN_ALL"'¶
- region_loop = "BOUT_FOR_SERIAL"¶
- field3D = Field("Field3D",["x", "y", "z"],index_var=index_var,jz_var=jz_var,mixed_base_ind_var=mixed_base_ind_var,)¶
- field2D = Field("Field2D",["x", "y"],index_var=index_var,jz_var=jz_var,mixed_base_ind_var=mixed_base_ind_var,)¶
- fieldPerp = Field("FieldPerp",["x", "z"],index_var=index_var,jz_var=jz_var,mixed_base_ind_var=mixed_base_ind_var,)¶
- boutreal = Field("BoutReal",[],index_var=index_var,jz_var=jz_var,mixed_base_ind_var=mixed_base_ind_var,)¶
- fields = [field3D, field2D, fieldPerp, boutreal]¶
- env = jinja2.Environment(loader=jinja2.FileSystemLoader("."), trim_blocks=True)¶
- template = env.get_template("gen_fieldops.jinja")¶
- rhs = copy(rhs)¶
- lhs = copy(lhs)¶
- out = returnType(rhs, lhs)¶
- name¶
- 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,}¶
- Field : public object
Abstracts over BoutReals and Field2D/3D/Perps Provides some helper functions for writing function signatures and passing data
Public Functions
- __init__(self, field_type, dimensions, name=None, index_var=None, jz_var='jz', mixed_base_ind_var='base_ind')¶
- passByReference(self)¶
Returns "Type& name", except if field_type is BoutReal, in which case just returns "Type name"
- index(self)¶
Returns "[{index_var}]", except if field_type is BoutReal, in which case just returns ""
- mixed_index(self)¶
Returns "[{index_var} + {jz_var}]" if field_type is Field3D, self.index if Field2D or just returns "" for BoutReal
- base_index(self)¶
Returns "[{mixed_base_ind_var}]" if field_type is Field3D, Field2D or FieldPerp or just returns "" for BoutReal
- __eq__(self, other)¶
- __ne__(self, other)¶
- __repr__(self)¶
- __str__(self)¶