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

MinSurface.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 <iostream>
00008 #include <fstream>
00009 #include <stdio.h>
00010 #include <string>
00011 #include <sstream>
00012 
00013 #include <boost/config.hpp>
00014 #include <boost/graph/graph_traits.hpp>
00015 #include <boost/graph/adjacency_list.hpp>
00016 #include <boost/graph/dijkstra_shortest_paths.hpp>
00017 #include <boost/lexical_cast.hpp>
00018 #include <boost/numeric/conversion/cast.hpp>
00019 
00020 #include "topology/Topology.h"
00021 
00022 using namespace boost;
00023 
00024 int main(int ac, char* av[]){
00025 
00026   typedef adjacency_list < listS, vecS, undirectedS,no_property, property < edge_weight_t, int > > graph_t;
00027   typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
00028   typedef stk::mesh::Entity Entity;
00029   typedef std::pair<int, int> Edge;
00030 
00031   //---------------------------------------------------------------------------------------------------------
00032   //
00033   // Create a command line processor and parse command line options
00034   //
00035   Teuchos::CommandLineProcessor command_line_processor;
00036 
00037   command_line_processor.setDocString("Test of barycentric subdivision.\n"
00038       "Reads in a mesh and applies the barycentric subdivision algorithm.\n"
00039       "Restricted to simplicial complexes.\n");
00040 
00041   std::string input_file = "input.e";
00042   command_line_processor.setOption("input", &input_file, "Input File Name");
00043 
00044   std::string output_file = "output.e";
00045   command_line_processor.setOption("output", &output_file, "Output File Name");
00046 
00047   // Throw a warning and not error for unrecognized options
00048   command_line_processor.recogniseAllOptions(true);
00049 
00050   // Don't throw exceptions for errors
00051   command_line_processor.throwExceptions(false);
00052 
00053   // Parse command line
00054   Teuchos::CommandLineProcessor::EParseCommandLineReturn parse_return =
00055       command_line_processor.parse(ac, av);
00056 
00057   if (parse_return == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED) {
00058     return 0;
00059   }
00060 
00061   if (parse_return != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
00062     return 1;
00063   }
00064 
00065   //
00066   // Read the mesh
00067   //
00068   // Copied from Partition.cc
00069   Teuchos::GlobalMPISession mpiSession(&ac, &av);
00070   LCM::Topology topology(input_file, output_file);
00071 
00072 
00073     //-----------------------------------------------------------------------------------------
00074   //GET THE 1D BUNDARY FROM THE INPUT MESH USING dijkstra_shortest_paths
00075   //-----------------------------------------------------------------------------------------
00076   stk::mesh::BulkData* bulkData_ = topology.getBulkData();
00077   std::vector<Entity*> MeshNodes = topology.getEntitiesByRank(
00078       *(bulkData_), 0);
00079 
00080 
00081     //Definition of parameters and arrays of the function Graph
00082   const int TotalNumberNodes = MeshNodes.size();//Total number of nodes of the input the mesh
00083   std::vector<int> _nodeNames = topology.nodeNames();//Vector with node names
00084 
00085   //Define edges and weights
00086   std::vector<Entity*> MeshEdges = topology.getEntitiesByRank(
00087         *(bulkData_), 1); //Get all the edges of the mesh
00088 
00089   //Initialize Array of edges
00090   const int ArraySize = MeshEdges.size();
00091   Edge* EdgesArray;
00092   EdgesArray = new Edge[ArraySize];
00093 
00094   //Create an array that holds the distances (or weights) of each of the edges
00095   double* EdgesWeights;
00096   EdgesWeights = new double[ArraySize];
00097 
00098   for (unsigned int i = 0; i < MeshEdges.size();++i){
00099 
00100      std::vector<Entity*> EdgeBoundaryNodes(2);
00101      EdgeBoundaryNodes = topology.getDirectlyConnectedEntities((*MeshEdges[i]),0);
00102      EdgesArray[i] = Edge((EdgeBoundaryNodes[0]->identifier())-1,
00103          (EdgeBoundaryNodes[1]->identifier())-1);
00104      //Adds the distance of the ith edge to the vector of edge distances
00105       EdgesWeights[i] = (topology.getDistanceBetweenNodes(
00106          EdgeBoundaryNodes[0],EdgeBoundaryNodes[1]));
00107   }
00108 
00109 
00110   //Definition of the graph
00111   graph_t Graph(EdgesArray, EdgesArray + ArraySize, EdgesWeights, TotalNumberNodes);
00112 
00113   std::vector<vertex_descriptor> predecessor(num_vertices(Graph));//The vertex descriptor associates a single vertex in the graph
00114   std::vector<int> dis(num_vertices(Graph));
00115   vertex_descriptor source = vertex(3, Graph); //Source from where the distance and path are calculated
00116   dijkstra_shortest_paths(Graph, source, predecessor_map(&predecessor[0]).distance_map(&dis[0])); //Compute the shortest path
00117 
00118   delete [] EdgesArray;//Deallocate memory
00119   delete [] EdgesWeights;//Deallocate memory
00120 
00121 
00122   //-------------------------------------------------------------------------------
00123   //Presenting the final output
00124   //-------------------------------------------------------------------------------
00125   std::cout << "Display distances to all graph vertices from source" << std::endl;
00126   graph_traits < graph_t >::vertex_iterator VertexIterator, vend;
00127   for (boost::tie(VertexIterator, vend) = vertices(Graph); VertexIterator != vend; ++VertexIterator) {
00128     std::cout << "distance(" << _nodeNames[*VertexIterator] << ") = " << dis[*VertexIterator] << std::endl;
00129   }
00130   std::cout << std::endl;
00131   //"predecessor" is the predecessor map obtained from dijkstra_shortest_paths
00132   std::vector< graph_traits< graph_t >::vertex_descriptor > ShortestPath;
00133 
00134   //When changing the sourceVertex, MAKE SURE TO CHANGE "source" above. Otherwise, an run time error will be displayed
00135   vertex_descriptor sourceVertex = vertex(3,Graph);//Source from where the distance and path are calculated (Same as above!)
00136   vertex_descriptor goalVertex = vertex(4, Graph);
00137 
00138 
00139   graph_traits< graph_t >::vertex_descriptor currentVertex=goalVertex;
00140   while(currentVertex!=sourceVertex) { ShortestPath.push_back(currentVertex);
00141     currentVertex=predecessor[currentVertex];
00142   }
00143   //ShortestPath contains the shortest path between the given vertices.
00144   //Vertices are saved starting from end vertex to start vertex. The format of this output is "unsigned long int"
00145   ShortestPath.push_back(sourceVertex);
00146 
00147   //Change the format of the output from unsigned long int
00148   std::vector<int> FV;
00149     std::vector<unsigned long int>::const_iterator I_points;
00150     for (I_points = ShortestPath.begin();I_points != ShortestPath.end();++I_points){
00151       int i =boost::numeric_cast<int>(*I_points);
00152         FV.push_back(i);
00153     }
00154 
00155     //This prints the path reversed use reverse_iterator
00156   std::vector< graph_traits< graph_t >::vertex_descriptor >::iterator Iterator_;
00157   std::cout << "From end node to start node"<< std::endl;
00158   for (Iterator_=ShortestPath.begin(); Iterator_ != ShortestPath.end(); ++Iterator_) {
00159     std::cout << _nodeNames[*Iterator_] << " ";}
00160     std::cout << std::endl;
00161 
00162 
00163   return 0;
00164 }

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