www.mooseframework.org
SolutionRasterizer.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
7 
8 #include "SolutionRasterizer.h"
9 
10 #include <fstream>
11 
12 template <>
13 InputParameters
15 {
16  InputParameters params = validParams<SolutionUserObject>();
17  params.addClassDescription("Process an XYZ file of atomic coordinates and filter atoms via "
18  "threshold or map variable values.");
19  params.addRequiredParam<FileName>("xyz_input", "XYZ input file.");
20  params.addRequiredParam<FileName>("xyz_output", "XYZ output file.");
21  params.addRequiredParam<std::string>(
22  "variable", "Variable from the mesh file to use for mapping to or filtering of the atoms.");
23  MooseEnum modeEnum("MAP FILTER", "MAP");
24  params.addParam<MooseEnum>("raster_mode", modeEnum, "Rasterization mode (MAP|FILTER).");
25  params.addParam<Real>("threshold",
26  "Accept atoms with a variable value above this threshold in FILTER mode.");
27  return params;
28 }
29 
30 SolutionRasterizer::SolutionRasterizer(const InputParameters & parameters)
31  : SolutionUserObject(parameters),
32  _xyz_input(getParam<FileName>("xyz_input")),
33  _xyz_output(getParam<FileName>("xyz_output")),
34  _variable(getParam<std::string>("variable")),
35  _raster_mode(getParam<MooseEnum>("raster_mode")),
36  _threshold(0.0)
37 {
38  if (_raster_mode == "FILTER")
39  {
40  if (!isParamValid("threshold"))
41  mooseError("Please specify 'threshold' parameter for raster_mode = FILTER");
42  _threshold = getParam<Real>("threshold");
43  }
44 }
45 
46 void
48 {
49  // only execute once
50  if (_initialized)
51  return;
52 
53  // initialize parent class
54  SolutionUserObject::initialSetup();
55 
56  // open input XYZ file
57  std::ifstream stream_in(_xyz_input.c_str());
58 
59  // open output XYZ file
60  std::ofstream stream_out(_xyz_output.c_str());
61 
62  std::string line, dummy;
63  Real x, y, z;
64  unsigned int current_line = 0;
65  unsigned int nfilter = 0, len0 = 0;
66  while (std::getline(stream_in, line))
67  {
68  if (current_line < 2)
69  {
70  // dump header
71  stream_out << line << '\n';
72 
73  // get length of line 0 - the amount of space we have to replace the atom count at the end of
74  // filtering
75  if (current_line == 0)
76  len0 = line.size();
77  }
78  else
79  {
80  std::istringstream iss(line);
81 
82  if (iss >> dummy >> x >> y >> z)
83  switch (_raster_mode)
84  {
85  case 0: // MAP
86  stream_out << line << ' ' << pointValue(0.0, Point(x, y, z), _variable) << '\n';
87  break;
88  case 1: // FILTER
89  if (pointValue(0.0, Point(x, y, z), _variable) > _threshold)
90  {
91  stream_out << line << '\n';
92  nfilter++;
93  }
94  break;
95  }
96  }
97 
98  current_line++;
99  }
100 
101  stream_in.close();
102  stream_out.close();
103 
104  // modify output file to fix atom count in line 0
105  if (_raster_mode == "FILTER")
106  {
107  // stringify the new number of atoms
108  std::ostringstream oss;
109  oss << nfilter;
110  std::string newline0 = oss.str();
111 
112  // the new number should always be lower -> shorter than the old one, but we check to be sure
113  if (newline0.size() > len0)
114  {
115  mooseWarning("SolutionRasterizer could not update XYZ atom count in header.");
116  return;
117  }
118 
119  // pad shorter numbers with spaces
120  while (newline0.size() < len0)
121  newline0 += ' ';
122 
123  // inject new number into the file
124  std::ofstream stream_fix(_xyz_output.c_str(), std::ios::binary | std::ios::in | std::ios::out);
125  stream_fix << newline0;
126  stream_fix.close();
127  }
128 }
virtual void initialSetup()
Initialize the System and Mesh objects for the solution being read.
SolutionRasterizer(const InputParameters &parameters)
InputParameters validParams< SolutionRasterizer >()