KASKADE 7 development version
Loading...
Searching...
No Matches
marcwriter.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2020-2021 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef MARCWRITER_HH
14#define MARCWRITER_HH
15
16#include "io/iobase.hh"
18
19#include "dune/geometry/type.hh"
20#include "dune/grid/common/entity.hh"
21
22#include <algorithm>
23#include <cassert>
24#include <cstdio>
25#include <fstream>
26#include <iomanip>
27#include <iterator>
28#include <map>
29#include <set>
30
31namespace Kaskade
32{
33
35 namespace MarcWriterDetail
36 {
37
38 int marcElementIdFromGeometryType(Dune::GeometryType gt)
39 {
40 // These are listed in Marc Volume B as "heat conduction elements"
41 if (gt.isTriangle()) return 37;
42 if (gt.isTetrahedron()) return 135;
43 throw LookupException("Unhandled cell type encountered in Marc output.",__FILE__,__LINE__);
44 return 0;
45 }
46
47 // MARC wants floating point numbers as -1.234+5, i.e. without the usual 'e' denoting the
48 // exponent.
49 std::string marcFloatNumber(double x)
50 {
51 int const n = 50;
52 char buf[n];
53 std::snprintf(buf,n,"%E",x);
54// std::remove(buf,buf+n,'e');
55 return std::string(buf);
56 }
57 }
59
77 template <class Material>
78 void writeMarc(Material const& material, std::string const& filename,
79 std::string const& boundaryFile="",
80 double const scale=1.0, IoOptions options=IoOptions())
81 {
82 using namespace MarcWriterDetail;
83
84 // open file
85 std::ofstream out(filename+".marc");
86 if (!out)
87 throw FileIOException("File " + filename + ".marc could not be opened for writing Marc output.",
88 filename+".marc",__FILE__,__LINE__);
89
90
91 using GridView = typename Material::GridView;
92 auto const& gridView = material.space().gridView();
93 auto const& is = gridView.indexSet();
94 constexpr int dim = GridView::dimension;
95 constexpr int dimworld = GridView::dimensionworld;
96
97 int const nCells = gridView.size(0);
98 int const nVertices = gridView.size(dim);
99
100 // Make sure the material function is piecewise constant and scalar
101 assert(material.space().mapper().maxOrder()==0);
102 static_assert(Material::components==1);
103 assert(material.dim()==nCells);
104
105
106 // Get the geometry (of first cell, which must coincide with all other cells)
107 Dune::GeometryType gt = gridView.template begin<0>()->geometry().type();
108 if (gridView.size(gt) != nCells)
109 throw GridException("Marc output implemented for grids with a single cell geometry type only.",
110 __FILE__,__LINE__);
111
112 int elementId = marcElementIdFromGeometryType(gt);
113
114 // Marc requires floating point data to be formatted as float.
115 out << std::scientific << std::setprecision(10);
116
117 // Marc input is organized in three groups: parameter data, model definition data,
118 // and history definition data. First write parameter data.
119
120 out << "TITLE " << "Kaskade 7 output file " << filename << "\n";
121 out << "EXTENDED\n"; // necessary for long float mantissa
122 out << "ELEMENTS," << elementId << ",\n"; // number of grid cells
123 out << "SIZING\n";
124 out << "VERSION, 15, 1, 0, 1\n"; // necessary for correct interpretation by MARC
125 out << "HEAT, 0, 0, 1, 0, 1, 1\n"; // necessary for interpretation as heat problem (not mechanics)
126 out << "END\n";
127
128
129 // Next gather cells by their material id. For each cell, an index vector consisting of
130 // the cell index (1-based) and all vertex indices (1-based) is stored.
131 std::map<int,std::vector<std::vector<int>>> id;
132 for (auto const& cell: Dune::elements(gridView))
133 {
134 int cellIdx = is.index(cell);
135 std::vector<int> c;
136 c.push_back(cellIdx+1);
137 for (int i=0; i<cell.subEntities(dim); ++i)
138 c.push_back(is.index(cell.template subEntity<dim>(i))+1);
139
140 int mid = static_cast<int>(std::round(material.coefficients()[cellIdx])); // material id
141 id[mid].push_back(c);
142 }
143
144
145 // Next the model definition data. First the grid topology, with one "CONNECTIVITY" section
146 // per material id. Marc appears not to accept arbitrary material ids, only contiguous numbers
147 // starting at 1. We therefore skip the material id and count the number up.
148 int materialNumber = 1;
149 for (auto const& [_,cs]: id)
150 {
151 out << "CONNECTIVITY\n";
152 out << "0, 0, 1, 0, 0, " << materialNumber << ", \n"; // this encodes the material id of subsequent cells
153 for (auto const& c: cs)
154 {
155 out << c[0] << ", " << elementId;
156 for (int i=1; i<c.size(); ++i)
157 out << ", " << c[i];
158 out << "\n";
159 }
160 ++materialNumber;
161 }
162
163 // Write out vertex coordinates (starting at 1, not 0)
164 out << "COORDINATES\n";
165 out << dimworld << ", " << nVertices << "\n";
166 for (auto const& v: Dune::entities(gridView,Dune::Codim<dim>()))
167 {
168 out << is.index(v)+1;
169 auto x = v.geometry().center();
170 for (int i=0; i<dimworld; ++i)
171 out << ", " << marcFloatNumber(scale*x[i]);
172 out << "\n";
173 }
174
175 // Then write out dummy (thermal) material properties for each material.
176 // The odd blank lines in the output appear to be necessary for MARC not to choke.
177 for (int i=1; i<=size(id); ++i)
178 out << "ISOTROPIC\n\n"
179 << i << ", 0, 0, 0, 0, mati" << i << "\n"
180 << "8.00000-2, 1.00000+3, 4.70000+2, 0.00000+0, 9.00000-1, 0.00000+0, 0.00000+0\n\n";
181
182
183 out << "END OPTION\n";
184
185 // Check if we shall write out a file with boundary node and face information.
186 // If not, we're done.
187 if (boundaryFile.empty())
188 return;
189
190 // Now write all boundary nodes and faces (in terms of their vertex ids).
191 // We implicitly assume faces have dim vertices, i.e. we assume simplicial grids.
192 std::set<int> boundaryNodes;
193 std::vector<std::array<int,dim>> boundaryFaces;
194 std::vector<int> boundaryCells;
195 for (auto const& cell: Dune::elements(gridView))
196 {
197 int const cellIndex = is.index(cell);
198 auto refElem = Dune::referenceElement(cell);
199
200 for (auto const& face: Dune::intersections(gridView,cell))
201 if (face.boundary() && !face.neighbor())
202 {
203 // If cell has a boundary face, store its number. Remember MARC starts indexing
204 // at 1.
205 if (boundaryCells.empty() || boundaryCells.back()!=cellIndex+1)
206 boundaryCells.push_back(cellIndex+1);
207
208 int const faceSubIndex = face.indexInInside();
209
210 // Now get the dim indices of vertices incident to the face. First we obtain the
211 // cell-local indices and then convert them to global ones. Remember MARC starts
212 // indices at 1.
213 std::array<int,dim> bf;
214 int pos = 0;
215 for (int i: refElem.subEntities(faceSubIndex,1,dim))
216 bf[pos++] = is.subIndex(cell,i,dim) + 1; // 1-based indexing
217 assert(pos==dim);
218
219 // Store the face and its vertices.
220 boundaryFaces.push_back(bf);
221 for (int i: bf)
222 boundaryNodes.insert(i);
223 }
224 }
225
226 // Write data to boundary file.
227 std::ofstream bout(boundaryFile+".bdry");
228 if (!bout)
229 throw FileIOException("File " + boundaryFile + ".bdry could not be opened for writing Marc output.",
230 boundaryFile+".bdry",__FILE__,__LINE__);
231 bout << "# " << boundaryNodes.size() << " boundary nodes\n";
232 std::copy(boundaryNodes.begin(),boundaryNodes.end(),std::ostream_iterator<int>(bout,"\n"));
233
234 bout << "\n# " << boundaryCells.size() << " boundary cells\n";
235 std::copy(boundaryCells.begin(),boundaryCells.end(),std::ostream_iterator<int>(bout,"\n"));
236
237 bout << "\n# " << boundaryFaces.size() << " boundary faces\n";
238 for (auto const& bf: boundaryFaces)
239 {
240 std::copy(bf.begin(),bf.end(),std::ostream_iterator<int>(bout," "));
241 bout << "\n";
242 }
243 }
244
245}
246
247#endif
An exception class for file IO errors.
An exception class for grid related errors.
constexpr StaticIndexRange< 0, std::numeric_limits< int >::max()> _
Convenience static range denoting the full available range, analogous to Matlab's :.
Definition tensor.hh:74
Output of mesh and solution for visualization software.
void writeMarc(Material const &material, std::string const &filename, std::string const &boundaryFile="", double const scale=1.0, IoOptions options=IoOptions())
Writes grid and material IDs given as FE function to a Marc input file.
Definition marcwriter.hh:78
options for VTK/AMIRA output
Definition iobase.hh:77