cig_create_input_data.py

We will create input data using the Concepts Input Generator (CIG), which was written in Python. In another tutorial, we will load the input data into a C++ code. See Concepts tutorials for more tutorials.

Introduction

Imagine you have a piece of code written in C++ and there are some parameters, which you want to modify after each run, e.g. you want to solve the Maxwell equations with various relative perimittivities or you want to try out different refinements of the mesh.

To avoid recompiling your whole code over and over again, it makes sense to encapsulate the definition of all parameters, which can vary, from the main code. Using CIG together with the class concepts::InOutParameters is one possible way to achieve this.

Commented Program

First, let us define some inputs which we want to pass to our main code in C++.

4 some_project_name = 'cig_input_data'
5 some_folder = '.'
6 
7 some_string = "Live long and prosper!"
8 some_float = 3.14159265359
9 some_int = 42
10 some_boolean = True

While the input parameters above use standard python classes, there is a special class for the mesh, which is also part of CIG.

13 some_mesh = RhomboidMesh()
14 
15 p1=some_mesh.addPoint((0, 0.))
16 p2=some_mesh.addPoint((1, 0.))
17 p3=some_mesh.addPoint((1, 1.))
18 p4=some_mesh.addPoint((0, 1.))
19 
20 some_mesh.setPointAttr(Nr=p1,Attr=777)
21 some_mesh.addQuad(p1,p2,p3,p4)

Finally, we specify some refinement strategy: We perform one h-refinement, followed by two p-refinements and finally three hp-refinements towards the vertex with attribute number 777, which is the point p1=(0,0) .

24 some_refinement = "h1,p2,hp3->v777"

Now we initialize a cigProject.Project object and write all the parameters above into this object

27 project = Project()
28 
29 project['some_project_name'] = some_project_name
30 project['some_folder'] = some_folder
31 project['some_string'] = some_string
32 project['some_float'] = some_float
33 project['some_int'] = some_int
34 project['some_boolean'] = some_boolean

While the names above were arbitrarily chosen, the following two names are keywords (e.g. using the name 'some_mesh' would not work).

37 project['mesh'] = some_mesh
38 project['refinement'] = some_refinement

With the following command, we finally generate our CIG input data, which consist of a cig-file and several dat-files for the mesh.

40 project.save(filename=some_project_name,dataDir=some_folder)

The Python code can be directly executed with the command

python cig_create_input_data.py

If nothing went wrong, there should be a cig-file and five dat-files in the directory, where the python code was executed.

Click here to see how to load the input data into a C++ code.

Complete Source Code

1 from cigProject import Project
2 from cigPatterns import RhomboidMesh
3 
4 some_project_name = 'cig_input_data'
5 some_folder = '.'
6 
7 some_string = "Live long and prosper!"
8 some_float = 3.14159265359
9 some_int = 42
10 some_boolean = True
11 
12 some_mesh = RhomboidMesh()
13 
14 p1=some_mesh.addPoint((0, 0.))
15 p2=some_mesh.addPoint((1, 0.))
16 p3=some_mesh.addPoint((1, 1.))
17 p4=some_mesh.addPoint((0, 1.))
18 
19 some_mesh.setPointAttr(Nr=p1,Attr=777)
20 some_mesh.addQuad(p1,p2,p3,p4)
21 
22 some_refinement = "h1,p2,hp3->v777"
23 
24 project = Project()
25 
26 project['some_project_name'] = some_project_name
27 project['some_folder'] = some_folder
28 project['some_string'] = some_string
29 project['some_float'] = some_float
30 project['some_int'] = some_int
31 project['some_boolean'] = some_boolean
32 
33 project['mesh'] = some_mesh
34 project['refinement'] = some_refinement
35 
36 project.save(filename=some_project_name,dataDir=some_folder)
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich