On Wed, Aug 26, 2009 at 1:19 AM, Flavio Cimolin<[email protected]> wrote:
> Hello everybody,
>
> I have a 2D mesh (for example in Nastran or STL format), which I want to
> import in freeFEM++.
> Surfing the archives I have seen that by means of gmsh it is possible to
> convert
> the .stl into the .mesh format, which can easily be modified from 3D to
> 2D in
> order to be importable by freeFEM++ with the readmesh command.
>
> However, I cannot figure out how to set the boundary labels of the mesh
> in order
> to properly define boundary conditions for the differential problem. Is
> it possible
> to import some kind of mesh realized outside freeFEM++ together with the
> boundary labels,
> or otherwise to easily re-define the boundaries from inside freeFEM++?
> What would you recommend for performing this task?

The basic problem here is that when Gmsh exports a mesh in MEDIT .mesh
format, it only exports certain element types (triangles,
quadrilaterals, and tetrahedra); see the function GModel::writeMESH in
Geo/GModelIO_Mesh.cpp.  In particular, it fails to export the 2-node
edge elements that constitute the boundary of a two-dimensional mesh.
You'll find that it works fine in three dimensions, since tetrahedra
(the domain elements) and triangles (the boundary elements) are
exported.
  I imagine that it shouldn't be too difficult to modify the Gmsh
source code to get it to export the edge elements required for
boundaries in two dimensions, but I'm not sufficiently familiar with
the internal coding of Gmsh yet to attempt this myself.  I'm copying
this to the Gmsh mailing list to see whether anyone there has any
suggestions.
  What I've done as a workaround in two dimensions is actually export
the Gmsh mesh in its native .msh format and then written a simple
translation function in GNU Octave.  I attach that in case it's
helpful.
## Copyright (C) 2009 G. D. McBain

## -*- texinfo -*-
## @deftypefn {Function File} {} msh2mesh (@var{filenamestub})
## Translate the Gmsh .msh two-dimensional mesh file
## @var{filenamestub}.msh into MEDIT format, @var{filenamestub}.mesh.
## This is written for speed rather than generality and makes several
## assumptions about the form and content of the input; it does no
## error-checking.
## @end deftypefn

## Author: G. D. McBain <[email protected]>
## Created: 2009-08-21
## Keywords: finite element, meshing, Gmsh, MEDIT

function msh2mesh (filenamestub, fout)
  
  fin = fopen (strcat (filenamestub, ".msh"));
  
  ## Write boilerplate
  fout = fopen (strcat (filenamestub, ".mesh"), "w");
  fdisp (fout, "MeshVersionFormatted\n1");
  fdisp (fout, "Dimension\n2");

  ## Read boilerplate and nodes
  do
    txt = fgetl (fin);
    until (regexp (txt, "[$]Nodes"))
  nnodes = fscanf (fin, "%d", 1);
  nodes = fscanf (fin, "%g", [4, nnodes])';
  printf ("Read %d nodes.\n", rows (nodes));

  ## Write nodes
  fprintf (fout, "Vertices\n%d\n", nnodes);
  fprintf (fout, "%.16g %.16g %d\n",
	   vertcat (nodes(:,2:3)', zeros (1, nnodes)) );

  ## Read edges
  do
    txt = fgetl (fin);
  until (regexp (txt, "[$]Elements"))
  nelts = fscanf (fin, "%d", 1); # number of elements
  fgetl (fin);			# discard rest of line
  edges = zeros (nelts, 3);
  nedges = 0;
  while (true)
    eltline = fgetl (fin);
    eltdata = sscanf (eltline, "%d", 8);
    if (eltdata(2) != 1)
      printf ("Read %d edges.\n", nedges);
      break;			# done reading edges
    endif
    edges(++nedges,:) = eltdata([7:8,4]);
  endwhile

  ## Write edges
  fprintf (fout, "Edges\n%d\n", nedges);
  fprintf (fout, "%d %d %d\n", edges(1:nedges,:)');

  ## Read triangles
  ntriangles = nelts - nedges
  triangles = zeros (ntriangles, 4);
  eltdata = sscanf (eltline, "%d", 9); # reread first triangle
  triangles(1,:) = eltdata([7:9,4]);
  eltdata = fscanf (fin, "%d", [9, ntriangles-1]); # rest of triangles
  triangles(2:end,:) = eltdata([7:9,4], :)';
  
  fclose (fin);

  ## Write triangles
  fprintf (fout, "Triangles\n%d\n", ntriangles);
  fprintf (fout, "%d %d %d %d\n", triangles');

  fclose (fout);
endfunction
_______________________________________________
gmsh mailing list
[email protected]
http://www.geuz.org/mailman/listinfo/gmsh

Reply via email to