Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/meshgen2d/src/main.cpp
3196 views
1
/*
2
*/
3
4
// Stdlib
5
#include "../config.h"
6
#include <stdlib.h>
7
#ifdef WIN32
8
#include <direct.h>
9
#else
10
#include <unistd.h>
11
#endif
12
#include <stdio.h>
13
14
#include <string.h>
15
16
#include <iostream>
17
#include <fstream>
18
19
/*
20
* BoundaryLayerMesh specific includes
21
*/
22
#include "MeshParser.h"
23
#include "Mesh.h"
24
#include "MGError.h"
25
26
/*
27
* Simple error&exit routine.
28
*/
29
30
template<class T>
31
void blm_error(const char* msg, const T &opt);
32
33
int main(int argc, char **argv)
34
{
35
const char *usage = "Usage: Mesh2D [--bgmesh=filename] [--bgcontrol=filename] <input file> [mesh directory]";
36
37
// Read command line parameters
38
char *modelfile = NULL;
39
char *meshdir = NULL;
40
std::map<int, BGMeshToken*> externalBGMeshes;
41
42
for (int i = 1; i < argc; i++)
43
{
44
if (strncmp(argv[i], "--", 2) != 0)
45
{
46
if (modelfile == NULL) // First parameter without "--" is the input file
47
modelfile = argv[i];
48
else if (meshdir == NULL) // Second parameter without "--" is the output directory
49
meshdir = argv[i];
50
else
51
{
52
std::cerr << usage << std::endl;
53
return 1;
54
}
55
}
56
else if (strncmp(argv[i], "--bgcontrol=", 12) == 0)
57
{
58
std::ifstream file(argv[i] + 12);
59
while (!file.fail() && !file.eof())
60
{
61
int id;
62
std::string filename;
63
file >> id;
64
file >> filename;
65
66
if (!file.fail())
67
{
68
MeshParser bgmeshparser(filename.c_str());
69
BGMeshToken *token = new BGMeshToken;
70
token->type = "Explicit";
71
if (!bgmeshparser.readExplicitBGMesh(token))
72
blm_error("Could not read background mesh from file:", filename);
73
externalBGMeshes[id] = token;
74
}
75
}
76
}
77
else if (strncmp(argv[i], "--bgmesh=", 9) == 0)
78
{
79
BGMeshToken *token = new BGMeshToken;
80
token->type = "Explicit";
81
82
MeshParser bgmeshparser(argv[i] + 9);
83
if (!bgmeshparser.readExplicitBGMesh(token))
84
blm_error("Could not read background mesh from file:", argv[i] + 9);
85
86
externalBGMeshes[-1] = token;
87
}
88
else
89
{
90
std::cerr << usage << std::endl;
91
return 1;
92
}
93
}
94
95
if (modelfile == NULL)
96
{
97
std::cerr << usage << std::endl;
98
return 1;
99
}
100
101
std::cout << "Mesh input file: " << modelfile << std::endl;
102
std::cout << "Mesh output directory: ";
103
if (meshdir != NULL)
104
std::cout << meshdir;
105
else
106
{
107
char buf[1024];
108
if (getcwd(buf, 1024) != NULL)
109
std::cout << buf;
110
}
111
std::cout << std::endl;
112
113
Mesh mesh;
114
MeshParser parser( modelfile, &externalBGMeshes );
115
parser.readInputFile();
116
117
if (meshdir && chdir(meshdir) != 0)
118
blm_error("Could not access directory:", meshdir);
119
120
mesh.convertEdgeFormat( parser );
121
mesh.discretize();
122
123
std::ofstream header("mesh.header", std::ios::out | std::ios::trunc);
124
if(header.fail())
125
blm_error("Could not open header file.");
126
127
mesh.outputHeader( header );
128
header.close();
129
130
std::ofstream nodes("mesh.nodes", std::ios::out | std::ios::trunc);
131
if (nodes.fail())
132
blm_error("Could not open nodes file.");
133
134
mesh.outputNodes( nodes );
135
nodes.close();
136
137
std::ofstream elements("mesh.elements", std::ios::out | std::ios::trunc);
138
if (elements.fail())
139
blm_error("Could not open elements file.");
140
141
mesh.outputElements( elements );
142
elements.close();
143
144
std::ofstream boundary("mesh.boundary", std::ios::out | std::ios::trunc);
145
if (boundary.fail())
146
blm_error("Could not open boundary file.");
147
148
mesh.outputBoundary( boundary );
149
boundary.close();
150
151
std::cout << "*** ALL DONE" << std::endl;
152
153
return 0;
154
}
155
156