source: trunk/src/mesh/test_s2.cc @ 352

Last change on this file since 352 was 343, checked in by cxsong, 18 years ago

an example of serializer managing field objects using different meshes

File size: 3.3 KB
Line 
1#include <stdio.h>
2#include "serializer.h"
3#include "field.h"
4#include "grid2d.h"
5
6//
7// This example show how to create field & grid1d objects, put into
8// a serializer and ask for a serialized byte stream.
9//
10
11#define Num_points 20
12
13static double points[20] = {
14        0.261243291268,
15        0.159055762008,
16        0.214550893827,
17        0.11741510008,
18        0.119735699467,
19        0.15196145742,
20        0.0245663797288,
21        0.128903081711,
22        0.0927746958394,
23        0.0465364541799,
24        0.531606236106,
25        0.252448742721,
26        0.348575614391,
27        0.180939456908,
28        0.0251118046222,
29        0.810354715199,
30        0.0980414196039,
31        0.392354903151,
32        0.151346474849,
33        0.368703495654
34};
35
36static double points2[20] = {
37        0.0245663797288,
38        0.128903081711,
39        0.0927746958394,
40        0.0465364541799,
41        0.531606236106,
42        0.252448742721,
43        0.348575614391,
44        0.180939456908,
45        0.0251118046222,
46        0.810354715199,
47        0.0980414196039,
48        0.392354903151,
49        0.151346474849,
50        0.368703495654,
51        0.261243291268,
52        0.159055762008,
53        0.214550893827,
54        0.11741510008,
55        0.119735699467,
56        0.15196145742
57};
58
59static double xy_pts[] = {
60        0.309504615287, 0.844069129715,
61        0.269863116215, 0.589394224151,
62        0.948725301748, 0.226146486693,
63        0.844001851438, 0.139117111982,
64        0.141301087635, 0.847379885543,
65        0.913736318664, 0.166307778641,
66        0.134835615817, 0.182195034894,
67        0.151951467689, 0.848317450773,
68        0.671395143807, 0.138181958412,
69        0.424175028887, 0.10971049923,
70        0.904360553671, 0.587825545849,
71        0.58394908746, 0.432312932998,
72        0.88346489886, 0.394555137211,
73        0.288191101648, 0.627845397977,
74        0.197603800892, 0.127081587039,
75        0.86023335618, 0.942017313532,
76        0.484988525736, 0.202152039484,
77        0.56932761081, 0.689154883236,
78        0.62612255133, 0.241720210408,
79        0.591576322723, 0.623256005171
80};
81
82
83void writeToFile(char* buf, int len, const char* filename)
84{
85        if (buf != NULL) {
86                FILE* fp = fopen(filename, "w");
87                fwrite(buf, 1, len, fp);
88                fclose(fp);
89        }
90}
91
92int main()
93{
94        int i, j, err, nbytes;
95
96        const char* fname1 = "output.field(f1d1)";
97        const char* fname2 = "output.field(f1d2)";
98        const char* gname1 = "output.mesh(m1d)";
99        const char* gname2 = "output.mesh(m2d)";
100
101        printf("Testing field\n");
102
103        RpField* f1 = new RpField(fname1, 20);
104        f1->addAllValues(&(points[0]), 20);
105        f1->setMeshId("output.mesh(m2d)");
106
107        RpField* f2 = new RpField(fname2, 20);
108        f2->addAllPoints(&(points2[0]), 20);
109        f2->setMeshId("output.mesh(m1d)");
110
111        printf("Testing grid1d\n");
112
113        // use a regular 1d grid
114        RpGrid1d* grid = new RpGrid1d(0, 1, 20);
115        grid->objectName(gname1);
116
117        printf("Testing grid2d\n");
118
119        RpGrid2d* mesh = new RpGrid2d(0.0, 6.0, 1.0, 0.0, 3.0, 1.0);
120        mesh->objectName(gname2);
121
122        printf("Testing serializer\n");
123
124        RpSerializer myvis;
125        myvis.addObject(f1);
126        myvis.addObject(f2);
127        myvis.addObject(grid);
128        myvis.addObject(mesh);
129
130        char* buf = myvis.serialize();
131
132        // write the byte stream to a file for verification
133        nbytes = myvis.numBytes();
134        writeToFile(buf, nbytes, "out.f");
135       
136        printf("Testing serializer::deserializer\n");
137
138        RpSerializer newvis;
139        newvis.deserialize(buf);
140
141        newvis.print();
142
143        printf("Testing serializer::getObject\n");
144
145        RpGrid1d* gptr = (RpGrid1d*) newvis.getObject(gname1);
146        gptr->print();
147
148        RpGrid2d* g2ptr = (RpGrid2d*) newvis.getObject(gname2);
149        g2ptr->print();
150
151        RpField* fptr = (RpField*) newvis.getObject(fname1);
152        fptr->print();
153
154        RpField* f2ptr = (RpField*) newvis.getObject(fname2);
155        f2ptr->print();
156
157        newvis.print();
158
159        printf("after clear()\n");
160        newvis.clear();
161        newvis.print();
162
163        return 0;
164}
165
166
Note: See TracBrowser for help on using the repository browser.