Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ModelPart #84

Merged
merged 37 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0500725
initial version of CoSimIO::ModelPart
philbucher Jul 17, 2020
76eb3fd
Merge branch 'master' into model-part
philbucher Aug 4, 2020
04b9568
adding empty test file
philbucher Aug 4, 2020
6afe340
Merge branch 'master' into model-part
philbucher Aug 6, 2020
7cc8d33
Improvements
philbucher Aug 6, 2020
502086d
minor
philbucher Aug 25, 2020
4e4e819
only allowing const version
philbucher Aug 25, 2020
4a51eef
commenting suite
philbucher Aug 25, 2020
27ae3b2
Merge remote-tracking branch 'origin/master' into model-part
philbucher Aug 27, 2020
d2ddaf1
using IdType
philbucher Aug 27, 2020
b1e0ee0
missing
philbucher Aug 27, 2020
39abf73
storing nodes instead of connectivities
philbucher Aug 27, 2020
1bf6175
fixes
philbucher Aug 27, 2020
978c336
finished node
philbucher Aug 27, 2020
dbbdf94
finished Element
philbucher Aug 27, 2020
ba739a4
more advances
philbucher Aug 27, 2020
779172b
replaced container access
philbucher Aug 27, 2020
eb50f89
added test for Node
philbucher Aug 27, 2020
7b22f3f
adding test for Element
philbucher Aug 27, 2020
6701609
fix
philbucher Aug 27, 2020
1543868
added another test
philbucher Aug 27, 2020
a846ec7
explicitly using const iterator
philbucher Aug 27, 2020
1f3a66a
delegating ctor
philbucher Aug 27, 2020
0c01881
checks and corresponding tests
philbucher Aug 27, 2020
e5f38fc
minor fix
philbucher Aug 27, 2020
014ab56
adding Name to ModelPart
philbucher Aug 27, 2020
e8c4b17
add test for ModelPart basics
philbucher Aug 27, 2020
71a2069
missing paranthesis in Macro
philbucher Aug 27, 2020
eae0a91
testing invalid names in Modelpart
philbucher Aug 27, 2020
a9aec96
more tests
philbucher Aug 28, 2020
782cac2
missing implementation
philbucher Aug 28, 2020
16187d5
update after discussing with Pooyan
philbucher Aug 28, 2020
1c281f8
updated pointer types
philbucher Aug 31, 2020
dda9ba9
intrnal refactor
philbucher Aug 31, 2020
dcc34be
fix
philbucher Aug 31, 2020
4ff7286
missing tests I
philbucher Aug 31, 2020
affe7c8
missing tests II
philbucher Aug 31, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion co_sim_io/impl/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ the code where the CoSimIO is included
#endif

#ifndef CO_SIM_IO_ERROR_IF_NOT
#define CO_SIM_IO_ERROR_IF_NOT(conditional) if (!conditional) CO_SIM_IO_ERROR
#define CO_SIM_IO_ERROR_IF_NOT(conditional) if (!(conditional)) CO_SIM_IO_ERROR
#endif

#ifndef CO_SIM_IO_INFO
Expand Down
240 changes: 240 additions & 0 deletions co_sim_io/impl/model_part.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
// ______ _____ _ ________
// / ____/___ / ___/(_)___ ___ / _/ __ |
// / / / __ \\__ \/ / __ `__ \ / // / / /
// / /___/ /_/ /__/ / / / / / / // // /_/ /
// \____/\____/____/_/_/ /_/ /_/___/\____/
// Kratos CoSimulationApplication
//
// License: BSD License, see license.txt
//
// Main authors: Philipp Bucher (https://github.com/philbucher)
//

#ifndef CO_SIM_IO_MODEL_PART_H_INCLUDED
#define CO_SIM_IO_MODEL_PART_H_INCLUDED

/* This file contains the implementation of th CoSimIO::ModelPart
It serves as a data container when exchanging data
Also it is used in order to be consistent with Kratos to reduce compatibility problems
This is a simplified version of Kratos::ModelPart
see https://github.com/KratosMultiphysics/Kratos/blob/master/kratos/includes/model_part.h
*/

// System includes
#include <memory>
#include <vector>
#include <string>
#include <functional>
#include <algorithm>

// Project includes
#include "define.hpp"
#include "macros.hpp"

