test_sln_mesh.c (2501B)
1 /* Copyright (C) 2022, 2026 |Méso|Star> (contact@meso-star.com) 2 * Copyright (C) 2026 Université de Lorraine 3 * Copyright (C) 2022 Centre National de la Recherche Scientifique 4 * Copyright (C) 2022 Université Paul Sabatier 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 19 #include "sln.h" 20 #include <rsys/math.h> 21 #include <stdlib.h> 22 23 static double 24 eval 25 (const struct sln_mesh* mesh, 26 const double nu) 27 { 28 const struct sln_vertex* vtx0 = NULL; 29 const struct sln_vertex* vtx1 = NULL; 30 double u; 31 size_t i; 32 CHK(mesh && nu >= 0); 33 CHK(mesh->nvertices); 34 35 FOR_EACH(i, 0, mesh->nvertices) { 36 if(nu <= mesh->vertices[i].wavenumber) break; 37 } 38 if(i == 0) return mesh->vertices[0].ka; 39 if(i == mesh->nvertices) return mesh->vertices[mesh->nvertices-1].ka; 40 41 vtx1 = mesh->vertices+i; 42 vtx0 = mesh->vertices+i-1; 43 u = (nu - vtx0->wavenumber) / (vtx1->wavenumber - vtx0->wavenumber); 44 u = CLAMP(u, 0, 1); 45 46 return u*vtx1->ka + (1.0-u)*vtx0->ka; 47 } 48 49 int 50 main(int argc, char** argv) 51 { 52 const struct sln_vertex vertices[] = { 53 {20, 20}, {40, 25}, {60, 40}, {80, 120}, {120, 140}, {200, 180} 54 }; 55 struct sln_mesh mesh = SLN_MESH_NULL; 56 size_t i; 57 (void)argc, (void)argv; 58 59 mesh.vertices = vertices; 60 mesh.nvertices = sizeof(vertices)/sizeof(vertices[0]); 61 62 CHK(sln_mesh_eval(&mesh, 10) == 20); 63 CHK(sln_mesh_eval(&mesh, 20) == 20); 64 CHK(sln_mesh_eval(&mesh, 201) == 180); 65 CHK(sln_mesh_eval(&mesh, 200) == 180); 66 67 FOR_EACH(i, 0, 100) { 68 const double r = (double)rand() / (double)((size_t)RAND_MAX + 1); 69 const double nu = 70 mesh.vertices[0].wavenumber * (1.0 - r) 71 + mesh.vertices[mesh.nvertices-1].wavenumber * r; 72 const double val0 = eval(&mesh, nu); 73 const double val1 = sln_mesh_eval(&mesh, nu); 74 CHK(eq_eps(val0, val1, val1*1e-6)); 75 } 76 77 mesh.nvertices = 1; 78 CHK(sln_mesh_eval(&mesh, 0) == 20); 79 CHK(sln_mesh_eval(&mesh, 40) == 20); 80 return 0; 81 }