fluid_problem_fem.c 5.76 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
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
#include "fluid_problem_fem.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

static void fe_f_line_p1(const double *xi, double *f) {
  f[0] = 1-xi[0];
  f[1] = xi[0];
}
static void fe_df_line_p1(const double *xi, const double dxidx[][D], double df[][D]) {
  for (int d = 0; d < D; ++d) {
    df[0][d] = -dxidx[0][d];
    df[1][d] = dxidx[0][d];
  }
}

static void fe_f_triangle_p1(const double *xi, double *f) {
  f[0] = 1-xi[0]-xi[1];
  f[1] = xi[0];
  f[2] = xi[1];
}

void fe_df_triangle_p1(const double *xi, const double dxidx[][D], double df[][D]) {
  for (int d = 0; d < D; ++d) {
    df[0][d] = -dxidx[0][d]-dxidx[1][d];
    df[1][d] = dxidx[0][d];
    df[2][d] = dxidx[1][d];
  }
}


FEElement *fe_element_new(const char *etype) {
  FEElement *fe = malloc(sizeof(FEElement));
  if (strcmp(etype,"triangle_p1") == 0) {
    fe->nlocal = 3;
    fe->n[0] = 1;
    fe->n[1] = 0;
    fe->n[2] = 0;
    fe->n[3] = 0;
    fe->f = fe_f_triangle_p1;
    fe->df = fe_df_triangle_p1;
  }
  else if (strcmp(etype,"triangle_p1dg") == 0) {
    fe->nlocal = 3;
    fe->n[0] = 0;
    fe->n[1] = 0;
    fe->n[2] = 3;
    fe->n[3] = 0;
    fe->f = fe_f_triangle_p1;
    fe->df = fe_df_triangle_p1;
  }
  else if (strcmp(etype,"line_p1") == 0) {
    fe->nlocal = 2;
    fe->n[0] = 1;
    fe->n[1] = 0;
    fe->n[2] = 0;
    fe->n[3] = 0;
    fe->f = fe_f_line_p1;
    fe->df = fe_df_line_p1;
  }
  else {
    printf("Unknown element type '%s'.\n", etype);
    exit(0);
  }
  return fe;
}

void fe_element_free(FEElement *fe) {
  free(fe);
}


FEFields *fe_fields_new(const char **etypes) {
  FEFields *fields = malloc(sizeof(FEFields));
  fields->n = 0;
  fields->local_size = 0;
  for (const char **etype = etypes; *etype != NULL; etype++) {
    fields->n++;
  }
  fields->element = malloc(sizeof(FEElement*)*fields->n);
  int n = 0;
  for (int i = 0; i < fields->n; ++i) {
    fields->element[i] = fe_element_new(etypes[i]);
    fields->local_size += fields->element[i]->nlocal;
  }
  for (int d = 0; d < DIMENSION+1; ++d) {
    fields->nd[d] = 0;
    for (int i = 0; i < fields->n; ++i) {
      fields->nd[d] += fields->element[i]->n[d];
    }
  }
  return fields;
}

void fe_fields_free(FEFields *fields) {
  for (int i = 0; i < fields->n; ++i) {
    fe_element_free(fields->element[i]);
  }
  free(fields->element);
  free(fields);
}

void fe_fields_f(const FEFields *fields, const double *xi, double *f) {
  int c = 0;
  for (int i = 0; i < fields->n; ++i) {
    fields->element[i]->f(xi, &f[c]);
    c += fields->element[i]->nlocal;
  }
}

void fe_fields_df(const FEFields *fields, const double *xi, const double dxidx[D][D], double df[][D]) {
  int c = 0;
  for (int i = 0; i < fields->n; ++i) {
    fields->element[i]->df(xi, dxidx, &df[c]);
    c += fields->element[i]->nlocal;
  }
}

