Mmg
Simplicial remeshers (mesh adaptation, isovalue discretization, lagrangian movement)
main.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include <mmg/mmg2d/libmmg2d.h>
4#include <math.h>
5
6int main() {
7 MMG5_pMesh mesh2 = NULL;
8 MMG5_pSol met2 = NULL;
9 int ier = 0;
12
13 // 4 vertices, no tris, no quads, no edges so far
14 MMG2D_Set_meshSize(mesh2, 4, 0, 0, 0);
15
16 // a rectangle [0, 2 * M_PI] x [-M_PI / 2, M_PI / 2] for spherical coordinates in usual lon/lat way
17 double v[8] = {0, -M_PI/2, 2 * M_PI, -M_PI/2, 2 * M_PI, M_PI/2, 0, M_PI/2};
18 MMG2D_Set_vertices(mesh2, v, NULL);
19
20 MMG2D_Set_dparameter(mesh2, met2, MMG2D_DPARAM_hsiz, 0.01);
21
22 // generate a regular fine mesh of the square in meshing mode
23 ier = MMG2D_mmg2dmesh(mesh2, met2);
24 if (ier) {
25 fprintf(stderr, "error %i during meshing.\n", ier);
26 exit(1);
27 }
28
29 // save the "computational geometry" mesh and the metric set by MMG2D_mmg2dmesh
30 // For Medit users
31 MMG2D_saveMesh(mesh2, "cg.mesh");
32 MMG2D_saveSol(mesh2, met2, "cg.sol");
33 // For Gmsh users
34 MMG2D_saveMshMesh(mesh2, NULL, "cg.msh");
35
36 MMG2D_Free_solutions(mesh2,met2);
37
38 // remesh with anisotropic metric
39 int np, nt, nquad, na;
40 MMG2D_Get_meshSize(mesh2, &np, &nt, &nquad, &na);
41 double* verts = malloc(2 * np * sizeof(double));
42 MMG2D_Get_vertices(mesh2, verts, NULL, NULL, NULL);
43 MMG2D_Set_solSize(mesh2, met2, MMG5_Vertex, np, MMG5_Tensor);
44 for (int i = 0; i < np; i++) {
45
46 // latitude
47 double y = verts[2 * i + 1];
48 // metric on the sphere, see standard textbooks on elementary differential geometry
49 // Gaussian fundamental quantities
50 double E = cos(y) * cos(y);
51 double F = 0.0;
52 double G = 1.0;
53
54 // The symetric metric tensor (E F \\ F G) may be diagonalized in the basis of its
55 // eigenvectors. The eigenvalue \lambda_i associated to the eigenvector e_i prescribes a
56 // length l_i = 1/\sqrt(\lambda_i) in the direction e_i. This formula allows to compute
57 // a size factor to have sufficiently small edges on the surface of the sphere of radius 1.
58 //
59 // For a detailed explanation, see:
60 // https://forum.mmgtools.org/t/how-to-scale-the-metric-for-anisotropic-meshing-in-mmg2d-example-provided/441/2?u=algiane
61 double factor = 100.0;
62
63 // we add a small amount to the 1-1-entry to make the metric non-singular everywhere
64 const double eps = 1.e-1;
65 MMG2D_Set_tensorSol(met2, factor * E + eps, factor * F, factor * G, i+1);
66 }
67
68 // disable hsiz because it is incompatible with an anisotropic metric
70
71 // set gradation to a not too restrictive value
73
74 // save the "computational geometry" mesh and the setted metric
75 // For Medit users
76 MMG2D_saveMesh(mesh2, "cg-with-met.mesh");
77 MMG2D_saveSol(mesh2, met2, "cg-with-met.sol");
78 // For Gmsh users
79 MMG2D_saveMshMesh(mesh2, met2, "cg-with-met.msh");
80
81 // do it, remesh!
82 ier = MMG2D_mmg2dlib(mesh2, met2);
83
84 if (ier != 0) {
85 fprintf(stderr, "error %i during remeshing.\n", ier);
86 exit(1);
87 }
88
89 // Save at Gmsh file format
90 MMG2D_saveMshMesh(mesh2, NULL, "out2.msh");
91
92 // Save at Medit one
93 MMG2D_saveMesh(mesh2, "out2.mesh");
94 MMG2D_saveSol(mesh2,met2, "out2.sol");
95
96 // map to 3d and save as Medit file
97 MMG2D_Get_meshSize(mesh2, &np, &nt, &nquad, &na);
98 verts = realloc(verts, 2 * np * sizeof(double));
99 int* tris = malloc(3 * nt * sizeof(int));
100 MMG2D_Get_vertices(mesh2, verts, NULL, NULL, NULL);
101 MMG2D_Get_triangles(mesh2, tris, NULL, NULL);
102
103 printf("\n\n -- Save the final mesh 'sphere-end.mesh' at Medit file format.\n");
104
105 FILE *fp = fopen("sphere-end.mesh", "w+");
106
107 fprintf(fp, "MeshVersionFormatted 2\n\n Dimension 3\n\n Vertices %d\n\n", np);
108 for (int i = 0; i < np; i++) {
109 double x = verts[2 * i];
110 double y = verts[2 * i + 1];
111 /* Parametrization of a sphere of radius 1 from the longitude (x) and
112 * latitude (y) */
113 double newx = cos(x) * cos(y);
114 double newy = sin(x) * cos(y);
115 double newz = sin(y);
116 fprintf(fp, "%f %f %f 0\n", newx, newy, newz);
117 }
118 fprintf(fp, "Triangles %d\n", nt);
119
120 for (int i = 0; i < nt; i++)
121 fprintf(fp, "%i %i %i 0\n", tris[3 * i], tris[3 * i +1], tris[3 * i + 2]);
122
123 fprintf(fp, "End\n");
124 fclose(fp);
125
126 free(verts);
127 free(tris);
129
130 return 0;
131}
int MMG2D_Init_mesh(const int starter,...)
Initialize a mesh structure and optionally the associated solution and metric structures.
int MMG2D_Set_iparameter(MMG5_pMesh mesh, MMG5_pSol sol, int iparam, MMG5_int val)
Set integer parameter iparam to value val.
int MMG2D_Set_meshSize(MMG5_pMesh mesh, MMG5_int np, MMG5_int nt, MMG5_int nquad, MMG5_int na)
Set the numbers of entities in the mesh.
int MMG2D_Set_tensorSol(MMG5_pSol met, double m11, double m12, double m22, MMG5_int pos)
Set a single element of a tensor sol structure.
int MMG2D_Get_vertices(MMG5_pMesh mesh, double *vertices, MMG5_int *refs, int *areCorners, int *areRequired)
Get the coordinates and references of all vertices in the mesh.
int MMG2D_Set_solSize(MMG5_pMesh mesh, MMG5_pSol sol, int typEntity, MMG5_int np, int typSol)
Set the size and type of a solution field.
int MMG2D_Get_meshSize(MMG5_pMesh mesh, MMG5_int *np, MMG5_int *nt, MMG5_int *nquad, MMG5_int *na)
Get the number of vertices, triangles and edges of the mesh.
int MMG2D_Set_vertices(MMG5_pMesh mesh, double *vertices, MMG5_int *refs)
Set the coordinates and references of all vertices in the mesh.
int MMG2D_Set_dparameter(MMG5_pMesh mesh, MMG5_pSol sol, int dparam, double val)
Set double parameter dparam to value val.
int MMG2D_Free_all(const int starter,...)
Deallocations before return.
int MMG2D_Get_triangles(MMG5_pMesh mesh, MMG5_int *tria, MMG5_int *refs, int *areRequired)
Get the vertices and references of all triangles in the mesh.
int ier
program main
Example for using mmglib (basic use)
Definition: main.F90:6
int MMG2D_saveMesh(MMG5_pMesh mesh, const char *filename)
Save a mesh in .mesh/.meshb format.
Definition: inout_2d.c:1101
int MMG2D_saveMshMesh(MMG5_pMesh mesh, MMG5_pSol sol, const char *filename)
Save a mesh and optionally one data field in MSH format, ascii or binary depending on the filename ex...
Definition: inout_2d.c:1536
int MMG2D_saveSol(MMG5_pMesh mesh, MMG5_pSol sol, const char *filename)
Save metric field in medit solution file format.
Definition: inout_2d.c:1617
int MMG2D_mmg2dlib(MMG5_pMesh mesh, MMG5_pSol met)
Main "program" for the mesh adaptation library.
Definition: libmmg2d.c:63
int MMG2D_mmg2dmesh(MMG5_pMesh mesh, MMG5_pSol met)
Main "program" for the mesh generation library.
Definition: libmmg2d.c:305
@ MMG2D_DPARAM_hgrad
Definition: libmmg2d.h:139
@ MMG2D_IPARAM_verbose
Definition: libmmg2d.h:112
@ MMG2D_DPARAM_hsiz
Definition: libmmg2d.h:137
LIBMMG2D_EXPORT void MMG2D_Free_solutions(MMG5_pMesh mesh, MMG5_pSol sol)
Free the solution.
#define MMG5_ARG_ppMesh
Definition: libmmgtypes.h:102
#define MMG5_ARG_end
Definition: libmmgtypes.h:179
@ MMG5_Tensor
Definition: libmmgtypes.h:221
#define MMG5_ARG_start
Definition: libmmgtypes.h:93
@ MMG5_Vertex
Definition: libmmgtypes.h:230
#define MMG5_ARG_ppMet
Definition: libmmgtypes.h:122
MMG mesh structure.
Definition: libmmgtypes.h:613