gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
GModelIO_ACTRAN.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include <stdlib.h>
7 #include <string.h>
8 #include "GModel.h"
9 #include "OS.h"
10 #include "MLine.h"
11 #include "MTriangle.h"
12 #include "MQuadrangle.h"
13 #include "MTetrahedron.h"
14 #include "MHexahedron.h"
15 #include "Context.h"
16 
17 static bool getMeshVertices(GModel *m, int num, int *n,
18  std::vector<MVertex *> &vec)
19 {
20  for(int i = 0; i < num; i++) {
21  MVertex *v = m->getMeshVertexByTag(n[i]);
22  if(!v) {
23  Msg::Error("Wrong node tag %d", n[i]);
24  return false;
25  }
26  else
27  vec.push_back(v);
28  }
29  return true;
30 }
31 
32 int GModel::readACTRAN(const std::string &name)
33 {
34  FILE *fp = Fopen(name.c_str(), "r");
35  if(!fp) {
36  Msg::Error("Unable to open file '%s'", name.c_str());
37  return 0;
38  }
39 
40  char buffer[256];
41  if(!fgets(buffer, sizeof(buffer), fp)) {
42  fclose(fp);
43  return 0;
44  }
45 
46  if(strncmp(buffer, "BEGIN ACTRAN", 12)) {
47  Msg::Error("Did not find ACTRAN header");
48  fclose(fp);
49  return 0;
50  }
51 
52  _vertexMapCache.clear();
53  std::map<int, std::vector<MElement *> > elements[3];
54  int nbv = 0, nbe = 0, dim = 0;
55 
56  while(!feof(fp)) {
57  if(!fgets(buffer, 256, fp)) break;
58  char str[256], str2[256];
59  sscanf(buffer, "%s %s", str, str2);
60  if(!strcmp(str, "BEGIN") && !strcmp(str2, "MESH")) {
61  if(!fgets(buffer, sizeof(buffer), fp)) break;
62  sscanf(buffer, "%d %d %d", &nbv, &nbe, &dim);
63  if(dim == 3 || dim == 2)
64  Msg::Info("ACTRAN mesh dimension %d", dim);
65  else {
66  Msg::Error("Cannot read ACTRAN mesh of dimension %d", dim);
67  break;
68  }
69  }
70  else if(!strcmp(str, "BEGIN") && !strcmp(str2, "NODE")) {
71  Msg::Info("%d nodes", nbv);
72  for(int i = 0; i < nbv; i++) {
73  if(!fgets(buffer, sizeof(buffer), fp)) break;
74  int num;
75  double x, y, z = 0.;
76  if(dim == 3)
77  sscanf(buffer, "%d %lf %lf %lf", &num, &x, &y, &z);
78  else
79  sscanf(buffer, "%d %lf %lf", &num, &x, &y);
80  _vertexMapCache[num] = new MVertex(x, y, z, nullptr, num);
81  }
82  }
83  else if(!strcmp(str, "BEGIN") && !strcmp(str2, "ELEMENT")) {
84  Msg::Info("%d elements", nbe);
85  for(int i = 0; i < nbe; i++) {
86  if(!fgets(buffer, sizeof(buffer), fp)) break;
87  int num, type, reg, n[8];
88  sscanf(buffer, "%d %d %d", &num, &type, &reg);
89  std::vector<MVertex *> vertices;
90  if(type == 2) {
91  sscanf(buffer, "%d %d %d %d %d", &num, &type, &reg, &n[0], &n[1]);
92  if(!getMeshVertices(this, 2, n, vertices)) break;
93  elements[0][reg].push_back(new MLine(vertices, num));
94  }
95  else if(type == 4) {
96  sscanf(buffer, "%d %d %d %d %d %d", &num, &type, &reg, &n[0], &n[1],
97  &n[2]);
98  if(!getMeshVertices(this, 3, n, vertices)) break;
99  elements[0][reg].push_back(new MTriangle(vertices, num));
100  }
101  else if(type == 8) {
102  sscanf(buffer, "%d %d %d %d %d %d %d", &num, &type, &reg, &n[0],
103  &n[1], &n[2], &n[3]);
104  if(!getMeshVertices(this, 4, n, vertices)) break;
105  elements[1][reg].push_back(new MTetrahedron(vertices, num));
106  }
107  else {
108  Msg::Error("Unknown type %d for element %d", type, num);
109  }
110  }
111  }
112  }
113 
114  for(int i = 0; i < (int)(sizeof(elements) / sizeof(elements[0])); i++)
115  _storeElementsInEntities(elements[i]);
118 
119  fclose(fp);
120  return 1;
121 }
MTriangle.h
MTetrahedron
Definition: MTetrahedron.h:34
Msg::Info
static void Info(const char *fmt,...)
Definition: GmshMessage.cpp:587
OS.h
MVertex
Definition: MVertex.h:24
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
GModel::_storeElementsInEntities
void _storeElementsInEntities(std::map< int, std::vector< MElement * > > &map)
Definition: GModel.cpp:2273
GModel::getMeshVertexByTag
MVertex * getMeshVertexByTag(int n)
Definition: GModel.cpp:1953
MLine.h
Fopen
FILE * Fopen(const char *f, const char *mode)
Definition: OS.cpp:273
MLine
Definition: MLine.h:21
MHexahedron.h
GModel
Definition: GModel.h:44
GModel::vertices
std::set< GVertex *, GEntityPtrLessThan > vertices
Definition: GModel.h:146
GModel::_vertexMapCache
std::map< int, MVertex * > _vertexMapCache
Definition: GModel.h:102
GModel::readACTRAN
int readACTRAN(const std::string &name)
Definition: GModelIO_ACTRAN.cpp:32
MTriangle
Definition: MTriangle.h:26
Context.h
MTetrahedron.h
z
const double z
Definition: GaussQuadratureQuad.cpp:56
MQuadrangle.h
getMeshVertices
static bool getMeshVertices(GModel *m, int num, int *n, std::vector< MVertex * > &vec)
Definition: GModelIO_ACTRAN.cpp:17
GModel.h
GModel::_storeVerticesInEntities
void _storeVerticesInEntities(std::map< int, MVertex * > &vertices)
Definition: GModel.cpp:2496
GModel::_associateEntityWithMeshVertices
void _associateEntityWithMeshVertices()
Definition: GModel.cpp:2470