1#ifndef ExporterParaView_DEF_hpp
2#define ExporterParaView_DEF_hpp
4#include "ExporterParaView_decl.hpp"
16template<
class SC,
class LO,
class GO,
class NO>
17ExporterParaView<SC,LO,GO,NO>::ExporterParaView():
21closingLinesPosition_(),
22closingLinesPositionTimes_(),
40nmbExportValuesGlob_(0),
41nmbPointsPerElement_(0),
52template<
class SC,
class LO,
class GO,
class NO>
53void ExporterParaView<SC,LO,GO,NO>::setup(std::string filename,
57 ParameterListPtr_Type parameterList){
60 setup( filename, mesh, FEType, parameterList);
61 saveTimestep_ = saveTimestep;
64template<
class SC,
class LO,
class GO,
class NO>
65void ExporterParaView<SC,LO,GO,NO>::setup(std::string filename,
68 ParameterListPtr_Type parameterList){
71 parameterList_ = parameterList;
72 comm_ = mesh->getComm();
73 verbose_ = (comm_->getRank() == 0);
74 Teuchos::RCP<const Teuchos::MpiComm<int> > mpiComm = Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >( mesh->getComm() );
75 commEpetra_.reset(
new Epetra_MpiComm( *mpiComm->getRawMpiComm() ) );
77 outputFilename_ = filename_ +
".h5";
79 dim_ = mesh->getDimension();
80 nmbElementsGlob_ = mesh->getNumElementsGlobal();
82 pointsUnique_ = mesh->getPointsUnique();
84 if (FEType_ ==
"P0") {
86 nmbPointsPerElement_ = 3 ;
90 nmbPointsPerElement_ = 4 ;
94 else if (FEType_ ==
"P1") {
96 nmbPointsPerElement_ = 3 ;
100 nmbPointsPerElement_ = 4 ;
104 else if (FEType_ ==
"P1-disc") {
106 nmbPointsPerElement_ = 3 ;
110 nmbPointsPerElement_ = 4 ;
114 else if(FEType_ ==
"P2"){
117 nmbPointsPerElement_ = 6 ;
121 nmbPointsPerElement_ = 10 ;
124 else if(FEType_ ==
"P2-CR"){
126 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Wrong dimension for P2-CR.");
129 nmbPointsPerElement_ = 10 ;
132 else if(FEType_ ==
"Q1"){
134 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Wrong dimension for Q1.");
137 nmbPointsPerElement_ = 8 ;
141 else if(FEType_ ==
"Q2"){
143 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Wrong dimension for Q2.");
146 nmbPointsPerElement_ = 20 ;
149 else if(FEType_ ==
"Q2-20"){
151 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Wrong dimension for Q2-20.");
154 nmbPointsPerElement_ = 20 ;
158 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Wrong FEType, choose either P0 or P1 or P1-disc or P2 or P2-CR or Q1 or Q2 or Q2-20. Subdomain export with P0 - Export stopped");
161 nmbPointsGlob_ = mesh->getMapUnique()->getGlobalNumElements();
163 closingLines_ =
"\n </Grid>\n\n </Domain>\n</Xdmf>\n";
167 MapConstPtr_Type elementMap = mesh->getElementMap();
168 Teuchos::ArrayView<const GO> nodeElementList = elementMap->getNodeElementList();
169 vec_int_Type nodeElementListInteger( nmbPointsPerElement_ * nodeElementList.size() );
171 for (
int i=0; i<nodeElementList.size(); i++) {
172 for (
int j=0; j<nmbPointsPerElement_; j++){
173 nodeElementListInteger[counter] = (int) nmbPointsPerElement_*nodeElementList[i] + j;
177 Teuchos::RCP<Epetra_BlockMap> mapElements;
178 if (nodeElementListInteger.size()>0)
179 mapElements.reset(
new Epetra_BlockMap( (
int) (nmbPointsPerElement_*nmbElementsGlob_), (
int) nodeElementListInteger.size(), &nodeElementListInteger[0],1, 0, *commEpetra_));
181 mapElements.reset(
new Epetra_BlockMap( (
int) (nmbPointsPerElement_*nmbElementsGlob_), (
int) nodeElementListInteger.size(), NULL,1, 0, *commEpetra_));
183 elementsHDF_.reset(
new Epetra_IntVector(*mapElements));
185 ElementsPtr_Type elements = mesh->getElementsC();
187 for (
int i=0; i<elements->numberElements(); i++) {
188 for (
int j=0; j<nmbPointsPerElement_; j++) {
189 int globalIndex = (int) mesh->getMapRepeated()->getGlobalElement( elements->getElement(i).getNode(j) );
190 (*elementsHDF_)[counter] = globalIndex;
195 Teuchos::ArrayView< const GO > indices = mesh->getMapUnique()->getNodeElementList();
196 int* intGlobIDs =
new int[indices.size()];
197 for (
int i=0; i<indices.size(); i++) {
198 intGlobIDs[i] = (int) indices[i];
201 EpetraMapPtr_Type mapEpetra = Teuchos::rcp(
new Epetra_Map((
int)nmbPointsGlob_,indices.size(),intGlobIDs,0,*commEpetra_));
202 delete [] intGlobIDs;
204 pointsHDF_.reset(
new Epetra_MultiVector(*mapEpetra,dim_));
217template<
class SC,
class LO,
class GO,
class NO>
218void ExporterParaView<SC,LO,GO,NO>::addVariable(MultiVecConstPtr_Type &u,
222 MapConstPtrConst_Type& mapUnique){
224 variables_.push_back(u);
225 varNames_.push_back(varName);
226 varTypes_.push_back(varType);
227 varDofPerNode_.push_back(dofPerNode);
229 nmbExportValuesGlob_ = mapUnique->getGlobalNumElements();
233 Teuchos::ArrayView< const GO > indices = mapUnique->getNodeElementList();
234 int* intGlobIDs =
new int[indices.size()];
235 for (
int i=0; i<indices.size(); i++) {
236 intGlobIDs[i] = (int) indices[i];
239 EpetraMapPtr_Type mapToStore = Teuchos::rcp(
new Epetra_Map( (
int) mapUnique->getGlobalNumElements(), indices.size(), intGlobIDs,0, *commEpetra_ ) );
241 uniqueMaps_.push_back(mapToStore);
242 delete [] intGlobIDs;
246template<
class SC,
class LO,
class GO,
class NO>
247void ExporterParaView<SC,LO,GO,NO>::save(
double time){
249 if (timeIndex_ % saveTimestep_ == 0) {
252 if (!parameterList_.is_null()){
253 if ( parameterList_->sublist(
"Exporter").get(
"Write new mesh",
false) )
254 writeMeshPointsHDF5();
257 writeVariablesHDF5();
263 std::cout <<
"\n \t ### Export criterion not satisfied for current time step - no export of time step! ###" << std::endl;
270template<
class SC,
class LO,
class GO,
class NO>
271void ExporterParaView<SC,LO,GO,NO>::save(
double time,
double dt){
273 if (timeIndex_ % saveTimestep_ == 0) {
276 if (!parameterList_.is_null()){
277 if ( parameterList_->sublist(
"Exporter").get(
"Write new mesh",
false) )
278 writeMeshPointsHDF5();
280 writeVariablesHDF5();
287 writeXmfTime(time, dt);
291 std::cout <<
"\n \t ### Export criterion not satisfied for current time step - no export of time step! ###" << std::endl;
298template<
class SC,
class LO,
class GO,
class NO>
299void ExporterParaView<SC,LO,GO,NO>::closeExporter(){
301 hdf5exporter_->Close();
304 xmf_times_out_.close();
309template<
class SC,
class LO,
class GO,
class NO>
310void ExporterParaView<SC,LO,GO,NO>::initHDF5(){
312 hdf5exporter_.reset(
new HDF5_Type(*commEpetra_) );
313 hdf5exporter_->Create(outputFilename_);
314 std::string nameConn =
"Connections";
316 writeMeshElements(nameConn);
318 if (parameterList_.is_null())
319 writeMeshPointsHDF5();
320 else if ( parameterList_->sublist(
"Exporter").get(
"Write new mesh",
false) ==
false )
321 writeMeshPointsHDF5();
325template<
class SC,
class LO,
class GO,
class NO>
326void ExporterParaView<SC,LO,GO,NO>::writeMeshPointsHDF5(){
327 if (!parameterList_.is_null()){
328 if ( parameterList_->sublist(
"Exporter").get(
"Write new mesh",
false) ) {
330 std::string nameP_X =
"PointsX" + std::to_string(timeIndex_);
331 std::string nameP_Y =
"PointsY" + std::to_string(timeIndex_);
332 std::string nameP_Z =
"PointsZ" + std::to_string(timeIndex_);
334 writeMeshPoints(nameP_X, nameP_Y, nameP_Z );
338 std::string nameP_X =
"PointsX";
339 std::string nameP_Y =
"PointsY";
340 std::string nameP_Z =
"PointsZ";
341 writeMeshPoints( nameP_X, nameP_Y, nameP_Z );
345 std::string nameP_X =
"PointsX";
346 std::string nameP_Y =
"PointsY";
347 std::string nameP_Z =
"PointsZ";
348 writeMeshPoints( nameP_X, nameP_Y, nameP_Z );
352template<
class SC,
class LO,
class GO,
class NO>
353void ExporterParaView<SC,LO,GO,NO>::writeMeshElements( std::string nameConn ){
355 hdf5exporter_->CreateGroup(nameConn);
357 hdf5exporter_->Write(nameConn,*elementsHDF_);
360template<
class SC,
class LO,
class GO,
class NO>
361void ExporterParaView<SC,LO,GO,NO>::writeMeshPoints(std::string nameP_X,
363 std::string nameP_Z){
367 hdf5exporter_->CreateGroup(nameP_X);
368 hdf5exporter_->CreateGroup(nameP_Y);
370 hdf5exporter_->Write(nameP_X,*(*pointsHDF_)(0));
371 hdf5exporter_->Write(nameP_Y,*(*pointsHDF_)(1));
374 hdf5exporter_->CreateGroup(nameP_Z);
375 hdf5exporter_->Write(nameP_Z,*(*pointsHDF_)(2));
377 hdf5exporter_->Flush();
381template<
class SC,
class LO,
class GO,
class NO>
382void ExporterParaView<SC,LO,GO,NO>::updatePoints(){
384 if (pointsUnique_->size()>0)
385 dim = pointsUnique_->at(0).size();
387 for (
int i=0; i<pointsUnique_->size(); i++) {
388 for (
int j = 0; j < dim; j++) {
389 pointsHDF_->ReplaceMyValue(i,j,(*pointsUnique_)[i][j]);
394template<
class SC,
class LO,
class GO,
class NO>
395void ExporterParaView<SC,LO,GO,NO>::writeVariablesHDF5(){
397 for (
int i=0; i<variables_.size(); i++) {
399 if (varTypes_.at(i)==
"Vector") {
400 EpetraMVPtr_Type u_export(
new Epetra_MultiVector(*(uniqueMaps_.at(i)),3));
402 hdf5exporter_->CreateGroup(varNames_[i]+postfix_);
404 prepareVectorField(variables_.at(i),u_export, varDofPerNode_.at(i));
406 hdf5exporter_->Write(varNames_[i]+postfix_,*u_export,
true);
408 else if(varTypes_.at(i)==
"Scalar"){
409 EpetraMVPtr_Type u_export(
new Epetra_MultiVector(*(uniqueMaps_.at(i)),1));
411 hdf5exporter_->CreateGroup(varNames_[i]+postfix_);
413 prepareScalar(variables_.at(i),u_export);
415 hdf5exporter_->Write(varNames_[i]+postfix_,*u_export);
417 hdf5exporter_->Flush();
422template<
class SC,
class LO,
class GO,
class NO>
423void ExporterParaView<SC,LO,GO,NO>::initXmf(){
425 if (comm_->getRank()==0) {
427 xmf_out_.open((filename_ +
".xmf").c_str(),std::ios_base::out);
428 xmf_out_ <<
"<?xml version=\"1.0\" ?>\n"
429 <<
"<!DOCTYPE Xdmf SYSTEM \""
432 <<
"<!ENTITY DataFile \""
440 <<
" <Domain Name=\""
445 <<
"Grid\" GridType=\"Collection\" CollectionType=\"Temporal\">\n"
448 closingLinesPosition_ = xmf_out_.tellp();
450 xmf_out_ << closingLines_;
456template<
class SC,
class LO,
class GO,
class NO>
457void ExporterParaView<SC,LO,GO,NO>::initXmfTimes(){
459 if (comm_->getRank()==0) {
461 xmf_times_out_.open((filename_ +
"_times.xmf").c_str(),std::ios_base::out);
462 xmf_times_out_ <<
"<?xml version=\"1.0\" ?>\n"
463 <<
"<!DOCTYPE Xdmf SYSTEM \""
467 <<
" <Domain Name=\""
472 <<
"Grid\" GridType=\"Collection\" CollectionType=\"Temporal\">\n"
475 closingLinesPositionTimes_ = xmf_times_out_.tellp();
477 xmf_times_out_ << closingLines_;
478 xmf_times_out_.flush();
482template<
class SC,
class LO,
class GO,
class NO>
483void ExporterParaView<SC,LO,GO,NO>::writeXmf(
double time){
488 std::string nameConn =
"Connections";
490 nameConn =
"Connections" + std::to_string(timeIndex_);
491 if (!parameterList_.is_null()){
492 if ( parameterList_->sublist(
"Exporter").get(
"Write new mesh",
false) ) {
493 nameP_X =
"PointsX" + std::to_string(timeIndex_);
494 nameP_Y =
"PointsY" + std::to_string(timeIndex_);
495 nameP_Z =
"PointsZ" + std::to_string(timeIndex_);
508 writeXmfElements( nameConn, time);
509 writeXmfPoints( nameP_X, nameP_Y, nameP_Z );
514template<
class SC,
class LO,
class GO,
class NO>
515void ExporterParaView<SC,LO,GO,NO>::writeXmfElements( std::string nameConn,
double time ){
518 xmf_out_.seekp (closingLinesPosition_);
521 "<!-- Time " << time <<
" Iteration " << postfix_.substr (1, 5) <<
" -->\n" <<
522 " <Grid Name=\"Mesh" << FEType_ <<
" " << time <<
"\">\n" <<
523 " <Time TimeType=\"Single\" Value=\"" << time <<
"\" />\n";
526 std::string FEstring;
529 FEstring =
"Triangle";
532 FEstring =
"Tetrahedron";
535 else if (FEType_==
"P1-disc") {
537 FEstring =
"Triangle";
540 FEstring =
"Tetrahedron";
543 else if (FEType_==
"P1") {
545 FEstring =
"Triangle";
548 FEstring =
"Tetrahedron";
552 else if (FEType_==
"P2"){
560 else if (FEType_==
"P2-CR"){
566 else if (FEType_==
"Q1"){
569 FEstring =
"Hexahedron";
572 else if (FEType_==
"Q2"){
578 else if (FEType_==
"Q2-20"){
585 xmf_out_ <<
" <Topology\n"
589 <<
" NumberOfElements=\""
595 <<
" <DataStructure Format=\"HDF\"\n"
599 << nmbPointsPerElement_
600 <<
"\"\n" <<
" DataType=\"Int\"\n"
601 <<
" Precision=\"8\">\n" <<
" "
602 << outputFilename_ <<
":/"<< nameConn <<
"/Values\n"
603 <<
" </DataStructure>\n" <<
" </Topology>\n";
605 closingLinesPosition_ = xmf_out_.tellp();
609template<
class SC,
class LO,
class GO,
class NO>
610void ExporterParaView<SC,LO,GO,NO>::writeXmfPoints(std::string nameP_X,
612 std::string nameP_Z ){
615 xmf_out_.seekp (closingLinesPosition_);
619 " <Geometry Type=\"X_Y\">\n" <<
620 " <DataStructure Format=\"HDF\"\n" <<
621 " Dimensions=\"" << nmbPointsGlob_ <<
"\"\n" <<
622 " DataType=\"Float\"\n" <<
623 " Precision=\"8\">\n" <<
624 " " << outputFilename_ <<
":/" << nameP_X <<
"/Values\n" <<
625 " </DataStructure>\n" <<
626 " <DataStructure Format=\"HDF\"\n" <<
627 " Dimensions=\"" << nmbPointsGlob_ <<
"\"\n" <<
628 " DataType=\"Float\"\n" <<
629 " Precision=\"8\">\n" <<
630 " " << outputFilename_ <<
":/" << nameP_Y <<
"/Values\n" <<
631 " </DataStructure>\n" <<
637 " <Geometry Type=\"X_Y_Z\">\n" <<
638 " <DataStructure Format=\"HDF\"\n" <<
639 " Dimensions=\"" << nmbPointsGlob_ <<
"\"\n" <<
640 " DataType=\"Float\"\n" <<
641 " Precision=\"8\">\n" <<
642 " " << outputFilename_ <<
":/" << nameP_X <<
"/Values\n" <<
643 " </DataStructure>\n" <<
644 " <DataStructure Format=\"HDF\"\n" <<
645 " Dimensions=\"" << nmbPointsGlob_ <<
"\"\n" <<
646 " DataType=\"Float\"\n" <<
647 " Precision=\"8\">\n" <<
648 " " << outputFilename_ <<
":/" << nameP_Y <<
"/Values\n" <<
649 " </DataStructure>\n" <<
650 " <DataStructure Format=\"HDF\"\n" <<
651 " Dimensions=\"" << nmbPointsGlob_ <<
"\"\n" <<
652 " DataType=\"Float\"\n" <<
653 " Precision=\"8\">\n" <<
654 " " << outputFilename_ <<
":/" << nameP_Z <<
"/Values\n" <<
655 " </DataStructure>\n" <<
659 closingLinesPosition_ = xmf_out_.tellp();
665template<
class SC,
class LO,
class GO,
class NO>
666void ExporterParaView<SC,LO,GO,NO>::writeXmfVariables(){
668 std::string centerString;
670 centerString =
"Cell";
672 else if (FEType_==
"P1-disc") {
675 else if (FEType_==
"P1") {
676 centerString =
"Node";
679 else if (FEType_==
"P2"){
680 centerString =
"Node";
682 else if (FEType_==
"Q2"){
683 centerString =
"Node";
687 xmf_out_.seekp (closingLinesPosition_);
689 for (
int i=0; i<varNames_.size(); i++) {
690 int dof = varDofPerNode_.at(i);
696 " Type=\"" << varTypes_.at(i) <<
"\"\n" <<
697 " Center=\"" << centerString <<
"\"\n" <<
698 " Name=\"" << varNames_.at(i) <<
"\">\n";
702 " <DataStructure ItemType=\"HyperSlab\"\n" <<
703 " Dimensions=\"" << nmbExportValuesGlob_ <<
" " << dof <<
"\"\n" <<
704 " Type=\"HyperSlab\">\n" <<
705 " <DataStructure Dimensions=\"3 2\"\n" <<
706 " Format=\"XML\">\n" <<
709 " " << nmbExportValuesGlob_ <<
" " << dof <<
"\n" <<
710 " </DataStructure>\n" <<
712 " <DataStructure Format=\"HDF\"\n" <<
713 " Dimensions=\"" << nmbExportValuesGlob_ <<
" " << dof <<
"\"\n" <<
714 " DataType=\"Float\"\n" <<
715 " Precision=\"8\">\n" <<
716 " " << outputFilename_ <<
":/" << varNames_.at(i)
717 << postfix_ <<
"/Values\n" <<
718 " </DataStructure>\n" <<
719 " </DataStructure>\n";
729 closingLinesPosition_ = xmf_out_.tellp();
731 xmf_out_ << closingLines_;
735template<
class SC,
class LO,
class GO,
class NO>
736void ExporterParaView<SC,LO,GO,NO>::writeXmfTime(
double time,
double dt){
738 if (comm_->getRank()==0) {
740 xmf_times_out_.seekp (closingLinesPositionTimes_);
743 "<!-- Time " << time <<
" Iteration " << postfix_.substr (1, 5) <<
" -->\n" <<
744 " <Grid Name=\"Mesh Times " << time <<
"\">\n" <<
745 " <Time TimeType=\"Single\" Value=\"" << time <<
"\" />\n";
748 <<
" Type=\"Polyvertex\"\n"
749 <<
" NumberOfElements=\""
755 <<
" <DataStructure Format=\"XML\"\n"
756 <<
" Dimensions=\"2\"\n"
757 <<
" DataType=\"Int\"\n"
758 <<
" Precision=\"8\">\n"
760 <<
" </DataStructure>\n" <<
" </Topology>\n";
764 " <Geometry Type=\"X_Y\">\n" <<
765 " <DataStructure Format=\"XML\"\n" <<
766 " Dimensions=\"" << 2 <<
"\"\n" <<
767 " DataType=\"Float\"\n" <<
768 " Precision=\"8\">\n" <<
769 " " <<
"0.0 1.0 \n" <<
770 " </DataStructure>\n" <<
771 " <DataStructure Format=\"XML\"\n" <<
772 " Dimensions=\"" << 2 <<
"\"\n" <<
773 " DataType=\"Float\"\n" <<
774 " Precision=\"8\">\n" <<
775 " " <<
"0.0 1.0 \n" <<
776 " </DataStructure>\n" <<
" </Geometry>\n" <<
781 " Type=\"" <<
"Scalar" <<
"\"\n" <<
782 " Center=\"" <<
"Node" <<
"\"\n" <<
783 " Name=\"" <<
"t_dt" <<
"\">\n";
787 " <DataStructure Format=\"XML\"\n" <<
788 " Dimensions=\"" << 2 <<
"\"\n" <<
789 " DataType=\"Float\"\n" <<
790 " Precision=\"8\">\n" <<
791 " " << time <<
" " << dt <<
"\n" <<
792 " </DataStructure>\n";
797 xmf_times_out_ <<
"\n"
799 closingLinesPositionTimes_ = xmf_times_out_.tellp();
801 xmf_times_out_ << closingLines_;
802 xmf_times_out_.flush();
807template<
class SC,
class LO,
class GO,
class NO>
808void ExporterParaView<SC,LO,GO,NO>::prepareVectorField(MultiVecConstPtr_Type &u,
809 EpetraMVPtr_Type &u_export,
812 TEUCHOS_TEST_FOR_EXCEPTION(u->getNumVectors()!=1, std::logic_error,
"Can only export single vector");
814 for (
int i=0; i<(u->getLocalLength()/dof); i++) {
815 for (
int j=0; j < dof; j++) {
816 Teuchos::ArrayRCP<const SC> tmpData = u->getData(0);
817 u_export->ReplaceMyValue( i, j, tmpData[ dof * i + j ] );
822template<
class SC,
class LO,
class GO,
class NO>
823void ExporterParaView<SC,LO,GO,NO>::prepareScalar(MultiVecConstPtr_Type &u,
824 EpetraMVPtr_Type &u_export)
const{
826 TEUCHOS_TEST_FOR_EXCEPTION(u->getNumVectors()!=1, std::logic_error,
"Can only export single vector");
828 Teuchos::ArrayRCP<const SC> tmpData = u->getData(0);
829 for (
int i=0; i<tmpData.size(); i++) {
831 u_export->ReplaceMyValue( i, 0, tmpData[ i ] );
836template<
class SC,
class LO,
class GO,
class NO>
837void ExporterParaView<SC,LO,GO,NO>::makePostfix(){
839 int postfixLength = 5;
840 std::ostringstream index;
843 if (timeIndex_ % saveTimestep_ == 0){
844 index << std::setw (postfixLength) << ( timeIndex_ / saveTimestep_ );
845 postfix_ =
"." + index.str();
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement.cpp:5