46 std::string dataName,
const std::string class_name,
DataType type)
47 : clout(class_name), _dim(dim), _comps(0), _origin(_dim, 0), _extent(_dim, 0),
48 _delta(0), _xmlReader(fileName), _nCuboids(0), _type(type)
55 std::stringstream stream_val_1(
_xmlReader[
"ImageData"].getAttribute(
"Spacing"));
59 stream_val_1 >> delta1;
60 stream_val_1 >> delta2;
61 stream_val_1 >> delta3;
62 T eps = std::numeric_limits<T>::epsilon();
63 if( std::fabs(delta1 - delta2) > eps || std::fabs(delta1 - delta3) > eps ) {
64 clout <<
" Error: The lattice has to be homogenous" << std::endl;
70 std::stringstream stream_val_2(
_xmlReader[
"ImageData"].getAttribute(
"Origin"));
71 for (
auto& origin_i :
_origin) {
72 stream_val_2 >> origin_i;
76 for (
auto& pieceReader :
_xmlReader[
"ImageData"] ) {
77 if (pieceReader->getName() ==
"Piece") {
80 for (
auto& dataArrayReader : (*pieceReader)[
"PointData"]) {
81 if (dataArrayReader->getAttribute(
"Name") == dataName && dataArrayReader->getName() ==
"DataArray") {
86 for (
auto& dataArrayReader : (*pieceReader)[
"CellData"]) {
87 if (dataArrayReader->getAttribute(
"Name") == dataName && dataArrayReader->getName() ==
"DataArray") {
101 clout <<
"Information on VTIreader Data:" << std::endl;
103 for (
auto& origin_i : _origin) {
104 clout << origin_i <<
" ";
109 for (
auto& extend_i : _extent) {
110 clout << extend_i <<
" ";
114 clout <<
"Spacing: " << _delta << std::endl;
122 std::stringstream extstr(reader->
getAttribute(extAttrName));
123 std::vector<int> extents;
125 for (
int i = 0; i < 2 * _dim; ++i) {
127 extents.push_back(tmp);
136 std::vector<int> nNodes;
137 for (
int i = 0; i < _dim; i++ ) {
138 if (_type ==
PointData) nNodes.push_back(extents[2 * i + 1 ] - extents[2 * i ] + 1);
139 else nNodes.push_back(extents[2 * i + 1] - extents[2 * i]);
148 int comps = std::atoi((tagReader.
getAttribute(
"NumberOfComponents")).c_str());
150 clout <<
"Warning: NumberOfComponents zero or not given!" << std::endl;
151 clout <<
"Example: <DataArray Name='physVelocity' NumberOfComponents='3'" << std::endl;
152 clout <<
"Setting to default of NumberOfComponents='1'" << std::endl;
161 for(
int i = 0; i < _dim; i++) {
162 result *= _extent[i];
164 return result * _comps;
170template<
typename T,
typename BaseType>
172 const std::string class_name,
const DataType type)
177template<
typename T,
typename BaseType>
180 if (pieceReader->
getName() ==
"Piece") {
181 std::vector<int> extents = this->readExtent(pieceReader,
"Extent");
182 std::vector<int> extent = this->getNbNodes(extents);
184 cuboid.init(extents[0] * this->_delta,
185 extents[2] * this->_delta,
186 extents[4] * this->_delta,
195template<
typename T,
typename BaseType>
199 for (
int iz = 0; iz < blockData.
getNz(); iz++) {
200 for (
int iy = 0; iy < blockData.
getNy(); iy++) {
201 for (
int ix = 0; ix < blockData.
getNx(); ix++) {
202 for (
unsigned iSize=0; iSize < blockData.
getSize(); iSize++) {
205 if(stream_val.eof()) {
206 this->clout <<
" Error: End of input has been reached, but there is still data to be written" << std::endl;
210 if (blockData.
isInside({ix, iy, iz})) {
214 blockData.
get({ix, iy, iz}, iSize) = tmp;
216 this->clout <<
"Found point outside of the block structure" << std::endl;
217 blockData.
get({ix, iy, iz}, iSize) = 0;
224 if (!stream_val.eof()) {
225 this->clout <<
" Error: There are still values left in the value stream but the Block Data is already filled" << std::endl;
230template<
typename T,
typename BaseType>
231void BaseVTIreader3D<T, BaseType>::readBinaryData(std::stringstream& stream_val, BlockData<3, T, BaseType>& blockData,
234 Base64Decoder<BaseType> decoder(stream_val, str_length);
235 size_t totalDataValues = this->computeTotalDataValues();
236 std::vector<BaseType> values(totalDataValues);
237 decoder.decode(values.data(), totalDataValues);
239 size_t value_idx = 0;
240 for (
int iz = 0; iz < blockData.getNz(); iz++) {
241 for (
int iy = 0; iy < blockData.getNy(); iy++) {
242 for (
int ix = 0; ix < blockData.getNx(); ix++) {
243 for (
unsigned iSize=0; iSize < blockData.getSize(); iSize++) {
245 if(value_idx >= totalDataValues) {
246 this->clout <<
" Error: End of input has been reached, but there is still data to be written" << std::endl;
250 if (blockData.isInside({ix, iy, iz})) {
252 blockData.get({ix, iy, iz}, iSize) = values[value_idx];
254 this->clout <<
"Found point outside of the block structure" << std::endl;
255 blockData.get({ix, iy, iz}, iSize) = 0;
263 if (value_idx < totalDataValues) {
264 this->clout <<
" Error: There are still values left in the value stream but the Block Data is already filled" << std::endl;
269template<
typename T,
typename BaseType>
271 const XMLreader& pieceTagReader,
const std::string dataName)
274 std::string arrayType = (this->_type ==
PointData) ?
"PointData" :
"CellData";
277 bool attributeFound =
false;
278 for (
auto & dataArrayReader : pieceTagReader[arrayType]) {
279 if (dataArrayReader->getAttribute(
"Name") == dataName && dataArrayReader->getName() ==
"DataArray") {
280 attributeFound =
true;
281 std::string data_str;
282 if (dataArrayReader->read(data_str)) {
283 std::stringstream stream_val(data_str);
284 if (dataArrayReader->getAttribute(
"format") ==
"binary") {
285 this->clout <<
"Reading binary data" << std::endl;
286 readBinaryData(stream_val, blockData, data_str.length());
287 }
else if (dataArrayReader->getAttribute(
"format") ==
"ascii") {
288 this->clout <<
"Reading ASCII data" << std::endl;
289 readAsciiData(stream_val, blockData);
291 this->clout <<
"Warning: The file format has to be either 'ascii' or 'binary', but it is '" << dataArrayReader->getAttribute(
"format") <<
"'."<< std::endl;
297 if(!attributeFound) {
298 this->clout <<
"Could not find attribute with the given Name: '" << dataName <<
"'" << std::endl;
305template<
typename T,
typename BaseType>
308 _cuboid(this->_origin, this->_delta, this->_extent),
309 _blockData(_cuboid, 0, this->_comps)
311 size_t pieceTagCounter = 0;
313 if(child->getName() ==
"Piece") {
317 if(pieceTagCounter == 0) {
318 this->
clout <<
" Error: No piece tag could be found in " << fileName << std::endl;
321 if(pieceTagCounter > 1) {
322 this->
clout <<
"Found several piece tags, only reads the first one" << std::endl;
329template<
typename T,
typename BaseType>
335template<
typename T,
typename BaseType>
344template<
typename T,
typename BaseType>
347 delete _loadBalancer;
352template<
typename T,
typename BaseType>
356 this->
clout <<
"Start reading \"" << fName <<
"\"... "
357 <<
"(" << this->
_nCuboids <<
" cuboids)" << std::endl;
362 this->
clout <<
"* Reading Cuboid Geometry..." << std::endl;
365 readCuboidDecomposition();
367 _cGeometry->printExtended();
375 this->
clout <<
"* Reading BlockData..." << std::endl;
378 readSuperData(dName);
380 this->
clout <<
"VTI Reader finished." << std::endl;
383template<
typename T,
typename BaseType>
387 for (
auto& piece : this->_xmlReader[
"ImageData"] ) {
388 if (piece->getName() ==
"Piece") {
389 std::vector<int> extents = this->readExtent(piece,
"Extent");
390 std::vector<int> extent = this->getNbNodes(extents);
393 extents[2] * this->_delta,
394 extents[4] * this->_delta,
399 _cGeometry->add(cuboid);
404template<
typename T,
typename BaseType>
405void SuperVTIreader3D<T,BaseType>::readSuperData(
const std::string dName)
409 for (
auto & piece : this->_xmlReader[
"ImageData"]) {
410 if (piece->getName() ==
"Piece") {
411 this->readBlockData(_superData->getBlock(counter), *piece, dName);
417template<
typename T,
typename BaseType>
423template<
typename T,
typename BaseType>
429template<
typename T,
typename BaseType>
432 return *_loadBalancer;
438template<
typename T,
typename BaseType>
440 const std::string class_name,
const DataType type)
445template<
typename T,
typename BaseType>
448 if (pieceReader->
getName() ==
"Piece") {
449 std::vector<int> extents = this->readExtent(pieceReader,
"Extent");
450 std::vector<int> extent = this->getNbNodes(extents);
452 cuboid.init(extents[0] * this->_delta,
453 extents[2] * this->_delta,
460template<
typename T,
typename BaseType>
464 for (
int iy = 0; iy < blockData.
getNy(); iy++) {
465 for (
int ix = 0; ix < blockData.
getNx(); ix++) {
466 for (
unsigned iSize=0; iSize < blockData.
getSize(); iSize++) {
469 if(stream_val.eof()) {
470 this->clout <<
" Error: End of input has been reached, but there is still data to be written" << std::endl;
478 blockData.
get({ix, iy}, iSize) = tmp;
480 this->clout <<
"Found point outside of the block structure" << std::endl;
481 blockData.
get({ix, iy}, iSize) = 0;
487 if (!stream_val.eof()) {
488 this->clout <<
" Error: There are still values left in the value stream but the Block Data is already filled" << std::endl;
493template<
typename T,
typename BaseType>
494void BaseVTIreader2D<T, BaseType>::readBinaryData(std::stringstream& stream_val, BlockData<2, T, BaseType>& blockData,
size_t str_length)
499 Base64Decoder<BaseType> decoder(stream_val, str_length);
501 size_t totalDataValues = this->computeTotalDataValues();
502 std::vector<BaseType> values(totalDataValues);
503 decoder.decode(values.data(), totalDataValues);
505 size_t value_idx = 0;
506 for (
int iy = 0; iy < blockData.getNy(); iy++) {
507 for (
int ix = 0; ix < blockData.getNx(); ix++) {
508 for (
unsigned iSize=0; iSize < blockData.getSize(); iSize++) {
510 if(value_idx >= totalDataValues) {
511 this->clout <<
" Error: End of input has been reached, but there is still data to be written" << std::endl;
515 if (blockData.isInside({ix, iy})) {
518 blockData.get({ix, iy}, iSize) = values[value_idx];
520 this->clout <<
"Found point outside of the block structure" << std::endl;
521 blockData.get({ix, iy}, iSize) = 0;
528 if (value_idx < totalDataValues) {
529 this->clout <<
" Error: There are still values left in the value stream but the Block Data is already filled" << std::endl;
534template<
typename T,
typename BaseType>
537 std::string arrayType = (this->_type ==
PointData) ?
"PointData" :
"CellData";
540 bool attributeFound =
false;
541 for (
auto & dataArrayReader : pieceTagReader[arrayType]) {
542 if (dataArrayReader->getAttribute(
"Name") == dataName && dataArrayReader->getName() ==
"DataArray") {
543 attributeFound =
true;
544 std::string data_str;
545 if (dataArrayReader->read(data_str)) {
546 std::stringstream stream_val(data_str);
547 if (dataArrayReader->getAttribute(
"format") ==
"binary") {
549 readBinaryData(stream_val, blockData, data_str.length());
550 }
else if (dataArrayReader->getAttribute(
"format") ==
"ascii") {
552 readAsciiData(stream_val, blockData);
554 this->clout <<
"Warning: The file format has to be either 'ascii' or 'binary', but it is '" << dataArrayReader->getAttribute(
"format") <<
"'."<< std::endl;
560 if(!attributeFound) {
561 this->clout <<
"Could not find attribute with the given Name: '" << dataName <<
"'" << std::endl;
568template<
typename T,
typename BaseType>
571 _cuboid(this->_origin, this->_delta, this->_extent),
572 _blockData(_cuboid, 0, this->_comps)
574 size_t pieceTagCounter = 0;
575 for (
XMLreader* child : this->_xmlReader[
"ImageData"]) {
576 if(child->getName() ==
"Piece") {
580 if(pieceTagCounter == 0) {
581 this->
clout <<
" Error: No piece tag could be found in " << fileName << std::endl;
584 if(pieceTagCounter > 1) {
585 this->
clout <<
"Found several piece tags, only reads the first one" << std::endl;
591template<
typename T,
typename BaseType>
597template<
typename T,
typename BaseType>
BaseVTIreader2D(const std::string &fileName, std::string dataName, const std::string class_name="BaseVTIreader2D", const DataType type=PointData)
bool readBlockData(BlockData< 2, T, BaseType > &blockData, const XMLreader &pieceTagReader, const std::string dataName)
Reads from DataArray and fills blockData.
void readCuboid(Cuboid2D< T > &cuboid, XMLreader *pieceReader)
Reads cuboid from piece node.
void readCuboid(Cuboid3D< T > &cuboid, XMLreader *pieceReader)
Reads cuboid from piece node.
bool readBlockData(BlockData< 3, T, BaseType > &blockData, const XMLreader &pieceTagReader, const std::string dataName)
Reads from DataArray and fills blockData.
BaseVTIreader3D(const std::string &fileName, std::string dataName, const std::string class_name="BaseVTIreader3D", const DataType type=PointData)
std::vector< int > getNbNodes(std::vector< int > &extents)
Converts 4D (or 6D) extents vector into 2D (3D) nb_nodes vector.
BaseVTIreader(const std::string &fName, int dim, std::string dName, const std::string class_name="BaseVTIreader", const DataType type=PointData)
size_t computeTotalDataValues()
Computes the total amount of data values Number of components * number of cells/point.
std::vector< int > readExtent(const XMLreader *reader, std::string extAttrName)
Reads Extent from extAttrName from XML Tag and returns as vector.
int getNbComps(const XMLreader &tagReader)
Reads size from XML tag (attribute "NumberOfComponents") Reads the number of components from the XML ...
std::vector< int > _extent
U & get(std::size_t iCell, int iD=0)
int getNy() const
Read only access to block height.
int getNx() const
Read only access to block width.
int getNz() const
Read only access to block height.
bool isInside(LatticeR< D > latticeR) const
Return whether location is valid.
BlockData< 2, T, BaseType > _blockData
Cuboid2D< T > & getCuboid()
BlockData< 2, T, BaseType > & getBlockData()
BlockVTIreader2D(const std::string &fileName, const std::string &dataName, const DataType type=PointData)
Cuboid3D< T > & getCuboid()
BlockData< 3, T, BaseType > _blockData
BlockVTIreader3D(const std::string &fileName, const std::string &dataName, const DataType type=PointData)
BlockData< 3, T, BaseType > & getBlockData()
Constructs a load balancer from a given cuboid geometry using a heurist.
Base class for all LoadBalancer.
~SuperVTIreader3D() override
CuboidDecomposition3D< T > & getCuboidDecomposition()
LoadBalancer< T > & getLoadBalancer()
SuperData< 3, T, BaseType > & getSuperData()
SuperVTIreader3D(const std::string &fName, const std::string dName)
std::string getAttribute(const std::string &aName) const
std::string getName() const
return the name of the element
Top level namespace for all of OpenLB.
CuboidDecomposition< T, 3 > CuboidDecomposition3D
typename util::BaseTypeHelper< T >::type BaseType
The VTI reader is able to read from VTI files and create and fill corresponding data structures.