cs205-lecture-examples

Example codes used during Harvard CS205 lectures
git clone https://git.0xfab.ch/cs205-lecture-examples.git
Log | Files | Refs | README | LICENSE

xdmf_wrapper.h (2318B)


      1 // File       : xdmf_wrapper.h
      2 // Description: XDMF wrapper for HDF5 file
      3 //              https://www.xdmf.org/index.php/XDMF_Model_and_Format
      4 // Copyright 2023 Harvard University. All Rights Reserved.
      5 #ifndef XDMF_WRAPPER_H_9PHDQVEM
      6 #define XDMF_WRAPPER_H_9PHDQVEM
      7 
      8 #include <cstdio>
      9 #include <string>
     10 
     11 void xdmf_wrapper(const std::string &fname,
     12                   const unsigned long long file_dims[])
     13 {
     14     const double dx =
     15         1.0 / std::max(file_dims[0], std::max(file_dims[1], file_dims[2]));
     16     FILE *xmf = 0;
     17     xmf = fopen((fname + ".xmf").c_str(), "w");
     18     // clang-format off
     19         fprintf(xmf, "<?xml version=\"1.0\" ?>\n");
     20         fprintf(xmf, "<!DOCTYPE Xdmf SYSTEM \"Xdmf.dtd\" []>\n");
     21         fprintf(xmf, "<Xdmf Version=\"2.0\">\n");
     22         fprintf(xmf, "  <Domain>\n");
     23         fprintf(xmf, "    <Grid GridType=\"Uniform\">\n");
     24         fprintf(xmf, "      <Time Value=\"00000\"/>\n");
     25         fprintf(xmf, "      <Topology TopologyType=\"3DRECTMESH\" Dimensions =\"%llu %llu %llu\"/>\n", file_dims[0] + 1, file_dims[1] + 1, file_dims[2] + 1);
     26         fprintf(xmf, "      <Geometry GeometryType =\"ORIGIN_DXDYDZ\">\n");
     27         fprintf(xmf, "        <DataItem Name =\"Origin\" Dimensions=\"3\" NumberType=\"Float\" Precision =\"8\" Format=\"XML\">\n");
     28         fprintf(xmf, "          0.000000e+00 0.000000e+00 0.000000e+00\n");
     29         fprintf(xmf, "        </DataItem>\n");
     30         fprintf(xmf, "        <DataItem Name =\"Spacing\" Dimensions=\"3\" NumberType=\"Float\" Precision =\"8\" Format=\"XML\">\n");
     31         fprintf(xmf, "          %e %e %e\n", dx, dx, dx);
     32         fprintf(xmf, "        </DataItem>\n");
     33         fprintf(xmf, "      </Geometry>\n");
     34         fprintf(xmf, "      <Attribute Name=\"data\" AttributeType=\"Scalar\" Center=\"Cell\">\n");
     35         fprintf(xmf, "        <DataItem Dimensions =\"%llu %llu %llu\" NumberType=\"Float\" Precision=\"4\" Format =\"HDF\">\n", file_dims[0], file_dims[1], file_dims[2]);
     36         fprintf(xmf, "          %s:/u\n", (fname + ".h5").c_str());
     37         fprintf(xmf, "        </DataItem>\n"); 
     38         fprintf(xmf, "      </Attribute>\n");
     39         fprintf(xmf, "    </Grid>\n");
     40         fprintf(xmf, "  </Domain>\n");
     41         fprintf(xmf, "</Xdmf>\n");
     42     // clang-format on
     43     fclose(xmf);
     44 }
     45 
     46 #endif /* XDMF_WRAPPER_H_9PHDQVEM */