5.5 FieldConvert in NekPy

The Python interface allows the user to instantiate input, output, and process modules by calling the static Create method of the InputModule, ProcessModule, and OutputModule, register configuration options, and run them. For example, consider the command:

FieldConvert -n 10 -m wss:bnd=0 session.xml field.fld  field_wss.fld

A Python script performing the same task is given below.

1import sys 
2from NekPy.FieldUtils import * 
3 
4field = Field(sys.argv, output_points=10) 
5 
6InputModule.Create("xml", field, "session.xml").Run() 
7InputModule.Create("fld", field, "field.fld").Run() 
8ProcessModule.Create("wss", field, bnd="0").Run() 
9OutputModule.Create("fld" , field, "field_wss.fld").Run()

The key points are that the FieldConvert command line options, in this case output-points, are passed to the constructor of Field. The configuration options for a given module are passed to the static Create method of the InputModule, ProcessModule, and OutputModule. This creates the corresponding module and the modules can be run immediately after instantiation. Note that the first parameter of the Create method has to be the key for a given module, the second is the field variable and for input and output modules the remaining arguments may identify the input and output files for a given module. Optionally we can explicitly specify the file type of an input module using the "infile" keyword and the "outfile" keyword for output modules.

1import sys 
2from NekPy.FieldUtils import * 
3 
4field = Field(sys.argv, output_points=10) 
5 
6InputModule.Create("xml", field, infile={"xml": "session.xml"}).Run() 
7InputModule.Create("fld", field, infile={"fld": "field.fld"}).Run() 
8ProcessModule.Create("wss", field, bnd="0").Run() 
9OutputModule.Create("fld" , field, outfile="field_wss.fld").Run()

It can also emulate the functionality of FieldConvert when using the nparts option in the following way. Here session_xml is a directory containing the mesh partitioned into 2.

1import sys 
2from NekPy.FieldUtils import * 
3 
4field = Field(sys.argv, nparts=2, force_output=True, error=True) 
5 
6inputxml = InputModule.Create("xml", field, "session_xml") 
7inputfld = InputModule.Create("fld", field, "field.fld") 
8processwss = ProcessModule.Create("wss", field, bnd="2") 
9outputfld = OutputModule.Create("fld", field, "field_wss.fld") 
10 
11for part in range(2): 
12      field.NewPartition(sys.argv, part) 
13      inputxml.Run() 
14      inputfld.Run() 
15      processwss.Run() 
16      outputfld.Run() 
17 
18OutputModule.Create("info", field, "field_wss_b0.fld", nparts=2).Run()

The number of partitions is looped over, with NewPartition called at the start of each. When using OutputModule, the info module must be used in order to obtain the correct result.