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