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