void fe_fields_local_map(const FEFields *fields, const Mesh *mesh, int iel, int map[]) {
  int c[fields->n];
  c[0] = 0;
  for (int i = 1; i < fields->n; ++i) {
    c[i] = c[i-1]+fields->element[i-1]->nlocal;
  }
  int l = 0;
  for (int i = 0; i < fields->n; ++i) {
    for (int j = 0; j < fields->element[i]->n[0]; ++j){
      for (int k = 0; k < DIMENSION+1; ++k) {
        map[c[i]++] = mesh->elements[iel*(DIMENSION+1)+k]*fields->nd[0]+l;
      }
      l++;
    }
  }
  int shift = l*mesh->n_nodes;
  // todo add edges (and faces in 3D)
  for (int i = 0; i < fields->n; ++i) {
    for (int j = 0; j < fields->element[i]->n[DIMENSION]; ++j){
      map[c[i]++] = shift+iel*fields->nd[DIMENSION]+l;
      l++;
    }
  }
  if (fields->n == 3) {
    int *el = mesh->elements + 3*iel;
    int map2[9] = {
      el[0]*3+0, el[1]*3+0, el[2]*3+0,
      el[0]*3+1, el[1]*3+1, el[2]*3+1,
      el[0]*3+2, el[1]*3+2, el[2]*3+2,
    };
  }
}

void fe_fields_eval_sf(const FEFields *fields, const Mesh *mesh, int iel, const double sf[], const double *v, double s[]) {
  int map[fields->local_size];
  fe_fields_local_map(fields, mesh, iel, map);
  int c = 0;
  for (int i = 0; i < fields->n; ++i) {
    s[i] = 0;
    for (int j = 0; j < fields->element[i]->nlocal; ++j) {
      s[i] += v[map[c]]*sf[c];
      c++;
    }
  }
}

void fe_fields_eval_grad_sf(const FEFields *fields, const Mesh *mesh, int iel, const double sf[], const double dsf[][DIMENSION], const double *v, double s[], double ds[][DIMENSION]) {
  int map[fields->local_size];
  fe_fields_local_map(fields, mesh, iel, map);
  int c = 0;
  for (int i = 0; i < fields->n; ++i) {
    s[i] = 0;
    for (int d = 0; d < DIMENSION; ++d) {
      ds[i][d] = 0;
    }
    for (int j = 0; j < fields->element[i]->nlocal; ++j) {
      s[i] += v[map[c]]*sf[c];
      for (int d = 0; d < DIMENSION; ++d){
        ds[i][d] += v[map[c]]*dsf[c][d];
      }
      c++;
    }
  }
}

void fe_fields_add_to_local_vector(const FEFields *fields, const double *f0, const double f1[][D], const double *sf, const double dsf[][D], double jw, double *local_vector) {
  int cc = 0;
  for (int ifield = 0; ifield < fields->n; ++ifield) {
    for (int iphi = 0; iphi < fields->element[ifield]->nlocal; ++iphi){
      double v = 0;
      if (f0) {
        v += sf[cc]*f0[ifield]*jw;
      }
      if (f1){
        for (int id = 0; id < D; ++id) {
          v += dsf[cc][id]*f1[ifield][id]*jw;
        }
      }
      local_vector[cc] += v;
      cc++;
    }
  }
}

void fe_fields_eval_grad(const FEFields *fields, const double *xi, const double dxidx[D][D], const Mesh *mesh, int iel, const double *v, double s[], double ds[][DIMENSION]) {
  double sf[fields->local_size], dsf[fields->local_size][D];
  fe_fields_f(fields, xi, sf);
  fe_fields_df(fields, xi, dxidx, dsf);
  fe_fields_eval_grad_sf(fields, mesh, iel, sf, dsf, v, s, ds);
}

void fe_fields_eval(const FEFields *fields, const double *xi, const Mesh *mesh, int iel, const double *v, double s[]) {
  double sf[fields->local_size];
  fe_fields_f(fields, xi, sf);
  fe_fields_eval_sf(fields, mesh, iel, sf, v, s);
}