Skip to content
Snippets Groups Projects
Commit 552e3e07 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

improve docs

parent a4c9bec7
No related branches found
No related tags found
No related merge requests found
// This reimplements gmsh/tutorial/t1.geo in C++. For all the elementary
// This file reimplements gmsh/tutorial/t1.geo in C++. For all the elementary
// explanations about the general philosphy of entities in Gmsh, see the
// comments in the .geo file. Comments here will focus on the specifics of the
// C++ API.
......@@ -9,28 +9,34 @@
int main(int argc, char **argv)
{
// Before using any functions in the C++ API, Gmsh must be initialized. If
// argc/argv are passed, Gmsh will parse the commandline, in the same way as
// argc/argv are passed, Gmsh will parse the commandline in the same way as
// the standalone Gmsh code.
gmshInitialize(argc, argv);
// By default Gmsh will not print out any messages: in order to output
// messages on the terminal, just set the standard Gmsh option (with the same
// format and meaning as in .geo files) using gmshOptionSetNumber():
// messages on the terminal, just set the standard Gmsh option
// "General.Terminal" (same format and meaning as in .geo files) using
// gmshOptionSetNumber():
gmshOptionSetNumber("General.Terminal", 1);
// This creates a new model, named "t1". If gmshModelCreate() is not called, a
// new default (unnamed) model will be created.
// new default (unnamed) model will be created on the fly, if necessary.
gmshModelCreate("t1");
// The C++ API provides direct access the internal CAD kernels. The built-in
// CAD kernel was used in t1.geo. To create geometrical points with the
// built-in CAD kernel, use gmshModelGeoAddPoint():
// - the first argument is the point id (or "tag") ; if positive, the point
// is created with this tag ; if negative, a new tag will be returned
// (in the 5th argument)
// The C++ API provides direct access to the internal CAD kernels. The
// built-in CAD kernel was used in t1.geo: the corresponding API functions
// have the "gmshModeGeo" prefix. To create geometrical points with the
// built-in CAD kernel, one thus uses gmshModelGeoAddPoint():
//
// - the first argument is the point tag ; if positive, the point is created
// with this tag ; if negative, a new (unused) tag will be assigned and
// returned (in the 5th argument)
//
// - the next 3 arguments are the point coordinates (x, y, z)
//
// - the fifth argument is the actual id of the point: equal to tag if tag >
// 0, or a new id if tag < 0
// 0, or a new one if tag < 0
//
// - the last (optional) argument is the target mesh size close to the point
double lc = 1e-2;
int o;
......@@ -40,21 +46,21 @@ int main(int argc, char **argv)
gmshModelGeoAddPoint(4, 0, .3, 0, o, lc);
// The API to create lines with the built-in kernel follows the same
// conventions: first argument is a tag (here positive to force the id),
// followed by 2 point ids, followed by the actual (returned) id.
// conventions: the first argument is a tag (here positive to force it),
// followed by 2 point tags, followed by the actual (returned) tag.
gmshModelGeoAddLine(1, 1, 2, o);
gmshModelGeoAddLine(2, 3, 2, o);
gmshModelGeoAddLine(3, 3, 4, o);
gmshModelGeoAddLine(4, 4, 1, o);
// The philosophy to construct line loops and surfaces is similar: the second
// arguments are now std::vectors of integers.
// arguments are now vectors of integers.
gmshModelGeoAddLineLoop(1, {4, 1, -2, 3}, o);
gmshModelGeoAddPlaneSurface(1, {1}, o);
// Physical groups ae defined by providing the dimension of the group (0 for
// Physical groups are defined by providing the dimension of the group (0 for
// physical points, 1 for physical lines, 2 for physical surfaces and 3 for
// phsyical volumes) and its tag, folllowed by a std::vector of entitiy ids.
// phsyical volumes) and its tag, followed by a vector of entity tags.
gmshModelAddPhysicalGroup(0, 1, {1, 2});
gmshModelAddPhysicalGroup(1, 2, {1, 2});
gmshModelAddPhysicalGroup(2, 6, {1});
......@@ -63,17 +69,22 @@ int main(int argc, char **argv)
// entity.
gmshModelSetPhysicalName(2, 6, "My surface");
// Before it can be meshed, the internal CAD representation (here in the
// built-in "Geo" CAD kernel) must be synchronized with the Gmsh model, which
// will create the relevant Gmsh data structure to represent the full topology
// of the model. This is achieved by the gmshModelGeoSynchronize() API call.
// Before it can be meshed, the internal CAD representation must be
// synchronized with the Gmsh model, which will create the relevant Gmsh data
// structures. This is achieved by the gmshModelGeoSynchronize() API call for
// the built-in CAD kernel. Synchronizations can be called at any time, but
// they involve a non trivial amount of processing; so while you could
// synchronize the internal CAD data after every CAD command, it is usually
// better to minimize the number of synchronization points.
gmshModelGeoSynchronize();
// We can then generate a 2D mesh, and save it to disk.
// We can then generate a 2D mesh...
gmshModelMesh(2);
// ... and save it to disk
gmshExport("t1.msh");
// Gmsh finalize should be called at the end.
// This should be called at the end:
gmshFinalize();
return 0;
}
// This reimplements gmsh/tutorial/t10.geo in C++.
#include <gmsh.h>
#include <sstream>
// this reimplements gmsh/tutorial/t10.geo
int main(int argc, char **argv)
{
gmshInitialize(argc, argv);
......@@ -26,30 +26,30 @@ int main(int argc, char **argv)
gmshModelGeoAddLineLoop(5, {1,2,3,4}, o);
gmshModelGeoAddPlaneSurface(6, {5}, o);
gmshModelFieldAdd(1, "Attractor");
gmshModelFieldCreate(1, "Attractor");
gmshModelFieldSetNumbers(1, "NodesList", {5});
gmshModelFieldSetNumber(1, "NNodesByEdge", 100);
gmshModelFieldSetNumbers(1, "EdgesList", {2});
gmshModelFieldAdd(2, "Threshold");
gmshModelFieldCreate(2, "Threshold");
gmshModelFieldSetNumber(2, "IField", 1);
gmshModelFieldSetNumber(2, "LcMin", lc / 30);
gmshModelFieldSetNumber(2, "LcMax", lc);
gmshModelFieldSetNumber(2, "DistMin", 0.15);
gmshModelFieldSetNumber(2, "DistMax", 0.5);
gmshModelFieldAdd(3, "MathEval");
gmshModelFieldCreate(3, "MathEval");
gmshModelFieldSetString(3, "F", "Cos(4*3.14*x) * Sin(4*3.14*y) / 10 + 0.101");
gmshModelFieldAdd(4, "Attractor");
gmshModelFieldCreate(4, "Attractor");
gmshModelFieldSetNumbers(4, "NodesList", {1});
gmshModelFieldAdd(5, "MathEval");
gmshModelFieldCreate(5, "MathEval");
std::stringstream stream;
stream << "F4^3 + " << lc / 100;
gmshModelFieldSetString(5, "F", stream.str());
gmshModelFieldAdd(6, "Box");
gmshModelFieldCreate(6, "Box");
gmshModelFieldSetNumber(6, "VIn", lc / 15);
gmshModelFieldSetNumber(6, "VOut", lc);
gmshModelFieldSetNumber(6, "XMin", 0.3);
......@@ -57,7 +57,7 @@ int main(int argc, char **argv)
gmshModelFieldSetNumber(6, "YMin", 0.3);
gmshModelFieldSetNumber(6, "YMax", 0.6);
gmshModelFieldAdd(7, "Min");
gmshModelFieldCreate(7, "Min");
gmshModelFieldSetNumbers(7, "FieldsList", {2, 3, 5, 6});
gmshModelFieldSetAsBackground(7);
......
#include <gmsh.h>
// This file reimplements gmsh/tutorial/t16.geo in C++.
// this reimplements gmsh/tutorial/t16.geo
#include <gmsh.h>
int main(int argc, char **argv)
{
......@@ -49,4 +49,3 @@ int main(int argc, char **argv)
gmshFinalize();
return 0;
}
// This reimplements gmsh/tutorial/t2.geo in C++. Comments focus on the new API
// functions used compared to t1.cpp.
// This file reimplements gmsh/tutorial/t2.geo in C++. Comments focus on the new
// API functions used, compared to the ones introduced in t1.cpp.
#include <gmsh.h>
......@@ -10,7 +10,7 @@ int main(int argc, char **argv)
gmshModelCreate("t2");
// Copy/paste from t1.cpp
// Copied from t1.cpp...
double lc = 1e-2;
int o;
gmshModelGeoAddPoint(1, 0, 0, 0, o, lc);
......@@ -29,20 +29,20 @@ int main(int argc, char **argv)
gmshModelAddPhysicalGroup(1, 2, {1, 2});
gmshModelAddPhysicalGroup(2, 6, {1});
gmshModelSetPhysicalName(2, 6, "My surface");
// End copy/paste
// ...end of copy
gmshModelGeoAddPoint(5, 0, .4, 0, o, lc);
gmshModelGeoAddLine(5, 4, 5, o);
// Geometrical transformations take a std::vector of std::pair<int, int> as
// first argument, which contains the list of entities, represented by
// (dimension,tag) pairs. Here we translate point 3 (dimension = 0, tag = 3),
// by dx=-0.05, dy=0, dz=0.
// Geometrical transformations take a vector of pairs of integers as first
// argument, which contains the list of entities, represented by (dimension,
// tag) pairs. Here we thus translate point 3 (dimension=0, tag=3), by
// dx=-0.05, dy=0, dz=0.
gmshModelGeoTranslate({{0, 3}}, -0.05, 0, 0);
// The "Duplicata" functionality in .geo files is handled by
// gmshModelGeoCopy(), which takes a vector of (dim,tag) pairs as input, and
// returns another vector of (dim,tag) pairs.
// gmshModelGeoCopy(), which takes a vector of (dim, tag) pairs as input, and
// returns another vector of (dim, tag) pairs.
std::vector<std::pair<int, int> > ov, ov2;
gmshModelGeoCopy({{0, 3}}, ov);
gmshModelGeoTranslate(ov, 0, 0.1, 0);
......@@ -83,16 +83,16 @@ int main(int argc, char **argv)
gmshModelGeoAddPlaneSurface(127, {126}, o);
// The API to create surface loops ("shells") and volumes is similar to the
// one use to create line loops and surfaces.
// one used to create line loops and surfaces.
gmshModelGeoAddSurfaceLoop(128, {127, 119, 121, 123, 125, 11}, o);
gmshModelGeoAddVolume(129, {128}, o);
// Extrusion works as expected, by providing a vector of (dim,tag) pairs as
// input, the translation vector, and a vector of (dim,tag) pairs as output.
// Extrusion works as expected, by providing a vector of (dim, tag) pairs as
// input, the translation vector, and a vector of (dim, tag) pairs as output.
gmshModelGeoExtrude({ov[1]}, 0, 0, 0.12, ov2);
// Mesh sizes associated to geometrical points can be set by passing a vector
// of (dim,tag) pairs for the corresponding entities
// of (dim, tag) pairs for the corresponding points.
gmshModelGeoSetMeshSize({{0,103}, {0,105}, {0,109}, {0,102}, {0,28},
{0, 24}, {0,6}, {0,5}}, lc * 3);
......
// This files reimplements gmsh/tutorial/t3.geo in C++.
#include <cmath>
#include <gmsh.h>
// this reimplements gmsh/tutorial/t3.geo
int main(int argc, char **argv)
{
gmshInitialize(argc, argv);
......@@ -10,7 +10,7 @@ int main(int argc, char **argv)
gmshModelCreate("t3");
// copy/paste from t1.cpp
// Copied from t1.cpp...
double lc = 1e-2;
int o;
gmshModelGeoAddPoint(1, 0, 0, 0, o, lc);
......@@ -29,15 +29,24 @@ int main(int argc, char **argv)
gmshModelAddPhysicalGroup(1, 2, {1, 2});
gmshModelAddPhysicalGroup(2, 6, {1});
gmshModelSetPhysicalName(2, 6, "My surface");
// end copy/paste
// ... end of copy
double h = 0.1, angle = 90.;
std::vector<std::pair<int, int> > ov;
// Extruding the mesh in addition to the geometry works as in .geo files: the
// number of elements for each layer and the (end) height of each layer are
// specified in two vectors.
gmshModelGeoExtrude({{2,1}}, 0, 0, h, ov, {8,2}, {0.5,1});
// Rotational and twisted extrusions are available as well with the built-in
// CAD kernel. The last (optional) argument for the Extrude/Revolve/Twist
// commands specified whether the extruded mesh should be recombined or not.
gmshModelGeoRevolve({{2,28}}, -0.1,0,0.1, 0,1,0, -M_PI/2, ov, {7});
gmshModelGeoTwist({{2,50}}, 0,0.15,0.25, -2*h,0,0, 1,0,0, angle*M_PI/180.,
ov, {10}, {}, true);
gmshModelAddPhysicalGroup(3, 101, {1, 2, ov[1].second});
gmshModelGeoSynchronize();
......
// This file reimplements gmsh/tutorial/t4.geo in C++.
#include <math.h>
#include <gmsh.h>
// this reimplements gmsh/tutorial/t4.geo
double hypoth(double a, double b){ return sqrt(a * a + b * b); }
int main(int argc, char **argv)
......@@ -77,9 +77,11 @@ int main(int argc, char **argv)
gmshModelGeoAddLineLoop(21, {17,-15,18,19,-20,16}, o);
gmshModelGeoAddPlaneSurface(22, {21}, o);
gmshModelGeoAddLineLoop(23, {11,-12,13,14,1,2,-3,4,5,6,7,-8,9,10}, o);
// A surface with one hole is specified using 2 line loops:
gmshModelGeoAddPlaneSurface(24, {23,21}, o);
// FIXME: this will be implemented through the gmshPost or gmshView API
// FIXME: this will be implemented through the gmshView API
/*
View "comments" {
T2(10, -10, 0){ StrCat("Created on ", Today, " with Gmsh") };
......@@ -90,6 +92,7 @@ int main(int argc, char **argv)
T2(350, -7, 0){ "file://image.png@20x0" };
};
*/
gmshModelGeoSynchronize();
gmshModelMesh(2);
......
// This file reimplements gmsh/tutorial/t5.geo in C++.
#include <gmsh.h>
#include <cstdio>
// this reimplements gmsh/tutorial/t5.geo
void cheeseHole(double x, double y, double z, double r, double lc,
std::vector<int> &shells, std::vector<int> &volumes)
{
// When the tag (first argument) is negative, the
int p1; gmshModelGeoAddPoint(-1, x, y, z, p1, lc);
int p2; gmshModelGeoAddPoint(-1, x+r,y, z, p2, lc);
int p3; gmshModelGeoAddPoint(-1, x, y+r,z, p3, lc);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment