Path: blob/devel/ElmerGUI/Application/plugins/nglib_api.cpp
3203 views
/*****************************************************************************1* *2* Elmer, A Finite Element Software for Multiphysical Problems *3* *4* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *5* *6* This program is free software; you can redistribute it and/or *7* modify it under the terms of the GNU General Public License *8* as published by the Free Software Foundation; either version 2 *9* of the License, or (at your option) any later version. *10* *11* This program is distributed in the hope that it will be useful, *12* but WITHOUT ANY WARRANTY; without even the implied warranty of *13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *14* GNU General Public License for more details. *15* *16* You should have received a copy of the GNU General Public License *17* along with this program (in file fem/GPL-2); if not, write to the *18* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *19* Boston, MA 02110-1301, USA. *20* *21*****************************************************************************/2223/*****************************************************************************24* *25* ELMER/Mesh3D nglib_api *26* *27*****************************************************************************28* *29* Authors: Mikko Lyly, Juha Ruokolainen and Peter Raback *30* Email: [email protected] *31* Web: http://www.csc.fi/elmer *32* Address: CSC - IT Center for Science Ltd. *33* Keilaranta 14 *34* 02101 Espoo, Finland *35* *36* Original Date: 15 Mar 2008 *37* *38*****************************************************************************/3940#include <iostream>41#include "nglib_api.h"4243using namespace std;4445NglibAPI::NglibAPI()46{47}484950NglibAPI::~NglibAPI()51{52}5354void NglibAPI::setDim(int ngDim)55{56this->ngDim = ngDim;57}5859int NglibAPI::getDim() const60{61return this->ngDim;62}6364void NglibAPI::setNgmesh(nglib::Ng_Mesh *ngmesh)65{66this->ngmesh = ngmesh;67}6869void NglibAPI::setNggeom2D(nglib::Ng_Geometry_2D *geom2d)70{71this->geom2d = geom2d;72}7374// Populate elmer's mesh structure:75//-----------------------------------------------------------------------------76mesh_t* NglibAPI::createElmerMeshStructure()77{78mesh_t *mesh = new mesh_t;7980mesh->setNodes(0);81mesh->setPoints(0);82mesh->setEdges(0);83mesh->setSurfaces(0);84mesh->setElements(0);8586bool twod = (ngDim == 2) ? true : false;8788if(twod) {89create2D(mesh);90} else {91create3D(mesh);92}9394return mesh;95}9697void NglibAPI::create2D(mesh_t *mesh)98{99Meshutils meshutils;100101// Node points:102//--------------103mesh->setNodes(nglib::Ng_GetNP_2D(ngmesh));104mesh->newNodeArray(mesh->getNodes());105106for(int i = 0; i < mesh->getNodes(); i++) {107node_t *node = mesh->getNode(i);108109double *x = node->getXvec();110x[0] = 0; x[1] = 0; x[2] = 0;111nglib::Ng_GetPoint_2D(ngmesh, i + 1, x);112113node->setIndex(-1); // default114}115116// Boundary elements:117//--------------------118mesh->setEdges(nglib::Ng_GetNSeg_2D(ngmesh));119mesh->newEdgeArray(mesh->getEdges());120121for(int i = 0; i < mesh->getEdges(); i++) {122edge_t *edge = mesh->getEdge(i);123124edge->setNature(PDE_BOUNDARY);125edge->setCode(202);126edge->setNodes(2);127edge->newNodeIndexes(2);128edge->setPoints(2);129edge->newPointIndexes(2);130131edge->setPointIndex(0, -1);132edge->setPointIndex(1, -1);133134int matIdx;135nglib::Ng_GetSegment_2D(ngmesh, i + 1, edge->getNodeIndexes(), &matIdx);136137int bcIdx;138nglib::EG_GetSegmentBCProperty(ngmesh, geom2d, matIdx-1, &bcIdx);139140edge->setIndex(bcIdx);141142edge->setNodeIndex(0, edge->getNodeIndex(0) - 1);143edge->setNodeIndex(1, edge->getNodeIndex(1) - 1);144145// swap orientation:146//------------------147int tmp = edge->getNodeIndex(0);148edge->setNodeIndex(0, edge->getNodeIndex(1));149edge->setNodeIndex(1, tmp);150}151152// Elements:153//-----------154mesh->setSurfaces(nglib::Ng_GetNE_2D(ngmesh));155mesh->newSurfaceArray(mesh->getSurfaces());156157double n[3];158n[0] = 0; n[1] = 0; n[2] = -1;159160for(int i = 0; i < mesh->getSurfaces(); i++) {161surface_t *surface = mesh->getSurface(i);162163surface->setNature(PDE_BULK);164surface->setCode(303);165surface->setNodes(3);166surface->newNodeIndexes(3);167168int matIdx;169170nglib::Ng_GetElement_2D(ngmesh, i+1, surface->getNodeIndexes(), &matIdx);171172surface->setIndex(matIdx);173174surface->setNodeIndex(0, surface->getNodeIndex(0) - 1);175surface->setNodeIndex(1, surface->getNodeIndex(1) - 1);176surface->setNodeIndex(2, surface->getNodeIndex(2) - 1);177178surface->setNormalVec(n);179}180181// Find parents for edge elements:182//---------------------------------183meshutils.findEdgeElementParents(mesh);184185mesh->setDim(ngDim);186mesh->setCdim(ngDim);187}188189void NglibAPI::create3D(mesh_t *mesh)190{191Meshutils meshutils;192193// Node points:194//--------------195mesh->setNodes(nglib::Ng_GetNP(ngmesh));196mesh->newNodeArray(mesh->getNodes());197198for(int i = 0; i < mesh->getNodes(); i++) {199node_t *node = mesh->getNode(i);200nglib::Ng_GetPoint(ngmesh, i+1, node->getXvec());201node->setIndex(-1); // default202}203204// Boundary elements:205//--------------------206mesh->setSurfaces(nglib::Ng_GetNSE(ngmesh));207mesh->newSurfaceArray(mesh->getSurfaces());208209for(int i = 0; i < mesh->getSurfaces(); i++) {210surface_t *surface = mesh->getSurface(i);211212surface->setNature(PDE_BOUNDARY);213surface->setCode(303);214surface->setNodes(3);215surface->newNodeIndexes(3);216surface->setEdges(3);217surface->newEdgeIndexes(3);218219int face = nglib::EG_GetSurfaceElementBCProperty(ngmesh, i+1);220221surface->setIndex(face);222223surface->setEdgeIndex(0, -1);224surface->setEdgeIndex(1, -1);225surface->setEdgeIndex(2, -1);226227nglib::Ng_GetSurfaceElement(ngmesh, i+1, surface->getNodeIndexes());228229surface->setNodeIndex(0, surface->getNodeIndex(0) - 1);230surface->setNodeIndex(1, surface->getNodeIndex(1) - 1);231surface->setNodeIndex(2, surface->getNodeIndex(2) - 1);232233// swap orientation:234//------------------235int tmp = surface->getNodeIndex(1);236surface->setNodeIndex(1, surface->getNodeIndex(2));237surface->setNodeIndex(2, tmp);238}239240// Elements:241//-----------242mesh->setElements(nglib::Ng_GetNE(ngmesh));243mesh->newElementArray(mesh->getElements());244245for(int i = 0; i < mesh->getElements(); i++) {246element_t *element = mesh->getElement(i);247248element->setNature(PDE_BULK);249element->setCode(504);250element->setNodes(4);251element->newNodeIndexes(4);252253nglib::Ng_GetVolumeElement(ngmesh, i+1, element->getNodeIndexes());254255element->setNodeIndex(0, element->getNodeIndex(0) - 1);256element->setNodeIndex(1, element->getNodeIndex(1) - 1);257element->setNodeIndex(2, element->getNodeIndex(2) - 1);258element->setNodeIndex(3, element->getNodeIndex(3) - 1);259260element->setIndex(1); // default (no multibody meshing atm)261}262263// Find parents for surface elements:264//------------------------------------265meshutils.findSurfaceElementParents(mesh);266267// Find edges for surface elements:268//----------------------------------269meshutils.findSurfaceElementEdges(mesh);270271// Compute normals for boundary elements:272//---------------------------------------273meshutils.findSurfaceElementNormals(mesh);274275mesh->setDim(ngDim);276mesh->setCdim(ngDim);277}278279280