KASKADE 7 development version
Loading...
Searching...
No Matches
amira.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) 2002-2025 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 AMIRA_HH
14#define AMIRA_HH
15
16#if HAVE_AMIRA == 0
17#error "libamiramesh is required for Amira file IO, but HAVE_AMIRA is set to 0"
18#endif
19
20#include <string>
21#include <iostream>
22#include <fstream>
23
24
25#undef max
26#include "io/iobase.hh"
27
28#include <boost/fusion/include/for_each.hpp>
29#include <boost/fusion/include/zip_view.hpp>
30#include <boost/fusion/include/vector.hpp>
31
32#include "utilities/enums.hh"
33#include "fem/assemble.hh"
34#include "fem/spaces.hh"
35
36#include "io/vtk.hh"
37#include "io/matlab.hh"
38#include "io/amirameshreader.hh"
39#include "io/amiramesh.hh"
41
42
51
52//---------------------------------------------------------------------
53
54namespace Kaskade
55{
56 template<class GridType>
58 : public Dune::AmiraMeshWriter<typename GridType::LeafGridView>
59 {
60
61 public:
62 typedef typename GridType::LeafGridView GridView;
64
67
69 MaterialAmiraMeshWriter(const GridType& grid) {
70 this->addGrid(grid.leafGridView());
71 }
72
80 void addMaterialData(const GridView& gridView, DataContainer& materialData) {
81
82 const typename GridView::IndexSet& indexSet = gridView.indexSet();
83 int noOfElements = indexSet.size(0);
84
85 AmiraMesh::Data* data = Dune::AmiraMeshWriter<GridView>::amiramesh_.findData("Tetrahedra",HxDOUBLE,1,"Materials");
86
87 for(int i=0; i<noOfElements; i++)
88 ((unsigned char*)data->dataPtr())[i] = materialData[i];
89 }
90
91
92 };
93
94
98 namespace IoDetail {
99
100 template <class GridView, class Filter, class RAIter>
101 struct AddDataTo<GridView,Kaskade::MaterialAmiraMeshWriter<typename GridView::Grid>,Filter,RAIter>
102 {
103 typedef typename GridView::Grid Grid;
105
106 AddDataTo(GridView const& gridView_, AmiraWriter& writer_, Filter const& filter_, RAIter const& names_):
107 gridView(gridView_),
108 writer(writer_),
109 filter(filter_),
110 names(names_)
111 {}
112
113
114 template <class Pair>
115 void operator()(Pair const& pair) const
116 {
117 using namespace boost::fusion;
118
119 int const id = boost::remove_reference<typename result_of::value_at_c<Pair,0>::type>::type::id;
120
121 if (!filter(names[id]))
122 return;
123
124 typedef typename boost::remove_reference<typename result_of::value_at_c<Pair,1>::type>::type Function;
125
126 Function const& f = at_c<1>(pair);
127 // std::string const& name = names[id];
128
129 // Get the index set. We hope this is actually the leaf index set
130 // (probably yes, but not guaranteed). Unfortunately there seems
131 // to be no possibility to check index sets for equality.
132 typedef typename Function::Space::IndexSet IndexSet;
133 IndexSet const& indexSet = f.space().indexSet();
134
135
136 // Create evaluator. Todo: Create and use a vertex-based
137 // quadrature rule, create and use a shape function cache based on
138 // this quadrature rule.
139 typename Function::Space::Evaluator eval(f.space());
140
141
142 if (Function::Space::continuity >= 0)
143 {
144 // Continuous function. Written as values at grid vertices.
145 Dune::BlockVector<typename Function::ValueType> data(indexSet.size(GridView::dimension));
146
147 // Step through all cells.
148 for (auto const& cell: Dune::elements(f.space().gridView()))
149 {
150 // Obtain reference element. This creates a GCC warning about dangling reference,
151 // but as this is a reference to a static variable in a function (a singleton),
152 // this should be ok.
153 auto const& refElem = Dune::ReferenceElements<typename Grid::ctype, GridView::dimension>::general(cell.type());
154 // Move evaluator to current cell.
155 eval.moveTo(cell);
156
157 // Step through all vertices of the current cell.
158 for (int i=0; i<cell.subEntities(GridView::dimension); ++i)
159 {
160 // Move evaluator to current vertex.
161 eval.evaluateAt(refElem.position(i,GridView::dimension));
162 // Store value of function in data vector (inefficient, occurs multiple times...).
163 data[indexSet.subIndex(cell,i,GridView::dimension)] = f.value(eval);
164 }
165 }
166
167 writer.addVertexData(data,gridView);
168 }
169 else
170 {
171 // Discontinuous function. Written as values at cells.
172 Dune::BlockVector<typename Function::ValueType> data(indexSet.size(0));
173
174 // Step through all cells.
175 for (auto const& cell: Dune::elements(f.space().grid().leafGridView()))
176 {
177 // Obtain reference element.
178 auto const& refElem = Dune::ReferenceElements<typename Grid::ctype, GridView::dimension>::general(cell.type());
179
180 data[indexSet.index(cell)] = f.value(cell,refElem.position(0,0));
181 }
182
183 writer.addCellData(data,gridView);
184 }
185 }
186
187 private:
188 GridView const& gridView;
189 AmiraWriter& writer;
190 Filter const& filter;
191 RAIter names;
192
193 };
194
195 } // End of namespace IoDetail
196
200
201 //---------------------------------------------------------------------
202
203
213 template <class GridView>
214 void copyHeader(GridView const& gridView, std::string const& file_in, std::string const& file_out){
215
216 int nTet = gridView.indexSet().size(0);
217 int nNodes = gridView.indexSet().size(int(GridView::dimensionworld));
218
219 std::string find1("nNodes ");
220 std::string find2("nTetrahedra ");
221 std::ofstream tmpFile("tmp.txt");
222 std::ifstream inFile(file_in);
223 std::ifstream outFile(file_out);
224 std::string line;
225 bool start = false;
226
227 if (!file_in.empty())
228 { //get amira-file header from input file_in
229 while( getline(inFile, line) )
230 {
231 if( line.substr(0,7).compare("Nodes {") == 0 )
232 break;
233 else if( line.substr(0,7).compare(find1) == 0)
234 tmpFile << find1 << nNodes << '\n';
235 else if( line.substr(0,12).compare(find2) == 0)
236 tmpFile << find2 << nTet << '\n';
237 else
238 tmpFile << line << '\n';
239 }
240 }
241 else
242 start = true;
243
244 inFile.close();
245
246 //copying data section of file_out
247 while( getline(outFile,line) )
248 {
249 if (line.substr(0,7).compare("Nodes {") == 0)
250 start = true; //Start of the data section
251
252 if (start)
253 tmpFile << line << '\n';
254 }
255 outFile.close();
256 tmpFile.close();
257
258 //rename
259 if (std::rename("tmp.txt" , file_out.c_str()) == 0)
260 ;
261 else
262 perror( "Error renaming file in call of copyHeader() in writeAMIRAFile" );
263
264 std::remove("tmp.txt");
265 }
266
267
279 template <class GridView, class VariableSet>
280 void writeAMIRAFile(GridView const& gridView,
281 VariableSet const& vars,
282 std::string file_out,
285 std::string file_in = "")
286 {
287 //check for .am extension
288 if( (!file_in.empty()) && (file_in.substr( file_in.length()-3) != ".am") )
289 file_in.append(".am");
290 if( (file_out.substr( file_out.length()-3) != ".am") )
291 file_out.append(".am");
292
293 auto const& gv = vars.descriptions->gridView;
294
296 AmiraWriter writer(gv.grid());
297
298 if( materialData.size() != 0)
299 writer.addMaterialData(gv, materialData);
300
301 std::string fullname = file_out;
302
303 if (options.outputType == IoOptions::ascii)
304 writePartialFile(vars.descriptions->gridView,writer,vars,fullname,UnaryTrue(),1);
305 else if (options.outputType == IoOptions::binary)
306 writePartialFile(gv,writer,vars,fullname,UnaryTrue(),0);
307 else std::cout << "Amira: graphic output for nonvalid driver" << '\n';
308
309 if(!file_in.empty())
310 copyHeader(gv, file_in, file_out);
311 }
312
313
314} // end namespace Kaskade
315
316
317
318#endif
Provides file writing facilities in the AmiraMesh format.
void addGrid(const GridType::LeafGridView &gridView, bool splitAll=false)
ValueType value(Cell const &cell, Dune::FieldVector< typename Cell::Geometry::ctype, Cell::dimension > const &localCoordinate) const
MaterialAmiraMeshWriter()
Default constructor.
Definition amira.hh:66
MaterialAmiraMeshWriter(const GridType &grid)
Constructor which initializes the AmiraMesh object with a given leaf grid.
Definition amira.hh:69
Dune::BlockVector< Dune::FieldVector< double, 1 > > DataContainer
Definition amira.hh:63
void addMaterialData(const GridView &gridView, DataContainer &materialData)
Definition amira.hh:80
GridType::LeafGridView GridView
Definition amira.hh:62
A class for storing a heterogeneous collection of FunctionSpaceElement s.
Definition variables.hh:337
Descriptions const * descriptions
Descriptions of variable set, of type VariableSetDescription (lots of useful infos)
Definition variables.hh:529
void writePartialFile(GridView const &gridView, Writer &writer, VariableSet const &vars, std::string const &filename, Filter const &filter, Options const &options)
Definition iobase.hh:191
Output of mesh and solution for visualization software.
void writeAMIRAFile(GridView const &gridView, VariableSet const &vars, std::string file_out, IoOptions options=ioOptions_default, Dune::BlockVector< Dune::FieldVector< double, 1 > > materialData=Dune::BlockVector< Dune::FieldVector< double, 1 > >(), std::string file_in="")
Definition amira.hh:280
IoOptions ioOptions_default
void copyHeader(GridView const &gridView, std::string const &file_in, std::string const &file_out)
Definition amira.hh:214
Convenience typedefs and creation functions for common function spaces.
options for VTK/AMIRA output
Definition iobase.hh:77
Output of mesh and solution for visualization software.