netgen/libsrc/interface/writeabaqus.cpp

244 lines
6.4 KiB
C++
Raw Normal View History

2009-01-13 04:40:13 +05:00
//
// Write Abaqus file
//
//
#include <mystdlib.h>
#include <myadt.hpp>
#include <linalg.hpp>
#include <csg.hpp>
#include <meshing.hpp>
2023-10-09 23:41:02 +05:00
#include "writeuser.hpp"
2009-01-13 04:40:13 +05:00
namespace netgen
{
using std::vector;
2009-01-13 04:40:13 +05:00
struct AbaqusElementType
{
const char * name;
const vector<int> permutation;
2009-01-13 04:40:13 +05:00
AbaqusElementType(const char * name, const vector<int> & permutation)
: name(name), permutation(permutation)
{}
};
2009-01-13 04:40:13 +05:00
static inline const AbaqusElementType & GetAbaqusType(int dim, int num_nodes)
2009-01-13 04:40:13 +05:00
{
// maps num_nodes to AbaqusElementType for each dimension
typedef std::map<int, AbaqusElementType> AbaqusElementTypes;
static const std::map<int, AbaqusElementType> abaqus_eltypes[3] =
{
// 1D
AbaqusElementTypes{
{2, AbaqusElementType{"T2D2", vector{0,1}}},
},
// 2D
AbaqusElementTypes{
{3, AbaqusElementType{"CPS3", vector{0,1,2}}},
},
// 3D
AbaqusElementTypes{
{4, AbaqusElementType{"C3D4", vector{0,1,3,2}}},
{10, AbaqusElementType{"C3D10", vector{0,1,3,2,4,8,6,5,7,9}}},
}
};
2009-01-13 04:40:13 +05:00
const auto & eltypes = abaqus_eltypes[dim-1];
if (eltypes.count(num_nodes) > 0)
return eltypes.at(num_nodes);
else
throw Exception("unsupported " + ToString(dim)+"d Element type with " + ToString(num_nodes) + " nodes");
}
2009-01-13 04:40:13 +05:00
static void WritePoints ( const Mesh & mesh, ostream & out )
{
out << "*Node" << endl;
for(auto pi : mesh.Points().Range() )
{
out << pi+1-PointIndex::BASE << ", ";
auto p = mesh[pi];
out << p[0] << ", " << p[1] << ", " << p[2] << '\n';
}
}
2009-01-13 04:40:13 +05:00
template <typename ElIndex>
static void WriteElement(ostream & out, const Mesh& mesh, ElIndex ei, const vector<int> & permutation, int & el_counter)
{
el_counter++;
auto el = mesh[ei];
out << el_counter;
for(auto i : Range(el.PNums()))
out << ", " << el[permutation[i]]+1-PointIndex::BASE;
out << '\n';
}
2009-01-13 04:40:13 +05:00
template <typename ElIndex, typename Elements>
static void WriteElements ( ostream & out, const Mesh & mesh, int dim, const Elements & el_range, int & el_counter)
{
// map index, num_nodes to elements
std::map<std::tuple<int, int>, Array<ElIndex>> elset_map;
2009-01-13 04:40:13 +05:00
for(auto ei : el_range)
{
const auto & el = mesh[ei];
int index = 0;
if constexpr(std::is_same_v<ElIndex,SegmentIndex>)
index = el.edgenr;
else
index = el.GetIndex();
elset_map[{index, el.GetNP()}].Append(ei);
}
2009-01-13 04:40:13 +05:00
for(auto & [key, elems] : elset_map)
2009-01-13 04:40:13 +05:00
{
auto [index, num_nodes] = key;
auto name = mesh.GetRegionName(elems[0]);
if (name == "") name = "default";
PrintMessage (5, index, ": ", name);
const auto & eltype = GetAbaqusType(dim, num_nodes) ;
out << "*Element, type=" << eltype.name << ", ELSET=" << name << endl;
for(auto ei : elems)
WriteElement(out, mesh, ei, eltype.permutation, el_counter);
2009-01-13 04:40:13 +05:00
}
}
void WriteAbaqusFormat (const Mesh & mesh,
const filesystem::path & filename)
2009-01-13 04:40:13 +05:00
{
PrintMessage (1, "Write Abaqus Mesh");
2009-01-13 04:40:13 +05:00
ofstream outfile (filename);
2009-01-13 04:40:13 +05:00
outfile << "*Heading" << endl;
outfile << " " << filename << endl;
outfile.precision(8);
int element_counter = 0;
WritePoints(mesh, outfile);
if(mesh.GetDimension() < 3)
WriteElements<SegmentIndex>(outfile, mesh, 1, mesh.LineSegments().Range(), element_counter);
WriteElements<SurfaceElementIndex>(outfile, mesh, 2, mesh.SurfaceElements().Range(), element_counter);
WriteElements<ElementIndex>(outfile, mesh, 3, mesh.VolumeElements().Range(), element_counter);
2009-01-13 04:40:13 +05:00
// Write identifications (untested!)
2009-01-13 04:40:13 +05:00
if (mesh.GetIdentifications().GetMaxNr())
{
const auto np = mesh.GetNP();
2009-01-13 04:40:13 +05:00
// periodic identification, implementation for
// Helmut J. Boehm, TU Vienna
2022-02-17 20:52:07 +05:00
auto mpcfilename = filename;
if (filename.extension() == ".inp")
mpcfilename.replace_extension(".mpc");
2009-01-13 04:40:13 +05:00
else
2022-02-17 20:52:07 +05:00
mpcfilename.concat(".mpc");
2009-01-13 04:40:13 +05:00
ofstream mpc (mpcfilename);
int masternode(0);
2019-07-09 13:39:16 +05:00
NgArray<INDEX_2> pairs;
2019-08-28 17:00:49 +05:00
NgBitArray master(np), help(np);
2009-01-13 04:40:13 +05:00
master.Set();
for (int i = 1; i <= 3; i++)
2009-01-13 04:40:13 +05:00
{
mesh.GetIdentifications().GetPairs (i, pairs);
help.Clear();
for (int j = 1; j <= pairs.Size(); j++)
2009-01-13 04:40:13 +05:00
{
help.Set (pairs.Get(j).I1());
}
master.And (help);
}
for (int i = 1; i <= np; i++)
2009-01-13 04:40:13 +05:00
if (master.Test(i))
masternode = i;
cout << "masternode = " << masternode << " = "
<< mesh.Point(masternode) << endl;
2020-06-17 22:11:17 +05:00
NgArray<int> minions(3);
for (int i = 1; i <= 3; i++)
2009-01-13 04:40:13 +05:00
{
mesh.GetIdentifications().GetPairs (i, pairs);
for (int j = 1; j <= pairs.Size(); j++)
2009-01-13 04:40:13 +05:00
{
if (pairs.Get(j).I1() == masternode)
2020-06-17 22:11:17 +05:00
minions.Elem(i) = pairs.Get(j).I2();
2009-01-13 04:40:13 +05:00
}
2020-06-17 22:11:17 +05:00
cout << "minion(" << i << ") = " << minions.Get(i)
<< " = " << mesh.Point(minions.Get(i)) << endl;
2009-01-13 04:40:13 +05:00
}
outfile << "**\n"
<< "*NSET,NSET=CTENODS\n"
2020-06-17 22:11:17 +05:00
<< minions.Get(1) << ", "
<< minions.Get(2) << ", "
<< minions.Get(3) << endl;
2009-01-13 04:40:13 +05:00
outfile << "**\n"
<< "**POINT_fixed\n"
<< "**\n"
<< "*BOUNDARY, OP=NEW\n";
for (int j = 1; j <= 3; j++)
2009-01-13 04:40:13 +05:00
outfile << masternode << ", " << j << ",, 0.\n";
outfile << "**\n"
<< "*BOUNDARY, OP=NEW\n";
for (int j = 1; j <= 3; j++)
2009-01-13 04:40:13 +05:00
{
2020-06-17 22:11:17 +05:00
Vec3d v(mesh.Point(masternode), mesh.Point(minions.Get(j)));
2009-01-13 04:40:13 +05:00
double vlen = v.Length();
int dir = 0;
if (fabs (v.X()) > 0.9 * vlen) dir = 2;
if (fabs (v.Y()) > 0.9 * vlen) dir = 3;
if (fabs (v.Z()) > 0.9 * vlen) dir = 1;
if (!dir)
cout << "ERROR: Problem with rigid body constraints" << endl;
2020-06-17 22:11:17 +05:00
outfile << minions.Get(j) << ", " << dir << ",, 0.\n";
2009-01-13 04:40:13 +05:00
}
outfile << "**\n"
<< "*EQUATION, INPUT=" << mpcfilename << endl;
2019-08-28 17:00:49 +05:00
NgBitArray eliminated(np);
2009-01-13 04:40:13 +05:00
eliminated.Clear();
for (int i = 1; i <= mesh.GetIdentifications().GetMaxNr(); i++)
2009-01-13 04:40:13 +05:00
{
mesh.GetIdentifications().GetPairs (i, pairs);
if (!pairs.Size())
continue;
for (int j = 1; j <= pairs.Size(); j++)
2009-01-13 04:40:13 +05:00
if (pairs.Get(j).I1() != masternode &&
!eliminated.Test(pairs.Get(j).I2()))
{
eliminated.Set (pairs.Get(j).I2());
for (int k = 1; k <= 3; k++)
2009-01-13 04:40:13 +05:00
{
mpc << "4" << "\n";
mpc << pairs.Get(j).I2() << "," << k << ", -1.0, ";
mpc << pairs.Get(j).I1() << "," << k << ", 1.0, ";
2020-06-17 22:11:17 +05:00
mpc << minions.Get(i) << "," << k << ", 1.0, ";
2009-01-13 04:40:13 +05:00
mpc << masternode << "," << k << ", -1.0 \n";
}
}
}
}
PrintMessage(1, "done");
2009-01-13 04:40:13 +05:00
}
2023-10-09 23:41:02 +05:00
static RegisterUserFormat reg_abaqus ("Abaqus Format", {".mesh"}, nullopt, WriteAbaqusFormat);
2009-01-13 04:40:13 +05:00
}