namespace CoSimIO {
class Node
{
public:
Node(
const IdType I_Id,
const double I_X,
const double I_Y,
const double I_Z)
: mId(I_Id),
mX(I_X),
mY(I_Y),
mZ(I_Z)
{
CO_SIM_IO_ERROR_IF(I_Id < 1) << "Id must be >= 1!" << std::endl;
}

Node(
const IdType I_Id,
const CoordinatesType& I_Coordinates)
: Node(I_Id, I_Coordinates[0], I_Coordinates[1], I_Coordinates[2])
{ }

// delete copy and assignment CTor
Node(const Node&) = delete;
Node& operator=(Node const&) = delete;

IdType Id() const { return mId; }
double X() const { return mX; }
double Y() const { return mY; }
double Z() const { return mZ; }
CoordinatesType Coordinates() const { return {mX, mY, mZ}; }

private:
IdType mId;
double mX;
double mY;
double mZ;
};

class Element
{
public:
using ElementType = std::size_t;
using NodesContainerType = std::vector<Node*>;
using ConnectivitiesType = std::vector<IdType>;

Element(
const IdType I_Id,
const ElementType I_Type,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is here

const NodesContainerType& I_Nodes)
: mId(I_Id),
mType(I_Type),
mNodes(I_Nodes)
{
CO_SIM_IO_ERROR_IF(I_Id < 1) << "Id must be >= 1!" << std::endl;
CO_SIM_IO_ERROR_IF(NumberOfNodes() < 1) << "No nodes were passed!" << std::endl;
}

// delete copy and assignment CTor
Element(const Element&) = delete;
Element& operator=(Element const&) = delete;

IdType Id() const { return mId; }
ElementType Type() const { return mType; }
std::size_t NumberOfNodes() const { return mNodes.size(); }
NodesContainerType::const_iterator NodesBegin() const { return mNodes.begin(); }
NodesContainerType::const_iterator NodesEnd() const { return mNodes.end(); }

private:
IdType mId;
ElementType mType;
NodesContainerType mNodes;
};

class ModelPart
{
public:

using NodePointerType = std::shared_ptr<Node>; // TODO switch to intrusive_ptr
using ElementPointerType = std::shared_ptr<Element>; // TODO switch to intrusive_ptr
using NodesContainerType = std::vector<NodePointerType>;
using ElementsContainerType = std::vector<ElementPointerType>;

explicit ModelPart(const std::string& I_Name) : mName(I_Name)
{
CO_SIM_IO_ERROR_IF(I_Name.empty()) << "Please don't use empty names (\"\") when creating a ModelPart" << std::endl;
CO_SIM_IO_ERROR_IF_NOT(I_Name.find(".") == std::string::npos) << "Please don't use names containing (\".\") when creating a ModelPart (used in \"" << I_Name << "\")" << std::endl;
}

// delete copy and assignment CTor
ModelPart(const ModelPart&) = delete;
ModelPart& operator=(ModelPart const&) = delete;

std::string Name() const { return mName; }
std::size_t NumberOfNodes() const { return mNodes.size(); }
std::size_t NumberOfElements() const { return mElements.size(); }

Node& CreateNewNode(
const IdType I_Id,
const double I_X,
const double I_Y,
const double I_Z)
{
CO_SIM_IO_ERROR_IF(HasNode(I_Id)) << "The Node with Id " << I_Id << " exists already!" << std::endl;

mNodes.push_back(std::make_shared<Node>(I_Id, I_X, I_Y, I_Z));
return *(mNodes.back());
}

Element& CreateNewElement(
const IdType I_Id,
const Element::ElementType I_Type,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

const Element::ConnectivitiesType& I_Connectivities)
{
CO_SIM_IO_ERROR_IF(HasElement(I_Id)) << "The Element with Id " << I_Id << " exists already!" << std::endl;

Element::NodesContainerType nodes;
nodes.reserve(I_Connectivities.size());
for (const IdType node_id : I_Connectivities) {
nodes.push_back(&GetNode(node_id));
}
mElements.push_back(std::make_shared<Element>(I_Id, I_Type, nodes));
return *(mElements.back());
}

NodesContainerType::const_iterator NodesBegin() const { return mNodes.begin(); }
ElementsContainerType::const_iterator ElementsBegin() const { return mElements.begin(); }

NodesContainerType::const_iterator NodesEnd() const { return mNodes.end(); }
ElementsContainerType::const_iterator ElementsEnd() const { return mElements.end(); }

Node& GetNode(const IdType I_Id)
{
auto it_node = FindNode(I_Id);
CO_SIM_IO_ERROR_IF(it_node == mNodes.end()) << "Node with Id " << I_Id << " does not exist!" << std::endl;
return **it_node;
}

const Node& GetNode(const IdType I_Id) const
{
auto it_node = FindNode(I_Id);
CO_SIM_IO_ERROR_IF(it_node == mNodes.end()) << "Node with Id " << I_Id << " does not exist!" << std::endl;
return **it_node;
}

Element& GetElement(const IdType I_Id)
{
auto it_elem = FindElement(I_Id);
CO_SIM_IO_ERROR_IF(it_elem == mElements.end()) << "Element with Id " << I_Id << " does not exist!" << std::endl;
return **it_elem;
}

const Element& GetElement(const IdType I_Id) const
{
auto it_elem = FindElement(I_Id);
CO_SIM_IO_ERROR_IF(it_elem == mElements.end()) << "Element with Id " << I_Id << " does not exist!" << std::endl;
return **it_elem;
}

private:
std::string mName;
NodesContainerType mNodes;
ElementsContainerType mElements;

NodesContainerType::const_iterator FindNode(const IdType I_Id) const
{
return std::find_if(
mNodes.begin(), mNodes.end(),
[I_Id](const NodePointerType& rp_node) { return rp_node->Id() == I_Id;});
}

NodesContainerType::iterator FindNode(const IdType I_Id)
{
return std::find_if(
mNodes.begin(), mNodes.end(),
[I_Id](const NodePointerType& rp_node) { return rp_node->Id() == I_Id;});
}

ElementsContainerType::const_iterator FindElement(const IdType I_Id) const
{
return std::find_if(
mElements.begin(), mElements.end(),
[I_Id](const ElementPointerType& rp_elem) { return rp_elem->Id() == I_Id;});
}

ElementsContainerType::iterator FindElement(const IdType I_Id)
{
return std::find_if(
mElements.begin(), mElements.end(),
[I_Id](const ElementPointerType& rp_elem) { return rp_elem->Id() == I_Id;});
}

bool HasNode(const IdType I_Id) const
{
return FindNode(I_Id) != mNodes.end();
}

bool HasElement(const IdType I_Id) const
{
return FindElement(I_Id) != mElements.end();
}
};

} //namespace CoSimIO

#endif // CO_SIM_IO_MODEL_PART_H_INCLUDED
Loading