|
| 1 | +#include "myfunc.H" |
| 2 | + |
| 3 | +namespace { |
| 4 | + void GotoNextLine (std::istream& is) |
| 5 | + { |
| 6 | + constexpr std::streamsize bl_ignore_max { 100000 }; |
| 7 | + is.ignore(bl_ignore_max, '\n'); |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +// create a checkpoint directory |
| 12 | +// write out time and BoxArray to a Header file |
| 13 | +// write out multifab data |
| 14 | +void WriteCheckpoint(const int& step, |
| 15 | + const Real& time, |
| 16 | + const MultiFab& phi) |
| 17 | +{ |
| 18 | + |
| 19 | + |
| 20 | + // checkpoint file name, e.g., chk00010 |
| 21 | + const std::string& checkpointname = Concatenate("chk",step,5); |
| 22 | + |
| 23 | + BoxArray ba = phi.boxArray(); |
| 24 | + |
| 25 | + // single level problem |
| 26 | + int nlevels = 1; |
| 27 | + |
| 28 | + // ---- prebuild a hierarchy of directories |
| 29 | + // ---- dirName is built first. if dirName exists, it is renamed. then build |
| 30 | + // ---- dirName/subDirPrefix_0 .. dirName/subDirPrefix_nlevels-1 |
| 31 | + // ---- if callBarrier is true, call ParallelDescriptor::Barrier() |
| 32 | + // ---- after all directories are built |
| 33 | + // ---- ParallelDescriptor::IOProcessor() creates the directories |
| 34 | + PreBuildDirectorHierarchy(checkpointname, "Level_", nlevels, true); |
| 35 | + |
| 36 | + VisMF::IO_Buffer io_buffer(VisMF::IO_Buffer_Size); |
| 37 | + |
| 38 | + // write Header file to store time and BoxArray |
| 39 | + if (ParallelDescriptor::IOProcessor()) { |
| 40 | + |
| 41 | + std::ofstream HeaderFile; |
| 42 | + HeaderFile.rdbuf()->pubsetbuf(io_buffer.dataPtr(), io_buffer.size()); |
| 43 | + std::string HeaderFileName(checkpointname + "/Header"); |
| 44 | + HeaderFile.open(HeaderFileName.c_str(), std::ofstream::out | |
| 45 | + std::ofstream::trunc | |
| 46 | + std::ofstream::binary); |
| 47 | + |
| 48 | + if( !HeaderFile.good()) { |
| 49 | + FileOpenFailed(HeaderFileName); |
| 50 | + } |
| 51 | + |
| 52 | + HeaderFile.precision(17); |
| 53 | + |
| 54 | + // write out title line |
| 55 | + HeaderFile << "Checkpoint file for MagneX\n"; |
| 56 | + |
| 57 | + // write out time |
| 58 | + HeaderFile << time << "\n"; |
| 59 | + |
| 60 | + // write the BoxArray |
| 61 | + ba.writeOn(HeaderFile); |
| 62 | + HeaderFile << '\n'; |
| 63 | + } |
| 64 | + |
| 65 | + // write the MultiFab data to, e.g., chk00010/Level_0/ |
| 66 | + VisMF::Write(phi, MultiFabFileFullPrefix(0, checkpointname, "Level_", "phi")); |
| 67 | +} |
| 68 | + |
| 69 | +// read in the time and BoxArray, then create a DistributionMapping |
| 70 | +// Define phi and fill it with data from the checkpoint file |
| 71 | +void ReadCheckpoint(const int& restart, |
| 72 | + Real& time, |
| 73 | + MultiFab& phi, |
| 74 | + BoxArray& ba, |
| 75 | + DistributionMapping& dm) |
| 76 | +{ |
| 77 | + |
| 78 | + // checkpoint file name, e.g., chk00010 |
| 79 | + const std::string& checkpointname = Concatenate("chk",restart,5); |
| 80 | + |
| 81 | + Print() << "Restart from checkpoint " << checkpointname << "\n"; |
| 82 | + |
| 83 | + VisMF::IO_Buffer io_buffer(VisMF::GetIOBufferSize()); |
| 84 | + |
| 85 | + std::string line, word; |
| 86 | + |
| 87 | + // Header |
| 88 | + { |
| 89 | + std::string File(checkpointname + "/Header"); |
| 90 | + Vector<char> fileCharPtr; |
| 91 | + ParallelDescriptor::ReadAndBcastFile(File, fileCharPtr); |
| 92 | + std::string fileCharPtrString(fileCharPtr.dataPtr()); |
| 93 | + std::istringstream is(fileCharPtrString, std::istringstream::in); |
| 94 | + |
| 95 | + // read in title line |
| 96 | + std::getline(is, line); |
| 97 | + |
| 98 | + // read in time |
| 99 | + is >> time; |
| 100 | + GotoNextLine(is); |
| 101 | + |
| 102 | + // read in BoxArray from Header |
| 103 | + ba.readFrom(is); |
| 104 | + GotoNextLine(is); |
| 105 | + |
| 106 | + // create a distribution mapping |
| 107 | + dm.define(ba, ParallelDescriptor::NProcs()); |
| 108 | + |
| 109 | + // Nghost = number of ghost cells for each array |
| 110 | + int Nghost = 1; |
| 111 | + |
| 112 | + // Ncomp = number of components for each array |
| 113 | + int Ncomp = 1; |
| 114 | + |
| 115 | + phi.define(ba, dm, Ncomp, Nghost); |
| 116 | + } |
| 117 | + |
| 118 | + // read in the MultiFab data |
| 119 | + VisMF::Read(phi, MultiFabFileFullPrefix(0, checkpointname, "Level_", "phi")); |
| 120 | + |
| 121 | +} |
0 commit comments