📄 qfel.c
字号:
/*--------------------------------------------------------------------------Free Finite Element Package Copyright (c) 2002-2006 by Joerg FrochteAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare met:1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOTLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FORA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER ORCONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.----------------------------------------------------------------------------*/#include "qfel.h"/** * \file * \brief Implementation of finite elements with quadratic base functions including FEM-Solvers like Multigrid Methods * \author Joerg Frochte */static inline MEML_FLOAT Q1 (MEML_FLOAT x, MEML_FLOAT y){ return (1 - x - y);}static inline double Q2 (MEML_FLOAT x, MEML_FLOAT y){ y = y; return (x);}static inline double Q3 (MEML_FLOAT x, MEML_FLOAT y){ x = x; return (y);}/*static inline MEML_FLOAT Q1(MEML_FLOAT x, MEML_FLOAT y){ return( (1-x-y)*(1-2*x-2*y) );}static inline double Q2(MEML_FLOAT x,MEML_FLOAT y){ return( x*(2*x-1) );}static inline double Q3(MEML_FLOAT x,MEML_FLOAT y){ return( y*(2*y-1) );}*/static inline MEML_FLOAT Q4 (MEML_FLOAT x, MEML_FLOAT y){ return ((1 - x - y) * x * 4);}static inline double Q5 (MEML_FLOAT x, MEML_FLOAT y){ return (4 * x * y);}static inline double Q6 (MEML_FLOAT x, MEML_FLOAT y){ return ((1 - x - y) * y * 4);}static void qfel_22mul (MEML_FLOAT A[2][2], MEML_FLOAT v[2], MEML_FLOAT u[2]){ u[0] = A[0][0] * v[0] + A[0][1] * v[1]; u[1] = A[1][0] * v[0] + A[1][1] * v[1];}static inline void qfel_invtrans_22_Matrix (MEML_FLOAT A[2][2], MEML_FLOAT I[2][2]){ I[0][0] = A[1][1]; I[1][1] = A[0][0]; I[0][1] = -A[1][0]; I[1][0] = -A[0][1];}/** * \~german * \brief Assembliert die Massen-Matrix/den Reaktionsanteil * fuer das Galerkinverfahren * * Die Matrix A muss vor dem Aufruf dieser Funktion erzeugt worden sein. * Bei groesseren Gitter sollte A eine Sparse-Matrix-Typ sein. * * \param r Reaktionsparameter der partiellen Differentialgleichung * \param A die Matrix zu der die Steifigkeitsmatrix addiert werden soll */int qfel_assemb_reaction (const MEML_FLOAT r, ME_MATRIX * A, MESH * mesh){ INDEXARRAY *triangles = mesh->triangles; VECTOR *points = mesh->points; TRANSFORMATION *transformation = mesh->transformation; /* reine quadratische * 1/360 ist noch noetig */ /* static const MEML_FLOAT S4[6][6]= { { 6 ,-1 ,-1 , 0 ,-4 , 0}, {-1 , 6 ,-1 , 0 , 0 ,-4}, {-1 ,-1 , 6 , -4 , 0 , 0}, { 0 , 0 ,-4 , 32 ,16 ,16}, {-4 , 0 , 0 , 16 ,32 ,16}, { 0 ,-4 , 0 , 16 ,16 ,32} }; */ /* hierachische Basis */ static const MEML_FLOAT S4[6][6] = { {2 / 24.0, 1 / 24.0, 1 / 24.0, 1.0 / 15, 1.0 / 30, 1.0 / 15}, {1 / 24.0, 2 / 24.0, 1 / 24.0, 1.0 / 15, 1.0 / 15, 1.0 / 30}, {1 / 24.0, 1 / 24.0, 2 / 24.0, 1.0 / 30, 1.0 / 15, 1.0 / 15}, {1.0 / 15, 1.0 / 15, 1.0 / 30, 4.0 / 45, 2.0 / 45, 2.0 / 45}, {1.0 / 30, 1.0 / 15, 1.0 / 15, 2.0 / 45, 4.0 / 45, 2.0 / 45}, {1.0 / 15, 1.0 / 30, 1.0 / 15, 2.0 / 45, 2.0 / 45, 4.0 / 45} }; MEML_INT i, row, col; MEML_INT x, y; MEML_FLOAT det, gamma, temp; TRIANGLE the_triangle; MEML_INT number_of_triangles = (triangles->dim) / 3; MEML_INT *triangle = triangles->data; M_TYPE type; MEML_INT knoten[6]; meml_matrix_property_get (A, &col, &row, &type); if ((row < points->dim / 2) || (col < points->dim / 2)) { fprintf (stderr, "The size of the matrix is not big enough " "so store the data!\n"); return (EXIT_FAILURE); } for (i = 0; i < number_of_triangles; i++) { the_triangle = smfl_get_triangle (i, triangle); det = fabs (transformation[i].det); gamma = det * r; /* Warnung. MatLab nutzt 1,1 als obersten Eintrag C hingegen 0,0. Das ist ein gut moeglichkeit speicherzugriffsfehler zu produzieren */ for (x = 0; x < 3; x++) knoten[x] = the_triangle.p[x] - 1; for (x = 0; x < 3; x++) knoten[x + 3] = mesh->triangles_adof[i][x] + mesh->number_of_points; for (x = 0; x < 6; x++) for (y = 0; y < 6; y++) { temp = gamma * S4[x][y]; meml_matrix_element_add (A, knoten[x], knoten[y], temp); } } return (EXIT_SUCCESS);}int qfel_assemb_diffusion (const MEML_FLOAT eps, ME_MATRIX * A, MESH * mesh){ INDEXARRAY *triangles = mesh->triangles; VECTOR *points = mesh->points; TRANSFORMATION *transformation = mesh->transformation; /* reine quadratische * 1/6 ist noch noetig */ /* static const MEML_FLOAT S1[6][6]= { { 3 , 1 , 0 , -4 , 0 , 0}, { 1 , 3 , 0 , -4 , 0 , 0}, { 0 , 0 , 0 , 0 , 0 , 0}, {-4 ,-4 , 0 , 8 , 0 , 0}, { 0 , 0 , 0 , 0 , 8 ,-8}, { 0 , 0 , 0 , 0 ,-8 , 8} }; static const MEML_FLOAT S2[6][6] = { { 6 , 1 , 1 , -4 , 0 ,-4}, { 1 , 0 ,-1 , -4 , 4 , 0}, { 1 ,-1 , 0 , 0 , 4 ,-4}, {-4 ,-4 , 0 , 8 ,-8 , 8}, { 0 , 4 , 4 , -8 , 8 ,-8}, {-4 , 0 ,-4 , 8 ,-8 , 8} }; static const MEML_FLOAT S3[6][6] = { { 3 , 0 , 1 , 0 , 0 ,-4}, { 0 , 0 , 0 , 0 , 0 , 0}, { 1 , 0 , 3 , 0 , 0 ,-4}, { 0 , 0 , 0 , 8 ,-8 , 8}, { 0 , 0 , 0 , -8 , 8 , 0}, {-4 , 0 ,-4 , 0 , 0 , 8} }; */ /* hirachsiche Basis */ static const MEML_FLOAT S1[6][6] = { {0.5, -0.5, 0, 0, -2 / 3.0, 2 / 3.0}, {-0.5, 0.5, 0, 0, 2 / 3.0, -2 / 3.0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 4 / 3.0, 0, 0}, {-2 / 3.0, 2 / 3.0, 0, 0, 4 / 3.0, -4 / 3.0}, {2 / 3.0, -2 / 3.0, 0, 0, -4 / 3.0, 4 / 3.0} }; static const MEML_FLOAT S2[6][6] = { {1, -0.5, -0.5, 2 / 3.0, -4 / 3.0, 2 / 3.0}, {-0.5, 0, 0.5, -2 / 3.0, 2 / 3.0, 0}, {-0.5, 0.5, 0, 0, 2 / 3.0, -2 / 3.0}, {2 / 3.0, -2 / 3.0, 0, 4 / 3.0, -4 / 3.0, 4 / 3.0}, {-4 / 3.0, 2 / 3.0, 2 / 3.0, -4 / 3.0, 4 / 3.0, -4 / 3.0}, {2 / 3.0, 0, -2 / 3.0, 4 / 3.0, -4 / 3.0, 4 / 3.0} }; static const MEML_FLOAT S3[6][6] = { {0.5, 0, -0.5, 2 / 3.0, -2 / 3.0, 0}, {0, 0, 0, 0, 0, 0}, {-0.5, 0, 0.5, -2 / 3.0, 2 / 3.0, 0}, {2 / 3.0, 0, -2 / 3.0, 4 / 3.0, -4 / 3.0, 0}, {-2 / 3.0, 0, 2 / 3.0, -4 / 3.0, 4 / 3.0, 0}, {0, 0, 0, 0, 0, 4 / 3.0} }; MEML_INT i, row, col; int x, y; MEML_FLOAT det, gamma1, gamma2, gamma3, temp; TRIANGLE the_triangle; MEML_INT number_of_triangles = (triangles->dim) / 3; MEML_INT *triangle = triangles->data; M_TYPE type; MEML_INT knoten[6]; meml_matrix_property_get (A, &col, &row, &type); if ((row < (points->dim + mesh->additional_degrees_of_freedom->dim) / 2) || (col < (points->dim + mesh->additional_degrees_of_freedom->dim) / 2)) { fprintf (stderr, "qfel_assemb_diffusion : " "The size of the matrix is not big enough " "so store the data!\n"); return (EXIT_FAILURE); } for (i = 0; i < number_of_triangles; i++) { the_triangle = smfl_get_triangle (i, triangle); det = fabs (transformation[i].det); gamma1 = (transformation[i].B[0][1] * transformation[i].B[0][1] + transformation[i].B[1][1] * transformation[i].B[1][1]) / det; gamma2 = -(transformation[i].B[0][0] * transformation[i].B[0][1] + transformation[i].B[1][0] * transformation[i].B[1][1]) / det; gamma3 = (transformation[i].B[1][0] * transformation[i].B[1][0] + transformation[i].B[0][0] * transformation[i].B[0][0]) / det; /* Warnung. MatLab nutzt 1,1 als obersten Eintrag C hingegen 0,0. Das ist ein gut moeglichkeit speicherzugriffsfehler zu produzieren */ for (x = 0; x < 3; x++) knoten[x] = the_triangle.p[x] - 1; for (x = 0; x < 3; x++) { knoten[x + 3] = mesh->triangles_adof[i][x] + mesh->number_of_points; } for (x = 0; x < 6; x++) for (y = 0; y < 6; y++) { temp = eps * (gamma1 * S1[x][y] + gamma2 * S2[x][y] + gamma3 * S3[x][y]); meml_matrix_element_add_f (A, knoten[x], knoten[y], temp); } } return (EXIT_SUCCESS);}/** * \~german * \brief Assembliert die rechte Seite des LGS fuer das Galerkinverfahren */VECTOR *qfel_assemb_load_vector (xyt_function f, const double time, MESH * mesh){ INDEXARRAY *triangles = mesh->triangles; VECTOR *points = mesh->points; TRANSFORMATION *transformation = mesh->transformation; const MEML_FLOAT gaus_x[7] = { 0, 1, 0, 0.5, 0.5, 0, 1.0 / 3.0 }; const MEML_FLOAT gaus_y[7] = { 0, 0, 1, 0, 0.5, 0.5, 1.0 / 3.0 }; const MEML_FLOAT gaus_w[7] = { 1.0 / 40.0, 1.0 / 40.0, 1.0 / 40.0, 1.0 / 15.0, 1.0 / 15.0, 1.0 / 15.0, 27.0 / 120.0 }; const MEML_FLOAT phi[6][7] = { {Q1 (gaus_x[0], gaus_y[0]), Q1 (gaus_x[1], gaus_y[1]), Q1 (gaus_x[2], gaus_y[2]), Q1 (gaus_x[3], gaus_y[3]), Q1 (gaus_x[4], gaus_y[4]), Q1 (gaus_x[5], gaus_y[5]), Q1 (gaus_x[6], gaus_y[6])}, {Q2 (gaus_x[0], gaus_y[0]), Q2 (gaus_x[1], gaus_y[1]), Q2 (gaus_x[2], gaus_y[2]), Q2 (gaus_x[3], gaus_y[3]), Q2 (gaus_x[4], gaus_y[4]), Q2 (gaus_x[5], gaus_y[5]), Q2 (gaus_x[6], gaus_y[6])}, {Q3 (gaus_x[0], gaus_y[0]), Q3 (gaus_x[1], gaus_y[1]), Q3 (gaus_x[2], gaus_y[2]), Q3 (gaus_x[3], gaus_y[3]), Q3 (gaus_x[4], gaus_y[4]), Q3 (gaus_x[5], gaus_y[5]), Q3 (gaus_x[6], gaus_y[6])}, {Q4 (gaus_x[0], gaus_y[0]), Q4 (gaus_x[1], gaus_y[1]), Q4 (gaus_x[2], gaus_y[2]), Q4 (gaus_x[3], gaus_y[3]), Q4 (gaus_x[4], gaus_y[4]), Q4 (gaus_x[5], gaus_y[5]), Q4 (gaus_x[6], gaus_y[6])}, {Q5 (gaus_x[0], gaus_y[0]), Q5 (gaus_x[1], gaus_y[1]), Q5 (gaus_x[2], gaus_y[2]), Q5 (gaus_x[3], gaus_y[3]), Q5 (gaus_x[4], gaus_y[4]), Q5 (gaus_x[5], gaus_y[5]), Q5 (gaus_x[6], gaus_y[6])}, {Q6 (gaus_x[0], gaus_y[0]), Q6 (gaus_x[1], gaus_y[1]), Q6 (gaus_x[2], gaus_y[2]), Q6 (gaus_x[3], gaus_y[3]), Q6 (gaus_x[4], gaus_y[4]), Q6 (gaus_x[5], gaus_y[5]), Q6 (gaus_x[6], gaus_y[6])} }; MEML_INT i, x, y; MEML_FLOAT temp, det; TRIANGLE the_triangle; MEML_FLOAT I_temp[6]; MEML_INT number_of_points = points->dim / 2; MEML_INT number_of_triangles = (triangles->dim) / 3; MEML_INT *triangle = triangles->data; MEML_FLOAT *b; VECTOR *temp_vector; MEML_FLOAT f_value[7]; MEML_FLOAT d[2]; MEML_INT knoten[6]; temp_vector = meml_vector_new (mesh->number_of_adof + mesh->number_of_points); b = temp_vector->data; for (i = 0; i < number_of_points; i++) b[i] = 0; for (i = 0; i < number_of_triangles; i++) { the_triangle = smfl_get_triangle (i, triangle); for (x = 0; x < 3; x++) knoten[x] = the_triangle.p[x] - 1; for (x = 0; x < 3; x++) knoten[x + 3] = mesh->triangles_adof[i][x] + mesh->number_of_points; for (x = 0; x < 7; x++) { d[0] = points->data[(the_triangle.p[0] - 1) * 2]; d[1] = points->data[(the_triangle.p[0] - 1) * 2 + 1]; f_value[x] = (*f) (transformation[i].B[0][0] * gaus_x[x] + transformation[i].B[0][1] * gaus_y[x] + d[0], transformation[i].B[1][0] * gaus_x[x] + transformation[i].B[1][1] * gaus_y[x] + d[1], time); } for (x = 0; x < 6; x++) I_temp[x] = 0; for (x = 0; x < 7; x++) { temp = gaus_w[x] * f_value[x]; for (y = 0; y < 6; y++) I_temp[y] += temp * phi[y][x]; } det = fabs (transformation[i].det); for (x = 0; x < 6; x++) { b[knoten[x]] += I_temp[x] * det; } } return (temp_vector);}VECTOR *qfel_assemb_right_side (const VECTOR * f, MESH * mesh){ INDEXARRAY *triangles = mesh->triangles; TRANSFORMATION *transformation = mesh->transformation; /* hierachische Basis */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -