-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (97 loc) · 3.75 KB
/
main.cpp
File metadata and controls
134 lines (97 loc) · 3.75 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <cgnslib.h>
#include <metis.h>
int main(int argc, char *argv[])
{
using namespace std;
if(argc != 3)
{
cerr << "Usage: partcgns filename nparts\n";
exit(-1);
}
const string filename = argv[1];
char name[256];
cout << "Reading file \"" << filename << "\"...\n";
//- Open the file. Assume only one base and one zone
int fileId, baseId = 1, zoneId = 1;
cg_open(filename.c_str(), CG_MODE_READ, &fileId);
cgsize_t sizes[2], one = 1;
//- Read the zone info
cg_zone_read(fileId, baseId, zoneId, name, sizes);
cout << "Zone \"" << name << "\" with " << sizes[0] << " nodes and " << sizes[1] << " elements.\n";
//- Read in the node coordinates
vector<double> xCoords(sizes[0]), yCoords(sizes[0]);
cg_coord_read(fileId, baseId, zoneId, "CoordinateX", RealDouble, &one, &sizes[0], xCoords.data());
cg_coord_read(fileId, baseId, zoneId, "CoordinateY", RealDouble, &one, &sizes[0], yCoords.data());
//- Section data
int nSections, secNo = 1;
cg_nsections(fileId, baseId, zoneId, &nSections);
ElementType_t type;
int nBoundary, parentFlag;
cgsize_t eBeg, eEnd;
cg_section_read(fileId, baseId, zoneId, secNo, name, &type, &eBeg, &eEnd, &nBoundary, &parentFlag);
if(type != TRI_3)
{
cerr << "This example only works for triangular meshes.\n";
exit(-1);
}
vector<cgsize_t> elems(3*sizes[1]);
cg_elements_read(fileId, baseId, zoneId, secNo, elems.data(), NULL);
cg_close(fileId);
//- Finished reading in mesh data. Now, partition the mesh
// int METIS PartMeshDual(idx t *ne, idx t *nn, idx t *eptr, idx t *eind, idx t *vwgt, idx t *vsize,
// idx t *ncommon, idx t *nparts, real t *tpwgts, idx t *options, idx t *objval,
// idx t *epart, idx t *npart)
idx_t ne = sizes[1];
idx_t nn = sizes[0];
idx_t nparts = atoi(argv[2]);
idx_t ncommon = 2;
idx_t objval;
vector<idx_t> eind(3*ne);
vector<idx_t> eptr(ne + 1);
vector<idx_t> epart(ne);
vector<idx_t> npart(nn);
cout << "Partitioning mesh into " << nparts << " partitions...\n";
eptr[0] = 0;
for(int i = 0; i < ne; ++i)
{
eind[3*i] = elems[3*i] - 1;
eind[3*i + 1] = elems[3*i + 1] - 1;
eind[3*i + 2] = elems[3*i + 2] - 1;
eptr[i + 1] = 3*(i + 1);
}
int status = METIS_PartMeshDual(&ne, &nn, eptr.data(), eind.data(), NULL, NULL, &ncommon, &nparts, NULL, NULL, &objval, epart.data(), npart.data());
//- Now, write the partitioned mesh to a cgns file!
cg_open(("partitioned_" + filename).c_str(), CG_MODE_WRITE, &fileId);
cg_base_write(fileId, "mesh", 2, 2, &baseId);
cg_zone_write(fileId, baseId, "Zone 1", sizes, Unstructured, &zoneId);
int xid;
cg_coord_write(fileId, baseId, zoneId, RealDouble, "CoordinateX", xCoords.data(), &xid);
cg_coord_write(fileId, baseId, zoneId, RealDouble, "CoordinateY", yCoords.data(), &xid);
cgsize_t start = 1, end;
for(int partNo = 0; partNo < nparts; ++partNo)
{
vector<cgsize_t> localElems;
for(int i = 0; i < ne; ++i)
{
if(epart[i] == partNo)
{
localElems.push_back(elems[3*i]);
localElems.push_back(elems[3*i + 1]);
localElems.push_back(elems[3*i + 2]);
}
}
end = start + localElems.size()/3 - 1;
int secId;
char buff[32];
sprintf(buff, "%d", partNo);
string secName = string("Proc") + buff;
cg_section_write(fileId, baseId, zoneId, secName.c_str(), TRI_3, start, end, 0, localElems.data(), &secId);
start = end + 1;
}
cg_close(fileId);
}