Skip to content
Snippets Groups Projects
Commit 950b5954 authored by Tom Fischer's avatar Tom Fischer
Browse files

Merge branch 'master' of github.com:ufz/ogs

parents 92f1a134 cb0e9a7c
No related branches found
No related tags found
No related merge requests found
...@@ -36,6 +36,7 @@ ADD_SUBDIRECTORY( GeoLib ) ...@@ -36,6 +36,7 @@ ADD_SUBDIRECTORY( GeoLib )
ADD_SUBDIRECTORY( MathLib ) ADD_SUBDIRECTORY( MathLib )
ADD_SUBDIRECTORY( MeshLib ) ADD_SUBDIRECTORY( MeshLib )
ADD_SUBDIRECTORY( SimpleTests/MatrixTests ) ADD_SUBDIRECTORY( SimpleTests/MatrixTests )
ADD_SUBDIRECTORY( SimpleTests/MeshTests )
IF(NOT MSVC) IF(NOT MSVC)
ADD_SUBDIRECTORY( SimpleTests/SolverTests ) ADD_SUBDIRECTORY( SimpleTests/SolverTests )
ENDIF(NOT MSVC) ENDIF(NOT MSVC)
......
# Source files
GET_SOURCE_FILES(SOURCES_FILEIO)
SET ( SOURCES ${SOURCES_FILEIO})
# Create the library
ADD_LIBRARY(FileIO STATIC ${SOURCES})
include_directories(
.
../Base
../GeoLib
../MathLib
../MeshLib
)
target_link_libraries (FileIO
GeoLib
MeshLib
)
/**
* MeshIO.cpp
*
* Date: 2012/05/08
* Author: KR
*/
#include "GEOObjects.h"
#include "MeshIO.h"
#include "Node.h"
#include "Elements/Edge.h"
#include "Elements/Tri.h"
#include "Elements/Quad.h"
#include "Elements/Tet.h"
#include "Elements/Hex.h"
#include "Elements/Pyramid.h"
#include "Elements/Prism.h"
#include <iomanip>
#include <sstream>
namespace FileIO
{
MeshIO::MeshIO()
: _mesh(NULL)
{
}
MeshLib::Mesh* MeshIO::loadMeshFromFile(const std::string& file_name)
{
std::cout << "Read mesh ... " << std::endl;
/*
#ifndef NDEBUG
QTime myTimer;
myTimer.start();
#endif
*/
std::ifstream in (file_name.c_str(),std::ios::in);
if (!in.is_open())
{
std::cout << "CFEMesh::FEMRead() - Could not open file...\n";
return NULL;
}
std::string line_string ("");
getline(in, line_string);
std::vector<MeshLib::Node*> nodes;
std::vector<MeshLib::Element*> elements;
if(line_string.find("#FEM_MSH") != std::string::npos) // OGS mesh file
{
while (!in.eof())
{
getline(in, line_string);
// check keywords
if (line_string.find("#STOP") != std::string::npos)
break;
else if (line_string.find("$NODES") != std::string::npos)
{
double x, y, z, double_dummy;
unsigned nNodes, idx;
in >> nNodes >> std::ws;
std::string s;
std::ios::pos_type position = in.tellg();
for (unsigned i = 0; i < nNodes; i++)
{
in >> idx >> x >> y >> z;
MeshLib::Node* node(new MeshLib::Node(x, y, z, nodes.size()));
nodes.push_back(node);
position = in.tellg();
in >> s;
if (s.find("$AREA") != std::string::npos)
in >> double_dummy;
else
in.seekg(position, std::ios::beg);
in >> std::ws;
}
}
else if (line_string.find("$ELEMENTS") != std::string::npos)
{
unsigned nElements;
in >> nElements >> std::ws;
for (unsigned i = 0; i < nElements; i++)
{
getline(in, line_string);
elements.push_back(readElement(line_string, nodes));
}
}
}
MeshLib::Mesh* mesh (new MeshLib::Mesh(file_name, nodes, elements));
std::cout << "Nr. Nodes: " << nodes.size() << std::endl;
std::cout << "Nr. Elements: " << elements.size() << std::endl;
/*
#ifndef NDEBUG
std::cout << "Loading time: " << myTimer.elapsed() << " ms" << std::endl;
#endif
*/
return mesh;
}
else
{
in.close();
return NULL;
}
}
MeshLib::Element* MeshIO::readElement(const std::string& line, const std::vector<MeshLib::Node*> &nodes)
{
std::stringstream ss (line);
std::string elem_type_str;
unsigned index, patch_index;
ss >> index >> patch_index >> elem_type_str;
MshElemType::type elem_type (String2MshElemType(elem_type_str));
unsigned* idx = new unsigned[8];
MeshLib::Element* elem;
switch(elem_type)
{
case MshElemType::LINE:
for (int i = 0; i < 2; i++)
ss >> idx[i];
elem = new MeshLib::Edge(nodes[idx[0]], nodes[idx[1]], patch_index);
break;
case MshElemType::TRIANGLE:
for (int i = 0; i < 3; i++)
ss >> idx[i];
elem = new MeshLib::Tri(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], patch_index);
break;
case MshElemType::QUAD:
for (int i = 0; i < 4; i++)
ss >> idx[i];
elem = new MeshLib::Quad(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], nodes[idx[3]], patch_index);
break;
case MshElemType::TETRAHEDRON:
for (int i = 0; i < 4; i++)
ss >> idx[i];
elem = new MeshLib::Tet(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], nodes[idx[3]], patch_index);
break;
case MshElemType::HEXAHEDRON:
for (int i = 0; i < 8; i++)
ss >> idx[i];
elem = new MeshLib::Hex(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], nodes[idx[3]], nodes[idx[4]], nodes[idx[5]], nodes[idx[6]], nodes[idx[7]], patch_index);
break;
case MshElemType::PYRAMID:
for (int i = 0; i < 5; i++)
ss >> idx[i];
elem = new MeshLib::Pyramid(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], nodes[idx[3]], nodes[idx[4]], patch_index);
break;
case MshElemType::PRISM:
for (int i = 0; i < 6; i++)
ss >> idx[i];
elem = new MeshLib::Prism(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], nodes[idx[3]], nodes[idx[4]], nodes[idx[5]], patch_index);
break;
default:
elem = NULL;
}
/*
neighbors.resize(nfaces);
for (unsigned i = 0; i < nfaces; i++)
neighbors[i] = NULL;
*/
return elem;
}
int MeshIO::write(std::ostream &out)
{
/*
if(!_mesh)
{
std::cout << "OGSMeshIO cannot write: no mesh set!" << std::endl;
return 0;
}
setPrecision(9);
out << "#FEM_MSH" << std::endl;
out << "$PCS_TYPE" << std::endl << " " << _mesh->pcs_name << std::endl;
out << "$NODES" << std::endl << " ";
const size_t n_nodes(_mesh->GetNodesNumber(false));
out << n_nodes << std::endl;
for (size_t i(0); i < n_nodes; i++)
{
double const* const coords (_mesh->nod_vector[i]->getData());
out << i << " " << coords[0] << " " << coords[1] << " " << coords[2] << std::endl;
}
out << "$ELEMENTS" << std::endl << " ";
writeElementsExceptLines(_mesh->ele_vector, out);
out << " $LAYER" << std::endl;
out << " ";
out << _mesh->_n_msh_layer << std::endl;
out << "#STOP" << std::endl;
*/
return 1;
}
void MeshIO::setMesh(const MeshLib::Mesh* mesh)
{
_mesh = mesh;
}
} // end namespace FileIO
/**
* MeshIO.h
*
* Date: 2012/05/08
* Author: KR
*/
/**
* This is currently just test functionality for testing ogs-6 mesh data structures!
*/
#ifndef MESHIO_H_
#define MESHIO_H_
#include "Writer.h"
#include <sstream>
#include <iostream>
#include <vector>
namespace MeshLib
{
class Mesh;
class Node;
class Element;
}
namespace FileIO
{
class MeshIO : public Writer
{
public:
/// Constructor.
MeshIO();
virtual ~MeshIO() {};
/// Read mesh from file.
MeshLib::Mesh* loadMeshFromFile(const std::string& fileName);
/// Set mesh for writing.
void setMesh(const MeshLib::Mesh* mesh);
protected:
/// Write mesh to stream.
int write(std::ostream &out);
private:
MeshLib::Element* readElement(const std::string& line, const std::vector<MeshLib::Node*> &nodes);
const MeshLib::Mesh* _mesh;
}; /* class */
} /* namespace */
#endif /* MESHIO_H_ */
/**
* \file Writer.cpp
* 13/02/2012 LB Initial implementation
*
* Implementation of the Writer class
*/
// ** INCLUDES **
#include "Writer.h"
#include <fstream>
namespace FileIO
{
Writer::Writer()
{
}
std::string Writer::writeToString()
{
// Empty stream and clear error states.
_out.str("");
_out.clear();
if (this->write(_out))
return _out.str();
else
return std::string("");
}
int Writer::writeToFile(std::string const& filename)
{
std::string file_content = this->writeToString();
if (!file_content.empty())
{
std::ofstream fileStream;
fileStream.open (filename.c_str());
// check file stream
if (!fileStream)
{
std::cerr << "Could not open file " << filename << " !" << std::endl;
return 0;
}
fileStream << file_content;
fileStream.close();
return 1;
}
return 0;
}
void Writer::setPrecision(unsigned int precision)
{
_out.precision(precision);
}
void Writer::setFormat(std::ios_base::fmtflags flags)
{
_out.setf(flags);
}
} // namespace FileIO
/**
* \file Writer.h
* 13/02/2012 LB Initial implementation
*/
#ifndef WRITER_H
#define WRITER_H
#include <string>
#include <iostream>
#include <sstream>
namespace FileIO
{
/// @brief Base class which enables writing an object to string, stringstream
/// or file. Also formatting (precision, scientific notation of decimal values)
/// can be set.
///
/// When subclassing you only need to implement void write(std::ostream& stream).
class Writer
{
public:
Writer();
virtual ~Writer() {};
/// @brief Writes the object to a string.
std::string writeToString();
/// @brief Writes the object to the given file.
int writeToFile(std::string const& filename);
/// @brief Sets the decimal precision.
void setPrecision(unsigned int precision);
/// @brief Sets the format (either ios::scientific or ios::fixed);
void setFormat(std::ios_base::fmtflags flags);
protected:
/// @brief Writes the object to the given stream.
/// This method must be implemented by a subclass.
virtual int write(std::ostream& stream) = 0;
/// @brief The stream to write to.
std::stringstream _out;
private:
};
} // namespace FileIO
#endif // WRITER_H
...@@ -86,7 +86,7 @@ double getAngle (const double p0[3], const double p1[3], const double p2[3]) ...@@ -86,7 +86,7 @@ double getAngle (const double p0[3], const double p1[3], const double p2[3])
return acos (scpr (v0,v1,3) / (sqrt(scpr(v0,v0,3)) * sqrt(scpr (v1,v1,3)))); return acos (scpr (v0,v1,3) / (sqrt(scpr(v0,v0,3)) * sqrt(scpr (v1,v1,3))));
} }
double calcTriangleaArea(const double p0[3], const double p1[3], const double p2[3]) double calcTriangleArea(const double* p0, const double* p1, const double* p2)
{ {
const double u0 (p2[0] - p0[0]); const double u0 (p2[0] - p0[0]);
const double u1 (p2[1] - p0[1]); const double u1 (p2[1] - p0[1]);
......
...@@ -98,7 +98,7 @@ double getAngle (const double p0[3], const double p1[3], const double p2[3]); ...@@ -98,7 +98,7 @@ double getAngle (const double p0[3], const double p1[3], const double p2[3]);
* Calculates the area of a triangle. * Calculates the area of a triangle.
* The formula is A=.5*|u x v|, i.e. half of the area of the parallelogram specified by u=p0->p1 and v=p0->p2. * The formula is A=.5*|u x v|, i.e. half of the area of the parallelogram specified by u=p0->p1 and v=p0->p2.
*/ */
double calcTriangleArea(const double p0[3], const double p1[3], const double p2[3]); double calcTriangleArea(const double* p0, const double* p1, const double* p2);
/** /**
* Calculates the volume of a tetrahedron. * Calculates the volume of a tetrahedron.
......
...@@ -22,8 +22,8 @@ class Node; ...@@ -22,8 +22,8 @@ class Node;
class Element class Element
{ {
/* friend functions: */ /* friend functions: */
friend void Mesh::setElementInformationForNodes(); friend class Mesh;//void Mesh::setElementInformationForNodes();
friend void Mesh::addElement(Element*); //friend void Mesh::addElement(Element*);
public: public:
......
...@@ -24,11 +24,6 @@ FemMesh::FemMesh(const FemMesh &mesh) ...@@ -24,11 +24,6 @@ FemMesh::FemMesh(const FemMesh &mesh)
{ {
} }
FemMesh::FemMesh(const std::string &file_name)
: Mesh(file_name)
{
}
FemMesh::~FemMesh() FemMesh::~FemMesh()
{ {
......
...@@ -27,9 +27,6 @@ public: ...@@ -27,9 +27,6 @@ public:
/// Copy constructor /// Copy constructor
FemMesh(const FemMesh &mesh); FemMesh(const FemMesh &mesh);
/// Constructor for reading a mesh from a file
FemMesh(const std::string &file_name);
/// Destructor /// Destructor
virtual ~FemMesh(); virtual ~FemMesh();
......
...@@ -30,12 +30,6 @@ Mesh::Mesh(const Mesh &mesh) ...@@ -30,12 +30,6 @@ Mesh::Mesh(const Mesh &mesh)
{ {
} }
Mesh::Mesh(const std::string &file_name)
{
// read mesh
this->makeNodesUnique();
}
Mesh::~Mesh() Mesh::~Mesh()
{ {
const size_t nElements (_elements.size()); const size_t nElements (_elements.size());
......
...@@ -31,9 +31,6 @@ public: ...@@ -31,9 +31,6 @@ public:
/// Copy constructor /// Copy constructor
Mesh(const Mesh &mesh); Mesh(const Mesh &mesh);
/// Constructor for reading a mesh from a file
Mesh(const std::string &file_name);
/// Destructor /// Destructor
virtual ~Mesh(); virtual ~Mesh();
......
...@@ -25,8 +25,8 @@ class Element; ...@@ -25,8 +25,8 @@ class Element;
class Node : public GEOLIB::PointWithID class Node : public GEOLIB::PointWithID
{ {
/* friend functions: */ /* friend functions: */
friend void Mesh::setElementInformationForNodes(); friend class Mesh;//void Mesh::setElementInformationForNodes();
friend void Mesh::addElement(Element*); //friend void Mesh::addElement(Element*);
public: public:
......
INCLUDE_DIRECTORIES(
.
${CMAKE_SOURCE_DIR}/Base/
${CMAKE_SOURCE_DIR}/Base/logog/include
${CMAKE_SOURCE_DIR}/FileIO/
${CMAKE_SOURCE_DIR}/MathLib/
${CMAKE_SOURCE_DIR}/MeshLib/
)
# Create the executable
ADD_EXECUTABLE( MeshRead
MeshRead.cpp
${SOURCES}
${HEADERS}
)
TARGET_LINK_LIBRARIES ( MeshRead
MeshLib
FileIO
MathLib
Base
GeoLib
)
/*
* MeshRead.cpp
*
* Created on: 2012/05/09
* Author: KR
*/
#include "Mesh.h"
#include "MeshIO.h"
int main(int argc, char *argv[])
{
std::string file_name("c:/Project/Data/Ammer/Ammer-Homogen100m-Final.msh");
FileIO::MeshIO mesh_io;
MeshLib::Mesh* mesh = mesh_io.loadMeshFromFile(file_name);
delete mesh;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment