https://mooseframework.inl.gov
SolutionRasterizer.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "SolutionRasterizer.h"
11 
12 #include <fstream>
13 #include "libmesh/mesh_function.h"
14 #include "libmesh/exodusII_io.h"
15 
16 registerMooseObject("PhaseFieldApp", SolutionRasterizer);
17 
20 {
22  params.addClassDescription("Process an XYZ file of atomic coordinates and filter atoms via "
23  "threshold or map variable values.");
24  params.addRequiredParam<FileName>("xyz_input", "XYZ input file.");
25  params.addRequiredParam<FileName>("xyz_output", "XYZ output file.");
26  params.addRequiredParam<std::string>(
27  "variable", "Variable from the mesh file to use for mapping to or filtering of the atoms.");
28  MooseEnum modeEnum("MAP FILTER", "MAP");
29  params.addParam<MooseEnum>("raster_mode", modeEnum, "Rasterization mode (MAP|FILTER).");
30  params.addParam<Real>("threshold",
31  "Accept atoms with a variable value above this threshold in FILTER mode.");
32  return params;
33 }
34 
36  : SolutionUserObject(parameters),
37  _xyz_input(getParam<FileName>("xyz_input")),
38  _xyz_output(getParam<FileName>("xyz_output")),
39  _variable(getParam<std::string>("variable")),
40  _raster_mode(getParam<MooseEnum>("raster_mode")),
41  _threshold(0.0)
42 {
43  if (_raster_mode == "FILTER")
44  {
45  if (!isParamValid("threshold"))
46  mooseError("Please specify 'threshold' parameter for raster_mode = FILTER");
47  _threshold = getParam<Real>("threshold");
48  }
49 }
50 
51 void
53 {
54  // only execute once
55  if (_initialized)
56  return;
57 
58  // initialize parent class
60 
61  // open input XYZ file
62  std::ifstream stream_in(_xyz_input.c_str());
63 
64  // open output XYZ file
65  std::ofstream stream_out(_xyz_output.c_str());
66 
67  std::string line, dummy;
68  Real x, y, z;
69  unsigned int current_line = 0;
70  unsigned int nfilter = 0, len0 = 0;
71  while (std::getline(stream_in, line))
72  {
73  if (current_line < 2)
74  {
75  // dump header
76  stream_out << line << '\n';
77 
78  // get length of line 0 - the amount of space we have to replace the atom count at the end of
79  // filtering
80  if (current_line == 0)
81  len0 = line.size();
82  }
83  else
84  {
85  std::istringstream iss(line);
86 
87  if (iss >> dummy >> x >> y >> z)
88  switch (_raster_mode)
89  {
90  case 0: // MAP
91  stream_out << line << ' ' << pointValue(0.0, Point(x, y, z), _variable) << '\n';
92  break;
93  case 1: // FILTER
94  if (pointValue(0.0, Point(x, y, z), _variable) > _threshold)
95  {
96  stream_out << line << '\n';
97  nfilter++;
98  }
99  break;
100  }
101  }
102 
103  current_line++;
104  }
105 
106  stream_in.close();
107  stream_out.close();
108 
109  // modify output file to fix atom count in line 0
110  if (_raster_mode == "FILTER")
111  {
112  // stringify the new number of atoms
113  std::ostringstream oss;
114  oss << nfilter;
115  std::string newline0 = oss.str();
116 
117  // the new number should always be lower -> shorter than the old one, but we check to be sure
118  if (newline0.size() > len0)
119  {
120  mooseWarning("SolutionRasterizer could not update XYZ atom count in header.");
121  return;
122  }
123 
124  // pad shorter numbers with spaces
125  while (newline0.size() < len0)
126  newline0 += ' ';
127 
128  // inject new number into the file
129  std::ofstream stream_fix(_xyz_output.c_str(), std::ios::binary | std::ios::in | std::ios::out);
130  stream_fix << newline0;
131  stream_fix.close();
132  }
133 }
virtual void initialSetup()
Initialize the System and Mesh objects for the solution being read.
virtual void initialSetup() override
static InputParameters validParams()
SolutionRasterizer(const InputParameters &parameters)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
const std::vector< double > y
void mooseWarning(Args &&... args) const
void addRequiredParam(const std::string &name, const std::string &doc_string)
bool isParamValid(const std::string &name) const
const std::vector< double > x
static InputParameters validParams()
registerMooseObject("PhaseFieldApp", SolutionRasterizer)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
This Userobject is the base class of Userobjects that generate one random number per timestep and qua...
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
Real pointValue(Real t, const Point &p, const unsigned int local_var_index, const std::set< subdomain_id_type > *subdomain_ids=nullptr) const