<div dir="ltr"><div>I finally was able to output HDF5 files to read in Paraview.</div><div><br></div><div>These are the following functions that make this possible,</div><div>
Here I create the domain using DMDACreate3d(For 2D I just use NZ=1 - You'll Just end up with a thickness during visualization - Accordingly set your DELTAZ)

</div><div><br></div><div>void write_xdmf_header(const std::string &filename){<br>    int rank;<br>    MPI_Comm_rank(PETSC_COMM_WORLD,&rank);<br>    if(rank==0){<br>        std::size_t name_split = filename.find_last_of(".");<br>        std::string xdmf_name = filename.substr(0,name_split) + ".xdmf";<br>        std::ofstream fp(xdmf_name,std::ios::out);<br>        fp << "<?xml version=\"1.0\" ?>\n";<br>        fp << "<!DOCTYPE Xdmf SYSTEM \"Xdmf.dtd\" []>\n";<br>        fp << "<Xdmf Version=\"2.0\"> \n\n";<br>        fp << "  <Domain>\n";<br>        fp << "    <Grid Name=\"Domain\" GridType=\"Uniform\">\n";<br>        fp << "      <Topology TopologyType=\"3DCoRectMesh\" NumberOfElements=\""<<NZ+1<<" "<<NY+1<<" "<<NX+1<<"\"/>\n";<br>        fp << "      <Geometry GeometryType=\"ORIGIN_DXDYDZ\" >"<<"\n";<br>        fp << "        <DataItem Format=\"XML\" NumberType=\"Double\" Dimensions=\"3\"> "<<0<<" "<<0<<" "<<0<<" </DataItem>\n";<br>        fp << "        <DataItem Format=\"XML\" NumberType=\"Double\" Dimensions=\"3\"> "<<DELTA_X<<" "<<DELTA_Y<<" "<<DELTA_Z<<" </DataItem>\n";<br>        fp << "      </Geometry>\n\n";<br>        fp.close();<br>    }<br>}</div><div><br></div><div>template <typename T><br>void Field<T>:: write_xdmf(const std::string &filename){<br>    int rank;<br>    MPI_Comm_rank(PETSC_COMM_WORLD,&rank);<br>    if(rank==0){<br>        std::size_t name_split = filename.find_last_of(".");<br>        std::string xdmf_name = filename.substr(0,name_split) + ".xdmf";<br>        std::ofstream fp(xdmf_name,std::ios::app);<br>        fp << "      <Attribute Name=\""<<field_name<<"\" Center=\"Cell\" AttributeType=\"Scalar\">\n";<br>        fp << "       <DataItem Format=\"HDF\" Dimensions=\"" << Nx << " " << Ny << " " << Nz << "\">\n";<br>        fp << "         "<<filename<<":/"<<field_name<<"\n";<br>        fp << "       </DataItem>\n";<br>        fp << "      </Attribute>\n\n";<br>        fp.close();<br>    }<br>}</div><div><br></div><div>void write_xdmf_footer(const std::string &filename){<br>    int rank;<br>    MPI_Comm_rank(PETSC_COMM_WORLD,&rank);<br>    if(rank==0){<br>        std::size_t name_split = filename.find_last_of(".");<br>        std::string xdmf_name = filename.substr(0,name_split) + ".xdmf";<br>        std::ofstream fp(xdmf_name,std::ios::app);<br>        fp << "    </Grid>\n";<br>        fp << "  </Domain>\n";<br>        fp << "</Xdmf>\n";<br>        fp.close();<br>    }<br>}</div><div><br></div><div>//For HDF file writing</div><div>template <typename T><br>void Field<T>:: write_to_file(const std::string &filename){<br>    PetscViewer viewer;<br>    PetscViewerHDF5Open(PETSC_COMM_WORLD,filename.c_str(),FILE_MODE_APPEND,&viewer);<br>    PetscViewerHDF5SetBaseDimension2(viewer, PETSC_FALSE);<br>    VecView(global_vec, viewer);<br>    PetscViewerDestroy(&viewer);<br>    write_xdmf(filename);<br>}</div><div><br></div><div>Thanks to Matteo (most of the above are from his scripts).</div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 14 Oct 2021 at 21:28, Abhishek G.S. <<a href="mailto:gsabhishek1ags@gmail.com">gsabhishek1ags@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div></div><div>Thanks, Matthew for the clarification/
suggestion. <br></div><div>Thanks, Matteo for the scripts, I'll give this a try and get back with an update<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 14 Oct 2021 at 19:57, Matthew Knepley <<a href="mailto:knepley@gmail.com" target="_blank">knepley@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">On Thu, Oct 14, 2021 at 9:21 AM Matteo Semplice <<a href="mailto:matteo.semplice@uninsubria.it" target="_blank">matteo.semplice@uninsubria.it</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

  
  <div>
    <p><br>
    </p>
    <div>Il 14/10/21 14:37, Matthew Knepley ha
      scritto:<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">
        <div dir="ltr">On Wed, Oct 13, 2021 at 6:30 PM Abhishek G.S.
          <<a href="mailto:gsabhishek1ags@gmail.com" target="_blank">gsabhishek1ags@gmail.com</a>>
          wrote:<br>
        </div>
        <div class="gmail_quote">
          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
            <div dir="ltr">
              <div>Hi,</div>
              <div>I need some help with getting the file output working
                right.<br>
              </div>
              <div><br>
              </div>
              <div>
                I am using a DMDACreate3D to initialize my DM. This is
                my write function
              </div>
              <div><br>
              </div>
              <div>
                <div>void write(){</div>
                <div>PetscViewer viewer;<br>
PetscViewerHDF5Open(PETSC_COMM_WORLD,filename.c_str(),FILE_MODE_WRITE,&viewer);</div>
                <div>
                  DMDAVecRestoreArray(dm,global_vector,global_array)</div>
                <div>VecView(global_vec, viewer);</div>
                <div>
                  DMDAVecGetArray(dm,global_vector,global_array);</div>
                <div>PetscViewerDestroy(&viewer);</div>
                <div>}</div>
              </div>
              <div><br>
              </div>
              <div>1) I have 2 PDE's to solve. Still, I went ahead
                creating a single DM with dof=1 and creating two vectors
                using the DMCreateGlobalVector(). I want to write the
                file out periodically.  Should I perform
                DMDAVecRestoreArray and DMDAVecGetArray every time is
                write out the global_vector? (I know that it is just
                indexing the pointers and there is no copying of values.
                But I am not sure)</div>
            </div>
          </blockquote>
          <div><br>
          </div>
          <div>I don't think you need the Get/RestoreArray() calls here.</div>
          <div> </div>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
            <div dir="ltr">
              <div>2) I am writing out to HDF5 format. I see that the
                vecview is supposed to reorder the global_vector based
                on the DM. However, when I read the H5 files, I get an
                error on ViSIT and my output image becomes a 1D image
                rather than a 2D/3D. What might be the reason for this
                ?.<br>
              </div>
              <div>Error Msg : "In domain 0, your zonal variable
                "avtGhostZones" has 25600 values, but it should have
                160.  Some values were removed to ensure VisIt runs
                smoothly"</div>
              <div>I was using a 160x160x1 DM</div>
            </div>
          </blockquote>
          <div><br>
          </div>
          <div>I do not believe we support HDF5 <-->
            Visit/Paraview for DMDA. The VecView() is just writing out
            the vector as a linear array without mesh details. For</div>
          <div>interfacing with the visualization, I think we use .vtu
            files. You should be able to get this effect using</div>
          <div><br>
          </div>
          <div>  VecViewFromOptions(global_vec, NULL, "-vec_view");</div>
          <div><br>
          </div>
          <div>in your code, and then</div>
          <div><br>
          </div>
          <div>  -vec_view vtk:sol.vtu</div>
          <div><br>
          </div>
          <div>on the command line.</div>
        </div>
      </div>
    </blockquote>
    <p>Hi.</p>
    <p>If you want to stick with HDF5, you can also write a XDMF file
      with the grid information and open that in Paraview.<br>
    </p>
    <p>I am attaching some routines that I have written to do that in a
      solver that deals with a time dependent PDE system with 2
      variables; with them I end up with a single XDMF file that
      Paraview can load and which contains references to all timesteps
      in my simulations, with each timestep being contained in an HDF5
      file on its own. The idea is to call writeDomain at the beginning
      of the simulation, writeHDF5 for each timestep that I want to save
      and writeSimulationXDMF at the end. (Warning: 3D is in use, while
      2D ia almost untested...)<br>
    </p>
    <p>It's not the optimal solution since (1) all timesteps could be in
      the same HDF5 and (2) in each HDF5 i write the vectors separately
      and it would be better to dump the entire data in one go and
      interpret them as a Nx*Ny*Nz*Nvariables data from the XDMF.
      Nevertheless they might be a starting point for you if you wan to
      try this approach.<br>
    </p>
    <p></p></div></blockquote><div>You can have HDF5 put the vectors in a single array with a time dimension now. Then you just alter the xdmf to point into that array. I do this</div><div>with the unstructured code.</div><div><br></div><div>  Thanks,</div><div><br></div><div>     Matt</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><p>Matteo<br>
    </p>
    <blockquote type="cite">
      <div dir="ltr">
        <div class="gmail_quote">
          <div> </div>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
            <div dir="ltr">
              <div>3) I tried using the "petsc_gen_xdmf.py" to generate
                the xdmf files for use in Paraview. Here the key
                ["viz/geometry"] is missing. The keys present in the
                output H5 file are just the two vectors I am writing and
                has no info about mesh. Isn't this supposed to come
                automatically since the vector is attached to the DM?
                How do I sort this out?</div>
            </div>
          </blockquote>
          <div><br>
          </div>
          <div>This support is for unstructured grids, DMPlex and
            DMForest.</div>
          <div> </div>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
            <div dir="ltr">
              <div>4) Can I have multiple vectors attached to the DM by
                DMCreateGlobalVector() even though I created the DMDA
                using dof=1.</div>
            </div>
          </blockquote>
          <div><br>
          </div>
          <div>Yes.</div>
          <div><br>
          </div>
          <div>  Thanks,</div>
          <div><br>
          </div>
          <div>     Matt</div>
          <div> </div>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
            <div dir="ltr">
              <div>thanks,<br>
              </div>
              <div>Abhishek<br>
              </div>
            </div>
          </blockquote>
        </div>
        <br clear="all">
        <div><br>
        </div>
        -- <br>
        <div dir="ltr">
          <div dir="ltr">
            <div>
              <div dir="ltr">
                <div>
                  <div dir="ltr">
                    <div>What most experimenters take for granted before
                      they begin their experiments is infinitely more
                      interesting than any results to which their
                      experiments lead.<br>
                      -- Norbert Wiener</div>
                    <div><br>
                    </div>
                    <div><a href="https://eur01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.cse.buffalo.edu%2F~knepley%2F&data=04%7C01%7Cmatteo.semplice%40uninsubria.it%7Cf27a34f639784d0231c708d98f0f7279%7C9252ed8bdffc401c86ca6237da9991fa%7C0%7C0%7C637698118789688898%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=Xb8nyv7BIl6zSBbbLFkQzluIF8s%2BOLwUQeVfjtr3q4k%3D&reserved=0" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
    <pre cols="72">-- 
---
Professore Associato in Analisi Numerica
Dipartimento di Scienza e Alta Tecnologia
Università degli Studi dell'Insubria
Via Valleggio, 11 - Como</pre>
  </div>

</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div>
</blockquote></div>
</blockquote></div>