mesh.c 9.37 KB
Newer Older
1
2
3
4
#include "mesh.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
5
#include "tools.h"
6
7
8
9

static int check_word_in_file(FILE *f, const char *w) {
  char word[256];
  if(fscanf(f,"%255s", word) != 1){
10
    printf("Invalid mesh file (\"%s\" expected).\n", w);
11
12
13
    return -1;
  }
  if(strcmp(word, w) != 0){
14
    printf("Invalid mesh file (\"%s\" expected).\n", w);
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
    return -1;
  }
  return 0;
}

static int read_quoted_string(FILE *f, char *w, size_t maxl) {
  char c;
  size_t pos = 0;
  c = fgetc(f);
  while (c != '\"' && !feof(f)) {
    c = fgetc(f);
  }
  do {
    c = fgetc(f);
    if (c != '\"')
      w[pos++] = c;
  }while (c != '\"' && !feof(f) && pos < maxl);
  if (pos == maxl)
    return printf("string too long in mesh file\n");
  if(feof(f))
    return printf("string not terminated in mesh file\n");
  w[pos] = '\0';
  return 0;
}

static void mesh_add_physical_node(Mesh *m, int dim, int id, int node)
{
  int i = 0;
  for (i = 0; i < m->n_phys; ++i) {
    if(m->phys_id[i] == id && m->phys_dimension[i] == dim) {
      if((m->phys_n_nodes[i]/2)*2 == m->phys_n_nodes[i])
        m->phys_nodes[i] = realloc(m->phys_nodes[i], 2*(m->phys_n_nodes[i]+1)*sizeof(int));
      m->phys_nodes[i][m->phys_n_nodes[i]] = node;
      m->phys_n_nodes[i] += 1;
      break;
    }
  }
  if (i == m->n_phys)
    printf("Unknown physical tag %i with dimension %i.\n", id, dim);
}

static int int_cmp(const void *p0, const void *p1)
{
  int i0 = *(int*)p0;
  int i1 = *(int*)p1;
  if (i0 == i1) return 0;
  if (i0 < i1) return -1;
  return 1;
}

