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