Eastsheng's Wiki

LBM/PALABOS:userInput

2024-05-29 18:37:28

[toc]

userInput主要读取参数

代码位置

1
2
3
4
5
6
palabos/examples/codesByTopic/userInput
├── build
├── CMakeLists.txt
├── demo.xml
├── readParameters.cpp
└── tmp

代码分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

#include "palabos2D.h"
#include "palabos2D.hh"

using namespace plb;
using namespace std;
typedef double T;

int main(int argc, char *argv[])
{
plbInit(&argc, &argv);
global::directories().setOutputDir("./tmp/");

try {
// 1. 从命令行读取参数。
int value = 27;
global::argv(1).readNoThrow(value);
pcout << value << endl;

// 2. 从XML文件中读取参数。
string fName("demo.xml");

XMLreader document(fName); // 读取xml文件

string text;
document["MyApp"]["Messages"]["Welcome"].read(text);
pcout << "TEXT:" << text << endl;

int number;
bool boolVal;
T someFloat;
std::vector<int> numbers;

document["MyApp"]["Value"]["OneNumber"].read(number); // 读取整数
pcout << "ONE NUMBER:" << number << endl;

document["MyApp"]["OneBool"].read(boolVal); // 读取布尔值
pcout << "ONE BOOL:" << boolVal << endl;

document["MyApp"]["OneFloat"].read(someFloat); // 读取双精度浮点数
pcout << "ONE FLOAT:" << someFloat << endl;

document["MyApp"]["TwoNumbers"].read(numbers); // 读取矢量
pcout << "TWO NUMBERS:" << numbers[0] << " " << numbers[1] << endl;

double nestedVal;
XMLreaderProxy nested = document["MyApp"]["Nested"]; // 读取内嵌数据:id和变量值
for (; nested.isValid(); nested = nested.iterId()) {
nested["NestedData"].read(nestedVal);
pcout << "Nested value at id " << nested.getId() << ": " << nestedVal << endl;
}

// 3. 将数据写入XML文件
//
XMLwriter demo2; // 实例化一个demo2对象
XMLwriter &myApp = demo2["MyApp"]; // 应该类似字典或者dataframe之类的

XMLwriter &messages = myApp["Messages"]; // 二级变量
messages.setString("Text of message"); // 设置字符串值,默认id为0
messages["Welcome"].setString("Welcome to MyApp");
messages["FareWell"].setString("Good bye");
XMLwriter &messages2 = myApp["Messages"][2]; // 指定id为2
messages2["Welcome"].setString("Welcome again"); // 设置id 2字符串值

vector<int> twoNumbers;
twoNumbers.push_back(12);
twoNumbers.push_back(24);
myApp["Windows"]["Window"];
myApp["OneNumber"][3].set(12); // 设置id=3的值
myApp["OneNumber"][2].set(2); // 设置id=2的值
myApp["OneNumber"][20].set(20); // 设置id=20的值
myApp["TwoNumbers"].set(twoNumbers); // 设置vector值
myApp["OneFloat"].set(12.34); // 设置浮点数值

demo2.print("demo2.xml"); // 写入文件

}
// 捕获和处理PALABOS输入输入出异常,并输出异常信息
catch (PlbIOException &exception)
{
pcout << exception.what() << endl;
return -1;
}
}

demo.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" ?>
<MyApp>
<!-- Settings for MyApp -->
<Messages>
Text of the message
<Welcome>Welcome to MyApp</Welcome>
<Farewell id="2">Thank you for using MyApp</Farewell>
</Messages>
<Windows>
<Window name="MainFrame" x="5" y="15" w="400" h="250" />
</Windows>
<Connection ip="192.168.0.1" timeout="123.456000" />
<Value>
<OneNumber> 12 </OneNumber>
</Value>
<Nested id="4">
<NestedData id="-1">
2
</NestedData>
</Nested>

<TwoNumbers> 12 24 </TwoNumbers>
<OneBool id="3"> true </OneBool>

<OneFloat> 12.34 </OneFloat>

<Nested id="2">
<NestedData id="-1">
3.142
</NestedData>
</Nested>
<Nested id="-1">
<NestedData id="-1">
-1
</NestedData>
</Nested>
<Nested id="300">
<NestedData id="-1">
300
</NestedData>
</Nested>
</MyApp>

demo2.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" ?>
<MyApp>
<!-- Settings for MyApp -->
<Messages>
Text of the message
<Welcome>Welcome to MyApp</Welcome>
<Farewell id="2">Thank you for using MyApp</Farewell>
</Messages>
<Windows>
<Window name="MainFrame" x="5" y="15" w="400" h="250" />
</Windows>
<Connection ip="192.168.0.1" timeout="123.456000" />
<Value>
<OneNumber> 12 </OneNumber>
</Value>
<Nested id="4">
<NestedData id="-1">
2
</NestedData>
</Nested>

<TwoNumbers> 12 24 </TwoNumbers>
<OneBool id="3"> true </OneBool>

<OneFloat> 12.34 </OneFloat>

<Nested id="2">
<NestedData id="-1">
3.142
</NestedData>
</Nested>
<Nested id="-1">
<NestedData id="-1">
-1
</NestedData>
</Nested>
<Nested id="300">
<NestedData id="-1">
300
</NestedData>
</Nested>
</MyApp>

执行

1
2
3
4
5
6
7
8
9
10
11
12
13
cd build && cmake .. && make && cd ..
mpirun -np 8 ./readParameters 20
# ----------------------------------- output -----------------------------------
20
TEXT:Welcome to MyApp
ONE NUMBER:12
ONE BOOL:1
ONE FLOAT:12.34
TWO NUMBERS:12 24
Nested value at id -1: -1
Nested value at id 2: 3.142
Nested value at id 4: 2
Nested value at id 300: 300