• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

BoundarySurfaceOutput.cc

Go to the documentation of this file.
00001 //*****************************************************************//
00002 //    Albany 2.0:  Copyright 2012 Sandia Corporation               //
00003 //    This Software is released under the BSD license detailed     //
00004 //    in the file "license.txt" in the top-level Albany directory  //
00005 //*****************************************************************//
00006 
00007 #include "topology/Topology.h"
00008 #include <iostream>
00009 #include <fstream>
00010 #include <stdio.h>
00011 #include <string>
00012 #include <sstream>
00013 
00014 typedef stk::mesh::Entity Entity;
00015 
00016 #include <boost/numeric/ublas/matrix.hpp>
00017 #include <boost/numeric/ublas/io.hpp>
00018 #include <boost/numeric/ublas/matrix_proxy.hpp>
00019 #include <boost/numeric/ublas/vector.hpp>
00020 #include <boost/numeric/ublas/matrix_sparse.hpp>
00021 
00022 
00023 // \brief Function that helps creating text files with different names
00024 std::string itoa(int num);
00025 
00026 
00027 int main (int ac, char* av[]){
00028 
00029 
00030   std::string input_file;
00031   std::string normals_file;
00032   typedef stk::mesh::Entity Entity;
00033   std::string output_file;// = "output.e";
00034 
00035   using namespace std;
00036 
00037   if (ac == 3){
00038 
00039     std::cout <<  "Generating .msh boundary files with mesh " << av[1] << " and normals from file: " << av[2] << std::endl;
00040     stringstream ss1;
00041     stringstream ss2;
00042     ss1 << av[1]; //insert the char
00043     ss1 >> input_file; //extract -convert char into string
00044     ss2 << av[2];
00045     ss2 >> normals_file;
00046 
00047     //Check if third parameter from command line is a .txt file
00048     string s2 = ".txt";
00049     if (normals_file.find(s2) == std::string::npos) {
00050       std::cout << "Missing extension .txt for the third input" << '\n';
00051       throw(0);
00052     }
00053   }
00054   else {
00055     std::cout << "Usage:BoundarySurfaceOutput Mesh.exo Normals.txt" << std::endl; //USAGE OF THIS FILE FROM THE COMMAND LINE
00056     return 1;
00057   }
00058 
00059   //--------------------------------------------------------------------------------------------------------
00060   // Read the mesh
00061   //--------------------------------------------------------------------------------------------------------
00062   // Copied from Partition.cc
00063   Teuchos::GlobalMPISession mpiSession(&ac, &av);
00064   LCM::Topology topology(input_file, output_file);
00065 
00066 
00067   //--------------------------------------------------------------------------------------------------------
00068   //Extract the input file name
00069   //--------------------------------------------------------------------------------------------------------
00070   stringstream ss;
00071   std::string file_name_;
00072   ss << av[1]; //insert the char
00073   ss >> file_name_; //extract -convert char into string
00074   string exoFile = ".exo"; //Define the string with the extension file of input mesh
00075   std::size_t Position = file_name_.find(exoFile); //Find the position where .exo starts
00076   std::string file_name2 = file_name_.substr(0, Position); //sets part of the name of the output file
00077 
00078 
00079   //--------------------------------------------------------------------------------------------------------
00080   //Create the contour of the minimum surface
00081   //--------------------------------------------------------------------------------------------------------
00082 
00083   ifstream normals_(normals_file.c_str()); //Read the text file that contains all normals
00084 
00085   //numberNormals_ sets the number of rows of the matrix mNormals
00086   //This input text file must have the total number of normals defined in its first line
00087   int numberNormals_;
00088   if (normals_.is_open())
00089   {
00090     normals_ >> numberNormals_; //Obtain the total number of input normals
00091   }
00092 
00093   //mNormal is a matrix containing all the normal vectors
00094   std::vector<std::vector<double> > mNormals(numberNormals_,std::vector<double>(3,0));
00095 
00096   if (normals_.is_open())
00097   {
00098     for (int i = 0; i < numberNormals_ ; i++)
00099     {
00100       normals_ >> mNormals[i][0] >> mNormals[i][1]>> mNormals[i][2];
00101     }
00102   }
00103 
00104   //Extract the numbers from the matrix that contains the normals
00105   std::vector<std::vector<int> >BoundaryVector;
00106   for(int i = 0; i < numberNormals_; i++)
00107   {
00108     std::string file_name = "Boundary_"+file_name2+"_"+itoa(i+1) + ".msh"; //file_name2 comes from the second input in the command line
00109 
00110     ofstream mshFile(file_name.c_str(),std::ofstream::out);
00111     if ( mshFile.is_open())
00112     {
00113       //Obtain the vector that defines the 1D boundary
00114       std::vector<std::vector<int> >BoundaryVector;
00115 
00116       std::vector<double> normalToPlane;
00117       normalToPlane.push_back(mNormals.at(i).at(0));
00118       normalToPlane.push_back(mNormals.at(i).at(1));
00119       normalToPlane.push_back(mNormals.at(i).at(2));
00120 
00121       //Finds coordinates of 3 vectors normal to the normal vector.
00122       std::vector<std::vector<double> > pointsOnPlane = topology.getCoordinatesOfTriangle(normalToPlane);
00123 
00124 
00125       //Finds the Node (Entity of rank 0) that is closest to each of the points on the plane
00126       std::vector<Entity*> closestNodes = topology.getClosestNodesOnSurface(pointsOnPlane);
00127 
00128       //Finds the identifiers of the nodes (entity rank 0) along the shortest
00129       //path connecting the three points
00130       std::vector<std::vector<int> > ShortestPathFinal = topology.shortestpath(closestNodes);
00131 
00132       //THE CODE WRITTEN BELOW IS TO CREATE THE .msh FILE UTILISED BY VTK
00133       //Create a vector with the identifiers of the nodes that build the shortest path
00134       std::vector<int> nodesIdentifiers;
00135       for (unsigned int i = 0; i < ShortestPathFinal.size(); ++i){
00136         for (unsigned int j = 0; j < ShortestPathFinal[i].size()-1; ++j){
00137           nodesIdentifiers.push_back((ShortestPathFinal[i][j]));
00138           nodesIdentifiers.push_back((ShortestPathFinal[i][j+1]));
00139         }
00140       }
00141 
00142       //Create a vector without repeated numbers. Same vector as nodesIdentifiers, but without repeated indices
00143       std::vector<int> setOfNodes;
00144       std::vector<int>::const_iterator iteratorInt;
00145       for(unsigned int i =0; i<nodesIdentifiers.size(); i = i+2)
00146       {
00147         setOfNodes.push_back(nodesIdentifiers[i]);
00148       }
00149 
00150       int dimension = 3;
00151       int NumNodesPerElement = 2;
00152 
00153       mshFile << dimension <<" "<< NumNodesPerElement << endl;   //Dimension  nodesperElement
00154       mshFile << setOfNodes.size() <<" "<< ShortestPathFinal.size() <<endl; //Nnodes Nelements
00155 
00156       //Create a map that assigns new numbering to the nodes
00157       std::map <int,int> node_map; int counter = 0;
00158       std::vector<int>::const_iterator I_setOfNodes;
00159 
00160       for(I_setOfNodes = setOfNodes.begin();I_setOfNodes != setOfNodes.end();++I_setOfNodes)
00161       {
00162         std::vector<double> nodeCoordinates = topology.findCoordinates(*I_setOfNodes);
00163         mshFile << nodeCoordinates[0]<<" "<< nodeCoordinates[1]<<" "<<nodeCoordinates[2] << endl;//Coordinates list OUTPUT
00164         node_map[*I_setOfNodes] = counter;
00165 
00166         counter++;
00167       }
00168 
00169       //Create the connectivity matrix
00170       for (unsigned int i = 0; i < ShortestPathFinal.size(); ++i){
00171         for (unsigned int j = 0; j < ShortestPathFinal[i].size(); ++j){
00172           mshFile << (node_map.find(ShortestPathFinal[i][j])->second)+1 << " "; //List of edges
00173         }
00174         mshFile << endl;
00175       }
00176 
00177     }//End of creating the .msh file, which contains the information of the contour
00178   }//End of for loop
00179 
00180   return 0;
00181 }
00182 
00183 // \brief Function that helps creating text files with different names
00184 std::string itoa(int num) {
00185   std::stringstream converter;
00186   converter << num;
00187   return converter.str();
00188 }
00189 

Generated on Wed Mar 26 2014 18:36:37 for Albany: a Trilinos-based PDE code by  doxygen 1.7.1