Skip to content

Commit

Permalink
Add a sampling info file (Exawind#1387)
Browse files Browse the repository at this point in the history
* Add a sampling info file

* add doc

* add parsing of info to the amrex particle reader

* tweak doc

* linting
  • Loading branch information
marchdf authored Dec 5, 2024
1 parent 8a7f71f commit 6f4e014
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions amr-wind/utilities/sampling/Sampling.H
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ protected:
*/
virtual void write_ascii();

//! Output extra information for certain formats
void write_info_file(const std::string& fname);

SamplingContainer& sampling_container() { return *m_scontainer; }

private:
Expand Down
29 changes: 29 additions & 0 deletions amr-wind/utilities/sampling/Sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ void Sampling::impl_write_native()
const SamplingContainer::SuperParticleType& p) {
return p.id() > 0;
});

const std::string info_name = name + "/sampling_info";
write_info_file(info_name);
}

void Sampling::write_ascii()
Expand All @@ -413,6 +416,32 @@ void Sampling::write_ascii()

const std::string fname = post_dir + "/" + sname + ".txt";
m_scontainer->WriteAsciiFile(fname);

const std::string info_name = post_dir + "/" + sname + "_info.txt";
write_info_file(info_name);
}

void Sampling::write_info_file(const std::string& fname)
{
BL_PROFILE("amr-wind::Sampling::write_info_file");

// Only I/O processor writes the info file
if (!amrex::ParallelDescriptor::IOProcessor()) {
return;
}

if ((m_out_fmt != "native") && (m_out_fmt != "ascii")) {
amrex::Abort(
"write_info_file is implemented only for native and ascii formats");
}

std::ofstream fh(fname.c_str(), std::ios::out);
if (!fh.good()) {
amrex::FileOpenFailed(fname);
}

fh << "time " << m_sim.time().new_time() << std::endl;
fh.close();
}

void Sampling::prepare_netcdf_file()
Expand Down
6 changes: 5 additions & 1 deletion docs/sphinx/user/inputs_Sampling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ where ``uid`` is the global probe id, ``set_id`` is the label id
input order), ``probe_id`` is the local probe id to this label,
``*co`` are the coordinates of the probe, and the other columns are
the user requested sampled fields. The same labels are seeing by other
visualization tools such as ParaView.
visualization tools such as ParaView. The directory also contains a
``sampling_info`` file where additional information (e.g., time) is
stored. This file is automatically parse by the provided particle
reader tool and the information is stored in a dictionary that is a
member variable of the class.

Sampling along a line
``````````````````````
Expand Down
9 changes: 9 additions & 0 deletions tools/amrex_particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __call__(self):
"""
if not hasattr(self, "df"):
self.parse_header()
self.parse_info()
self.load_binary_data()
return self.df

Expand Down Expand Up @@ -79,6 +80,14 @@ def parse_header(self):
[int(ix) for ix in fh.readline().strip().split()])
self.grid_info.append(ginfo)

def parse_info(self):
"""Parse the sampling info file"""
self.info = {}
with open(self.pdir.parent / "sampling_info", 'r') as fh:
for line in fh:
(key, val) = line.split()
self.info[key] = float(val)

def load_binary_data(self):
"""Read binary data into memory"""
self.real_data = np.empty((self.num_particles,
Expand Down
7 changes: 5 additions & 2 deletions tools/fcompare_particles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ def main():
assert Path(args.f0).is_dir()
assert Path(args.f1).is_dir()

p0df = AmrexParticleFile(Path(args.f0) / "particles")()
p1df = AmrexParticleFile(Path(args.f1) / "particles")()
p0f = AmrexParticleFile(Path(args.f0) / "particles")
p1f = AmrexParticleFile(Path(args.f1) / "particles")
p0df = p0f()
p1df = p1f()
assert np.abs(p0f.info["time"] - p1f.info["time"]) <= args.abs_tol
assert p0df.shape == p1df.shape
p0df.sort_values(by=["uid"], inplace=True, kind="stable", ignore_index=True)
p1df.sort_values(by=["uid"], inplace=True, kind="stable", ignore_index=True)
Expand Down

0 comments on commit 6f4e014

Please sign in to comment.