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

static int check_word_in_file(FILE *f, const char *w) {
  char word[256];
  if(fscanf(f,"%255s", word) != 1){
9
    printf("Invalid mesh file (\"%s\" expected).\n", w);
10
11
12
    return -1;
  }
  if(strcmp(word, w) != 0){
13
    printf("Invalid mesh file (\"%s\" expected).\n", w);
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
    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;
134
    int phys=0;
135
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    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;
}

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);
}

int mesh_write_msh_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.msh",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,"$MeshFormat\n2.1 0 8\n$EndMeshFormat\n");
  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");
  fclose(f);
  return 0;
}

int mesh_write_msh_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.msh",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,"$MeshFormat\n2.1 0 8\n$EndMeshFormat\n");
  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");
  fclose(f);
  return 0;
}