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