Mesh *mesh_load_msh(const char *filename)
{
  Mesh *m = malloc(sizeof(Mesh));
  FILE *f = fopen(filename, "r");
  if (!f){
    printf("Cannot open file \"%s\".\n", filename);
    return NULL;
  }
  check_word_in_file(f,"$MeshFormat");
  float v0;
  int v1, v2;
  if(fscanf(f, "%g %d %d", &v0, &v1, &v2) != 3){
    printf("Cannot parse mesh file version\n");
    return NULL;
  }
  if ((int)10*v0 != 22){
    printf("Mesh file format is %g and not msh 2.2\n", v0);
    return NULL;
  }
  check_word_in_file(f,"$EndMeshFormat");
  check_word_in_file(f,"$PhysicalNames");
  if (fscanf(f, "%d", &m->n_phys) != 1){
    printf("Cannot read physical names\n");
    return NULL;
  }
  m->phys_name = malloc(sizeof(char*)*m->n_phys);
  m->phys_nodes = malloc(sizeof(int*)*m->n_phys);
  m->phys_dimension = malloc(sizeof(int)*m->n_phys);
  m->phys_n_nodes = malloc(sizeof(int)*m->n_phys);
  m->phys_id = malloc(sizeof(int)*m->n_phys);
  for (int i = 0; i < m->n_phys; ++i) {
    if (fscanf(f, "%i %i", &m->phys_dimension[i], &m->phys_id[i]) != 2){
      printf("Cannot read physical names\n");
      return NULL;
    }
    char pname[256];
    if(read_quoted_string(f, pname, 256) < 0) return NULL;
    m->phys_name[i] = strdup(pname);
    m->phys_nodes[i] = NULL;
    m->phys_n_nodes[i] = 0;
  }
  check_word_in_file(f,"$EndPhysicalNames");
  check_word_in_file(f,"$Nodes");
  if (fscanf(f, "%d", &m->n_nodes) != 1){
      printf("Cannot read physical nodes.\n");
      return NULL;
  }
  m->x = malloc(sizeof(double)*m->n_nodes*3);
  for (int i = 0; i < m->n_nodes; ++i) {
    int nid;
    if (fscanf(f, "%d %le %le %le", &nid, &m->x[i*3], &m->x[i*3+1], &m->x[i*3+2]) != 4){
      printf("Cannot read nodes.\n");
      return NULL;
    }
    if (nid != i+1){
      printf("Nodes are not sequentialy numbered.\n");
      return NULL;
    }
  }
  check_word_in_file(f,"$EndNodes");
  check_word_in_file(f,"$Elements");
  int n_el;
  if (fscanf(f, "%d", &n_el) != 1){
    printf("Cannot read elements.\n");
    return NULL;
  }
  m->triangles = malloc(sizeof(int)*3*n_el);
  m->n_triangles = 0;
  for (int i = 0; i < n_el; ++i) {
    int eid, etype, nflags, flag;
135
    int phys=0;
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    if (fscanf(f, "%d %d %d", &eid, &etype, &nflags) != 3){
      printf("Cannot read elements.\n");
      return NULL;
    }
    for (int j = 0; j < nflags; ++j){
      if (fscanf(f, "%d", &flag) != 1){
        printf("Cannot read elements.\n");
        return NULL;
      }
      if (j == 0)
        phys = flag;
    }
    int n0, n1, n2;
    switch (etype) {
      case(1):
        if (fscanf(f, "%d %d", &n0, &n1) != 2){
          printf("Cannot read elements.\n");
          return NULL;
        }
        mesh_add_physical_node(m, 1, phys, n0-1);
        mesh_add_physical_node(m, 1, phys, n1-1);
        break;
      case(15):
        if (fscanf(f, "%d", &n0) != 1){
          printf("Cannot read elements.\n");
          return NULL;
        }
        mesh_add_physical_node(m, 0, phys, n0-1);
        break;
      case(2):
        if (fscanf(f, "%d %d %d", &n0, &n1, &n2) != 3){
          printf("Cannot read elements.\n");
          return NULL;
        }
        m->triangles[m->n_triangles*3+0] = n0-1;
        m->triangles[m->n_triangles*3+1] = n1-1;
        m->triangles[m->n_triangles*3+2] = n2-1;
        m->n_triangles++;
        break;
      default:
        break;
    }
  }
  m->triangles = realloc(m->triangles, 3*sizeof(int)*m->n_triangles);
  check_word_in_file(f,"$EndElements");
  fclose(f);
  for (int i = 0; i < m->n_phys; ++i) {
    qsort(m->phys_nodes[i], m->phys_n_nodes[i], sizeof(int), int_cmp);
    int l = 0;
    for(int j = 0; j < m->phys_n_nodes[i]; ++j){
      if (j == 0 || m->phys_nodes[i][l-1] != m->phys_nodes[i][j]){
        m->phys_nodes[i][l++] = m->phys_nodes[i][j];
      }
    }
    m->phys_n_nodes[i] = l;
  }
  return m;
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
int mesh_write_msh(Mesh *mesh, FILE *f)
{
  fprintf(f,"$MeshFormat\n2.2 0 0\n$EndMeshFormat\n");
  fprintf(f,"$Nodes\n");
  fprintf(f, "%d\n", mesh->n_nodes);
  for (int i = 0; i < mesh->n_nodes; ++i) {
    fprintf(f, "%i %.16g %.16g %.16g\n", i+1, mesh->x[i*3], mesh->x[i*3+1], mesh->x[i*3+2]);
  }
  fprintf(f, "$EndNodes\n");
  fprintf(f, "$Elements\n");
  fprintf(f, "%i\n", mesh->n_triangles);
  for (int i= 0; i < mesh->n_triangles; ++i) {
    int *tri = mesh->triangles+i*3;
    fprintf(f, "%i 2 2 1 1 %i %i %i\n", i+1, tri[0]+1, tri[1]+1, tri[2]+1);
  }
  fprintf(f, "$EndElements\n");
  return 0;
}

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
void mesh_free(Mesh *m)
{
  free(m->triangles);
  free(m->x);
  for (int i = 0; i < m->n_phys; ++i){
    free(m->phys_name[i]);
    free(m->phys_nodes[i]);
  }
  free(m->phys_name);
  free(m->phys_nodes);
  free(m->phys_dimension);
  free(m->phys_id);
  free(m->phys_n_nodes);
  free(m);
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
230
int mesh_write_msh_scalar(const Mesh *mesh, FILE *f, const char *field_name, double t, int iter, const double *solution, int n_fields, int field_id)
231
232
233
234
235
236
237
238
239
240
241
242
{
  fprintf(f,"$NodeData\n");
  fprintf(f, "1\n\"%s\"\n",field_name);
  fprintf(f, "1\n%.16g\n",t);
  fprintf(f, "3\n%i\n%i\n%i\n", iter, 1, mesh->n_nodes);
  for (int i = 0; i < mesh->n_nodes; ++i) {
    fprintf(f, "%i %.16g\n", i+1, solution[i*n_fields+field_id]);
  }
  fprintf(f, "$EndNodeData\n");
  return 0;
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
243
int mesh_write_msh_vector(const Mesh *mesh, FILE *f, const char *field_name, double t, int iter, const double *solution, int n_fields, int field_id_0, int field_id_1)
244
245
246
247
248
249
250
251
252
{
  fprintf(f,"$NodeData\n");
  fprintf(f, "1\n\"%s\"\n",field_name);
  fprintf(f, "1\n%.16g\n",t);
  fprintf(f, "3\n%i\n%i\n%i\n", iter, 3, mesh->n_nodes);
  for (int i = 0; i < mesh->n_nodes; ++i) {
    fprintf(f, "%i %.16g %.16g 0.\n", i+1, solution[i*n_fields+field_id_0], solution[i*n_fields+field_id_1]);
  }
  fprintf(f, "$EndNodeData\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
  return 0;
}

int mesh_write_pos_scalar(const Mesh *mesh, const char *dir_name, const char *field_name, double t, int iter, const double *solution, int n_fields, int field_id)
{
  char file_name[1024];
  sprintf(file_name,"%s/%s_%05i.pos",dir_name, field_name, iter);
  FILE *f = fopen(file_name, "w");
  if (!f)
    return printf("Cannot open file \"%s\" for writing.\n", file_name);
  fprintf(f,"View\"%s\"{\nTIME {%g};\n", field_name, t);
  for (int iel = 0; iel < mesh->n_triangles; ++iel) {
    int *tri = mesh->triangles+iel*3;
    const double x[3][2] = {
      {mesh->x[tri[0]*3+0],mesh->x[tri[0]*3+1]},
      {mesh->x[tri[1]*3+0],mesh->x[tri[1]*3+1]},
      {mesh->x[tri[2]*3+0],mesh->x[tri[2]*3+1]}};
    double V[3] = {solution[tri[0]*n_fields+field_id], solution[tri[1]*n_fields+field_id], solution[tri[2]*n_fields+field_id]};
    fprintf(f, "ST(%.8g, %.8g, 0, %.8g, %.8g, 0, %.8g, %.8g, 0){%.8g, %.8g, %.8g};\n",
        x[0][0],x[0][1],x[1][0],x[1][1],x[2][0],x[2][1],
        V[0],V[1],V[2]);
  }
  fprintf(f, "};\n");
  fclose(f);
  return 0;
}

int mesh_write_pos_vector(const Mesh *mesh, const char *dir_name, const char *field_name, double t, int iter, const double *solution, int n_fields, int field_id_0, int field_id_1)
{
  char file_name[1024];
  sprintf(file_name,"%s/%s_%05i.pos",dir_name, field_name, iter);
  FILE *f = fopen(file_name, "w");
  if (!f)
    return printf("Cannot open file \"%s\" for writing.\n", file_name);
  fprintf(f,"View\"%s\"{\nTIME {%g};\n", field_name, t);
  for (int iel = 0; iel < mesh->n_triangles; ++iel) {
    int *tri = mesh->triangles+iel*3;
    const double x[3][2] = {
      {mesh->x[tri[0]*3+0],mesh->x[tri[0]*3+1]},
      {mesh->x[tri[1]*3+0],mesh->x[tri[1]*3+1]},
      {mesh->x[tri[2]*3+0],mesh->x[tri[2]*3+1]}};
    double V0[3] = {solution[tri[0]*n_fields+field_id_0], solution[tri[1]*n_fields+field_id_0], solution[tri[2]*n_fields+field_id_0]};
    double V1[3] = {solution[tri[0]*n_fields+field_id_1], solution[tri[1]*n_fields+field_id_1], solution[tri[2]*n_fields+field_id_1]};
    fprintf(f, "VT(%.8g, %.8g, 0, %.8g, %.8g, 0, %.8g, %.8g, 0){%.8g, %.8g, 0, %.8g, %.8g, 0, %.8g, %.8g, 0};\n",
        x[0][0],x[0][1],x[1][0],x[1][1],x[2][0],x[2][1],
        V0[0],V1[0], V0[1], V1[1], V0[2], V1[2]);
  }
  fprintf(f, "};\n");
301
302
303
  fclose(f);
  return 0;
}