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

new C++ tutos

parent 864ceb0e
Branches
Tags
No related merge requests found
// This file reimplements gmsh/tutorial/t11.geo in C++.
#include <gmsh.h>
int main(int argc, char **argv)
{
gmsh::initialize();
gmsh::option::setNumber("General.Terminal", 1);
gmsh::model::add("t11");
int p1 = gmsh::model::geo::addPoint(-1.25, -.5, 0);
int p2 = gmsh::model::geo::addPoint(1.25, -.5, 0);
int p3 = gmsh::model::geo::addPoint(1.25, 1.25, 0);
int p4 = gmsh::model::geo::addPoint(-1.25, 1.25, 0);
int l1 = gmsh::model::geo::addLine(p1, p2);
int l2 = gmsh::model::geo::addLine(p2, p3);
int l3 = gmsh::model::geo::addLine(p3, p4);
int l4 = gmsh::model::geo::addLine(p4, p1);
int cl = gmsh::model::geo::addCurveLoop({l1, l2, l3, l4});
int pl = gmsh::model::geo::addPlaneSurface({cl});
gmsh::model::geo::synchronize();
// add an analytical size field with tag 1
gmsh::model::mesh::field::add("MathEval", 1);
gmsh::model::mesh::field::setString
(1, "F", "0.01*(1.0+30.*(y-x*x)*(y-x*x) + (1-x)*(1-x))");
gmsh::model::mesh::field::setAsBackgroundMesh(1);
// to generate quadrangles instead of triangles for the plane surface, add the
// recombine constraint before meshing
gmsh::model::mesh::setRecombine(2, pl);
gmsh::model::mesh::generate(2);
// you could also set the option "Mesh.RecombineAll"
// gmsh::option::setNumber("Mesh.RecombineAll", 1);
// gmsh::model::mesh::generate(2);
// You could also apply the recombination algo after meshing
// gmsh::model::mesh::generate(2);
// gmsh::model::mesh::recombine();
// Better 2D planar quadrilateral meshes with the Frontal-Delaunay for quads
// algorithm:
// gmsh::option::setNumber("Mesh.Algorithm", 8);
// gmsh::model::mesh::generate(2);
// Force a full-quad mesh with either option
// gmsh::option::setNumber("Mesh.SubdivisionAlgorithm", 1);
// gmsh::option::setNumber("Mesh.RecombinationAlgorithm", 2); // or 3
// gmsh::model::mesh::generate(2);
gmsh::fltk::run();
gmsh::finalize();
return 0;
}
// This file reimplements gmsh/tutorial/t12.geo in C++.
#include <gmsh.h>
namespace factory = gmsh::model::geo;
int main(int argc, char **argv)
{
gmsh::initialize();
gmsh::option::setNumber("General.Terminal", 1);
gmsh::model::add("t12");
double lc = 0.1;
factory::addPoint(0, 0, 0, lc, 1);
factory::addPoint(1, 0, 0, lc, 2);
factory::addPoint(1, 1, 0.5, lc, 3);
factory::addPoint(0, 1, 0.4, lc, 4);
factory::addPoint(0.3, 0.2, 0, lc, 5);
factory::addPoint(0, 0.01, 0.01, lc, 6);
factory::addPoint(0, 0.02, 0.02, lc, 7);
factory::addPoint(1, 0.05, 0.02, lc, 8);
factory::addPoint(1, 0.32, 0.02, lc, 9);
factory::addLine(1, 2, 1);
factory::addLine(2, 8, 2);
factory::addLine(8, 9, 3);
factory::addLine(9, 3, 4);
factory::addLine(3, 4, 5);
factory::addLine(4, 7, 6);
factory::addLine(7, 6, 7);
factory::addLine(6, 1, 8);
factory::addSpline({7, 5, 9}, 9);
factory::addLine(6, 8, 10);
factory::addCurveLoop({5, 6, 9, 4}, 11);
factory::addSurfaceFilling({11}, 1);
factory::addCurveLoop({-9, 3, 10, 7}, 13);
factory::addSurfaceFilling({13}, 5);
factory::addCurveLoop({-10, 2, 1, 8}, 15);
factory::addSurfaceFilling({15}, 10);
factory::synchronize();
// set compound curve (dim 1); of curves 2, 3, 4
gmsh::model::mesh::setCompound(1, {2, 3, 4});
// set compound curve of curves 6, 7, 8
gmsh::model::mesh::setCompound(1, {6, 7, 8});
// set compound surface from surfaces 1, 5, 10
gmsh::model::mesh::setCompound(2, {1, 5, 10});
gmsh::fltk::run();
gmsh::finalize();
return 0;
}
// This file reimplements gmsh/tutorial/t13.geo in C++.
#include <gmsh.h>
#include <math.h>
namespace factory = gmsh::model::geo;
int main(int argc, char **argv)
{
gmsh::initialize();
gmsh::option::setNumber("General.Terminal", 1);
gmsh::model::add("t13");
// Let's merge an STL mesh that we would like to remesh (from the parent
// directory):
try {
gmsh::merge("../t13_data.stl");
}
catch(...) {
gmsh::logger::write("Could not load STL mesh: bye!");
gmsh::finalize();
return 0;
}
// We first classify ("color") the surfaces by splitting the original surface
// along sharp geometrical features. This will create new discrete surfaces,
// curves and points.
double angle = 40; // Angle for surface detection
bool forceParametrizablePatches = false; // Create surfaces guaranteed to be parametrizable?
bool includeBoundary = true;
gmsh::model::mesh::classifySurfaces(angle * M_PI / 180.,
includeBoundary,
forceParametrizablePatches);
// Create a geometry for all the discrete curves and surfaces in the mesh, by
// computing a parametrization for each one
gmsh::model::mesh::createGeometry();
// Create a volume from all the surfaces
std::vector<std::pair<int, int> > s;
gmsh::model::getEntities(s, 2);
std::vector<int> sl;
for(std::size_t i = 0; i < s.size(); i++) sl.push_back(s[i].second);
int l = gmsh::model::geo::addSurfaceLoop(sl);
gmsh::model::geo::addVolume({l});
gmsh::model::geo::synchronize();
// element size imposed by a size field, just because we can :-)
bool funny = true;//false;
int f = gmsh::model::mesh::field::add("MathEval");
if(funny)
gmsh::model::mesh::field::setString(f, "F", "2*Sin((x+y)/5) + 3");
else
gmsh::model::mesh::field::setString(f, "F", "4");
gmsh::model::mesh::field::setAsBackgroundMesh(f);
gmsh::model::mesh::generate(3);
gmsh::fltk::run();
gmsh::finalize();
return 0;
}
// This file reimplements gmsh/tutorial/t14.geo in Python.
// Homology computation in Gmsh finds representative chains of (relative)
// (co)homology space bases using a mesh of a model. The representative basis
// chains are stored in the mesh as physical groups of Gmsh, one for each chain.
#include <gmsh.h_cwrap>
#include <math.h>
namespace factory = gmsh::model::geo;
int main(int argc, char **argv)
{
gmsh::initialize(argc, argv);
gmsh::option::setNumber("General.Terminal", 1);
gmsh::model::add("t14");
double m = 0.5; // mesh characteristic length
double h = 2; // geometry height in the z-direction
gmsh::model::geo::addPoint(0, 0, 0, m, 1);
gmsh::model::geo::addPoint(10, 0, 0, m, 2);
gmsh::model::geo::addPoint(10, 10, 0, m, 3);
gmsh::model::geo::addPoint(0, 10, 0, m, 4);
gmsh::model::geo::addPoint(4, 4, 0, m, 5);
gmsh::model::geo::addPoint(6, 4, 0, m, 6);
gmsh::model::geo::addPoint(6, 6, 0, m, 7);
gmsh::model::geo::addPoint(4, 6, 0, m, 8);
gmsh::model::geo::addPoint(2, 0, 0, m, 9);
gmsh::model::geo::addPoint(8, 0, 0, m, 10);
gmsh::model::geo::addPoint(2, 10, 0, m, 11);
gmsh::model::geo::addPoint(8, 10, 0, m, 12);
gmsh::model::geo::addLine(1, 9, 1);
gmsh::model::geo::addLine(9, 10, 2);
gmsh::model::geo::addLine(10, 2, 3);
gmsh::model::geo::addLine(2, 3, 4);
gmsh::model::geo::addLine(3, 12, 5);
gmsh::model::geo::addLine(12, 11, 6);
gmsh::model::geo::addLine(11, 4, 7);
gmsh::model::geo::addLine(4, 1, 8);
gmsh::model::geo::addLine(5, 6, 9);
gmsh::model::geo::addLine(6, 7, 10);
gmsh::model::geo::addLine(7, 8, 11);
gmsh::model::geo::addLine(8, 5, 12);
gmsh::model::geo::addCurveLoop({6, 7, 8, 1, 2, 3, 4, 5}, 13);
gmsh::model::geo::addCurveLoop({11, 12, 9, 10}, 14);
gmsh::model::geo::addPlaneSurface({13, 14}, 15);
std::vector<std::pair<int, int> > ext_tags;
gmsh::model::geo::extrude({{2,15}}, 0, 0, h, ext_tags);
// Create physical groups, which are used to define the domain of the
// (co);homology computation and the subdomain of the relative (co);homology
// computation.
// Whole domain
int domain_tag = 1;
int domain_physical_tag = 1001;
gmsh::model::addPhysicalGroup(3,{domain_tag}, domain_physical_tag);
gmsh::model::setPhysicalName(3, domain_physical_tag, "Whole domain");
// Four "terminals" of the model
std::vector<int> terminal_tags = {36, 44, 52, 60};
int terminals_physical_tag = 2001;
gmsh::model::addPhysicalGroup(2, terminal_tags, terminals_physical_tag);
gmsh::model::setPhysicalName(2, terminals_physical_tag, "Terminals");
// Find domain boundary tags
std::vector<std::pair<int, int> > boundary_dimtags;
gmsh::model::getBoundary({{3, domain_tag}}, boundary_dimtags, false, false);
std::vector<int> boundary_tags, complement_tags;
for(auto it = boundary_dimtags.begin(); it != boundary_dimtags.end(); ++it) {
complement_tags.push_back(it->second);
boundary_tags.push_back(it->second);
}
for(auto it = terminal_tags.begin(); it != terminal_tags.end(); ++it) {
auto it2 = std::find(complement_tags.begin(), complement_tags.end(), *it);
if(it2 != complement_tags.end()) complement_tags.erase(it2);
}
// Whole domain surface
int boundary_physical_tag = 2002;
gmsh::model::addPhysicalGroup(2, boundary_tags, boundary_physical_tag);
gmsh::model::setPhysicalName(2, boundary_physical_tag, "Boundary");
// Complement of the domain surface respect to the four terminals
int complement_physical_tag = 2003;
gmsh::model::addPhysicalGroup(2, complement_tags, complement_physical_tag);
gmsh::model::setPhysicalName(2, complement_physical_tag, "Complement");
gmsh::model::geo::synchronize();
// Find bases for relative homology spaces of the domain modulo the four
// terminals
gmsh::model::mesh::computeHomology({domain_physical_tag},
{terminals_physical_tag},
{0,1,2,3});
// Find homology space bases isomorphic to the previous bases: homology spaces
// modulo the non-terminal domain surface, a.k.a the thin cuts
gmsh::model::mesh::computeHomology({domain_physical_tag},
{complement_physical_tag},
{0,1,2,3});
// Find cohomology space bases isomorphic to the previous bases: cohomology
// spaces of the domain modulo the four terminals, a.k.a the thick cuts
gmsh::model::mesh::computeCohomology({domain_physical_tag},
{terminals_physical_tag},
{0,1,2,3});
// more examples
// gmsh::model::mesh::computeHomology();
// gmsh::model::mesh::computeHomology({domain_physical_tag});
// gmsh::model::mesh::computeHomology({domain_physical_tag},
// {boundary_physical_tag},
// {0,1,2,3});
// Generate the mesh and perform the requested homology computations
gmsh::model::mesh::generate(3);
// Find physical tags of a certain homology or cohomology space chains
/*
physicals = gmsh::model::getPhysicalGroups();
for pi in physicals:
name = gmsh::model::getPhysicalName(pi{0}, pi{1});
if(name.find("H^1"); == 0);: # find tags of all cohomology chains of dimension 1
print("H^1 tag: " + str(pi{1}); + ", name: " + name);
gmsh.write("t14.msh");
*/
gmsh::fltk::run();
gmsh::finalize();
return 0;
}
// This file reimplements gmsh/tutorial/t7.geo in C++.
//
// Background mesh
#include <gmsh.h>
namespace model = gmsh::model;
namespace factory = gmsh::model::geo;
int main(int argc, char **argv)
{
gmsh::initialize();
gmsh::option::setNumber("General.Terminal", 1);
model::add("t7");
// Copied from t1.cpp...
double lc = 1e-2;
factory::addPoint(0, 0, 0, lc, 1);
factory::addPoint(.1, 0, 0, lc, 2);
factory::addPoint(.1, .3, 0, lc, 3);
factory::addPoint(0, .3, 0, lc, 4);
factory::addLine(1, 2, 1);
factory::addLine(3, 2, 2);
factory::addLine(3, 4, 3);
factory::addLine(4, 1, 4);
factory::addCurveLoop({4, 1, -2, 3}, 1);
factory::addPlaneSurface({1}, 1);
factory::synchronize();
// add the background mesh file as a view
gmsh::merge("../t7_bgmesh.pos");
// add the post-processing view as a new size field
int bg_field = model::mesh::field::add("PostView");
model::mesh::field::setAsBackgroundMesh(bg_field);
model::mesh::generate(2);
gmsh::write("t7.msh");
// show the mesh file
gmsh::fltk::run();
gmsh::finalize();
return 0;
}
// This file reimplements gmsh/tutorial/t9.geo in C++.
#include <gmsh.h>
int main(int argc, char **argv)
{
gmsh::initialize();
gmsh::option::setNumber("General.Terminal", 1);
gmsh::model::add("t9");
// add a three-dimensional scalar view to work on:
try {
gmsh::merge("../view3.pos");
}
catch(...) {
gmsh::logger::write("Could not load post-processing views: bye!");
gmsh::finalize();
return 0;
}
// First plugin is Isosurface
gmsh::plugin::setNumber("Isosurface", "Value", 0.67);
gmsh::plugin::setNumber("Isosurface", "View", 0);
gmsh::plugin::run("Isosurface");
// Second is CutPlane
gmsh::plugin::setNumber("CutPlane", "A", 0);
gmsh::plugin::setNumber("CutPlane", "B", 0.2);
gmsh::plugin::setNumber("CutPlane", "C", 1);
gmsh::plugin::setNumber("CutPlane", "D", 0);
gmsh::plugin::setNumber("CutPlane", "View", 0);
gmsh::plugin::run("CutPlane");
// Third is Annotate
gmsh::plugin::setString("Annotate", "Text", "A nice title");
// By convention, window coordinates larger than 99999 represent the center
gmsh::plugin::setNumber("Annotate", "X", 1.e5);
gmsh::plugin::setNumber("Annotate", "Y", 50);
gmsh::plugin::setString("Annotate", "Font", "Times-BoldItalic");
gmsh::plugin::setNumber("Annotate", "FontSize", 28);
gmsh::plugin::setString("Annotate", "Align", "Center");
gmsh::plugin::setNumber("Annotate", "View", 0);
gmsh::plugin::run("Annotate");
gmsh::plugin::setString("Annotate", "Text", "(and a small subtitle)");
gmsh::plugin::setNumber("Annotate", "Y", 70);
gmsh::plugin::setString("Annotate", "Font", "Times-Roman");
gmsh::plugin::setNumber("Annotate", "FontSize", 12);
gmsh::plugin::run("Annotate");
// set some general options
gmsh::option::setNumber("View[0].Light", 1);
gmsh::option::setNumber("View[0].IntervalsType", 1);
gmsh::option::setNumber("View[0].NbIso", 6);
gmsh::option::setNumber("View[0].SmoothNormals", 1);
gmsh::option::setNumber("View[1].IntervalsType", 2);
gmsh::option::setNumber("View[2].IntervalsType", 2);
// show the GUI at the end
gmsh::fltk::run();
gmsh::finalize();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment