ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_IBSEBMaterials.cpp File Reference

CSV reader of the face material library (rank-0 read, broadcast). More...

#include "ERF_IBSEBMaterials.H"
#include <AMReX.H>
#include <AMReX_ParallelDescriptor.H>
#include <fstream>
#include <sstream>
#include <set>
#include <cstring>
#include <algorithm>
Include dependency graph for ERF_IBSEBMaterials.cpp:

Functions

Vector< IBSEBMaterialread_ibseb_materials (const std::string &file)
 

Detailed Description

CSV reader of the face material library (rank-0 read, broadcast).

Function Documentation

◆ read_ibseb_materials()

Vector<IBSEBMaterial> read_ibseb_materials ( const std::string &  file)

Read the material CSV on the I/O rank and broadcast it. Aborts on a missing file, a header that does not match the schema, a duplicate id, or a value outside its physical range.

26 {
27  static_assert(std::is_trivially_copyable<IBSEBMaterial>::value, "IBSEBMaterial must be broadcastable");
28  Vector<IBSEBMaterial> table;
29  int n = 0;
30  if (ParallelDescriptor::IOProcessor()) {
31  std::ifstream in(file);
32  if (!in.good()) { Abort("erf.ibseb.material_file: cannot open '" + file + "'"); }
33  std::string line;
34  if (!std::getline(in, line)) { Abort("erf.ibseb.material_file: empty file '" + file + "'"); }
35  if (line.size() >= 3 && static_cast<unsigned char>(line[0]) == 0xEF) { line.erase(0, 3); }
36  const std::string expected = "mat_id,name,albedo,emissivity,k_therm_W_per_mK,rho_cp_J_per_m3K,thickness_m,description";
37  if (strip(line) != expected) {
38  Abort("erf.ibseb.material_file: header must be '" + expected + "', got '" + line + "'");
39  }
40  std::set<int> seen;
41  while (std::getline(in, line)) {
42  if (strip(line).empty() || line[0] == '#') { continue; }
43  std::stringstream ss(line);
44  std::string f;
45  IBSEBMaterial m;
46  std::getline(ss, f, ','); m.mat_id = std::stoi(strip(f));
47  std::getline(ss, f, ','); f = strip(f); std::strncpy(m.name, f.c_str(), sizeof(m.name) - 1); m.name[sizeof(m.name) - 1] = '\0';
48  std::getline(ss, f, ','); m.albedo = std::stod(strip(f));
49  std::getline(ss, f, ','); m.emissivity = std::stod(strip(f));
50  std::getline(ss, f, ','); m.k_therm = std::stod(strip(f));
51  std::getline(ss, f, ','); m.rho_cp = std::stod(strip(f));
52  std::getline(ss, f, ','); m.thickness = std::stod(strip(f));
53  if (!seen.insert(m.mat_id).second) { Abort("erf.ibseb.material_file: duplicate mat_id " + std::to_string(m.mat_id)); }
54  if (m.albedo < 0.0 || m.albedo > 1.0 || m.emissivity <= 0.0 || m.emissivity > 1.0 ||
55  m.k_therm <= 0.0 || m.rho_cp <= 0.0 || m.thickness <= 0.0) {
56  Abort("erf.ibseb.material_file: material " + std::to_string(m.mat_id) + " has a value outside its range");
57  }
58  table.push_back(m);
59  }
60  n = static_cast<int>(table.size());
61  if (n == 0) { Abort("erf.ibseb.material_file: no materials in '" + file + "'"); }
62  }
63  ParallelDescriptor::Bcast(&n, 1, ParallelDescriptor::IOProcessorNumber());
64  table.resize(n);
65  ParallelDescriptor::Bcast(reinterpret_cast<char*>(table.data()), n * sizeof(IBSEBMaterial),
66  ParallelDescriptor::IOProcessorNumber());
67  return table;
68 }
Definition: ERF_IBSEBMaterials.H:24
amrex::Real albedo
Definition: ERF_IBSEBMaterials.H:26
amrex::Real k_therm
[W/m/K]
Definition: ERF_IBSEBMaterials.H:28
char name[64]
Definition: ERF_IBSEBMaterials.H:31
amrex::Real thickness
[m]
Definition: ERF_IBSEBMaterials.H:30
int mat_id
Definition: ERF_IBSEBMaterials.H:25
amrex::Real rho_cp
[J/m3/K]
Definition: ERF_IBSEBMaterials.H:29
amrex::Real emissivity
Definition: ERF_IBSEBMaterials.H:27

Referenced by IBFaceSet::assign_materials().

Here is the caller graph for this function: