www.mooseframework.org
DiscreteNucleationFromFile.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 
11 #include "MooseMesh.h"
12 
13 #include <algorithm>
14 
16 
17 template <>
18 InputParameters
20 {
21  InputParameters params = validParams<DiscreteNucleationInserterBase>();
22  params.addClassDescription(
23  "Manages the list of currently active nucleation sites and adds new "
24  "sites according to a predetermined list from a CSV file (use this with sync_times).");
25  params.addRequiredParam<FileName>(
26  "file", "CSV file with (time, x, y, z) coordinates for nucleation events.");
27  params.addRequiredParam<Real>("hold_time", "Time to keep each nucleus active");
28  params.addParam<Real>("tolerance", 1e-9, "Tolerance for determining insertion time");
29  return params;
30 }
31 
32 DiscreteNucleationFromFile::DiscreteNucleationFromFile(const InputParameters & parameters)
33  : DiscreteNucleationInserterBase(parameters),
34  _hold_time(getParam<Real>("hold_time")),
35  _reader(getParam<FileName>("file")),
36  _history_pointer(0),
37  _tol(getParam<Real>("tolerance")),
38  _nucleation_rate(0.0)
39 {
40  _reader.read();
41 
42  auto & names = _reader.getNames();
43  auto & data = _reader.getData();
44 
45  const std::size_t rows = data[0].size();
46  _nucleation_history.resize(rows);
47 
48  bool found_time = false;
49  bool found_x = false;
50  bool found_y = false;
51  bool found_z = false;
52 
53  for (std::size_t i = 0; i < names.size(); ++i)
54  {
55  // make sure all data columns have the same length
56  if (data[i].size() != rows)
57  paramError("file", "Mismatching column lengths in file");
58 
59  if (names[i] == "time")
60  {
61  for (std::size_t j = 0; j < rows; ++j)
62  _nucleation_history[j].first = data[i][j];
63  found_time = true;
64  }
65  else if (names[i] == "x")
66  {
67  for (std::size_t j = 0; j < rows; ++j)
68  _nucleation_history[j].second(0) = data[i][j];
69  found_x = true;
70  }
71  else if (names[i] == "y")
72  {
73  for (std::size_t j = 0; j < rows; ++j)
74  _nucleation_history[j].second(1) = data[i][j];
75  found_y = true;
76  }
77  else if (names[i] == "z")
78  {
79  for (std::size_t j = 0; j < rows; ++j)
80  _nucleation_history[j].second(2) = data[i][j];
81  found_z = true;
82  }
83  }
84 
85  // check if all required columns were found
86  if (!found_time)
87  paramError("file", "Missing 'time' column in file");
88  if (!found_x)
89  paramError("file", "Missing 'x' column in file");
90  if (!found_y && _mesh.dimension() >= 2)
91  paramError("file", "Missing 'y' column in file");
92  if (!found_z && _mesh.dimension() >= 3)
93  paramError("file", "Missing 'z' column in file");
94 
95  // sort the nucleation history primarily according to time
96  std::sort(_nucleation_history.begin(), _nucleation_history.end());
97 }
98 
99 void
101 {
102  // clear insertion and deletion counter
103  _changes_made = {0, 0};
104 
105  // expire entries from the local nucleus list (if the current time step converged)
106  if (_fe_problem.converged())
107  {
108  unsigned int i = 0;
109  while (i < _global_nucleus_list.size())
110  {
111  if (_global_nucleus_list[i].first - _tol <= _fe_problem.time())
112  {
113  // remove entry (by replacing with last element and shrinking size by one)
115  _global_nucleus_list.pop_back();
116  _changes_made.second++;
117  }
118  else
119  ++i;
120  }
121  }
122 
123  // check if it is time to insert from the nucleus history
124  while (_history_pointer < _nucleation_history.size() &&
125  _nucleation_history[_history_pointer].first <= _fe_problem.time())
126  {
127  _global_nucleus_list.push_back(
130  _changes_made.first++;
132  }
133 }
134 
135 void
137 {
138  // no communication necessary as all ranks have the full nucleus history from the
139  // DelimitedFileReader
140  _update_required = _changes_made.first > 0 || _changes_made.second > 0;
141 }
validParams< DiscreteNucleationInserterBase >
InputParameters validParams< DiscreteNucleationInserterBase >()
Definition: DiscreteNucleationInserterBase.C:14
DiscreteNucleationFromFile::finalize
void finalize() override
Definition: DiscreteNucleationFromFile.C:136
DiscreteNucleationFromFile::_hold_time
const Real _hold_time
Duration of time each nucleus is kept active after insertion.
Definition: DiscreteNucleationFromFile.h:40
DiscreteNucleationFromFile::_history_pointer
std::size_t _history_pointer
pointer to the next nucleation event in the history
Definition: DiscreteNucleationFromFile.h:49
DiscreteNucleationFromFile::_nucleation_history
NucleusList _nucleation_history
Total nucleation history read from file.
Definition: DiscreteNucleationFromFile.h:46
DiscreteNucleationInserterBase
This UserObject manages the insertion and expiration of nuclei in the simulation domain it manages a ...
Definition: DiscreteNucleationInserterBase.h:25
DiscreteNucleationFromFile
This UserObject manages the insertion and expiration of nuclei in the simulation domain it manages a ...
Definition: DiscreteNucleationFromFile.h:26
validParams< DiscreteNucleationFromFile >
InputParameters validParams< DiscreteNucleationFromFile >()
Definition: DiscreteNucleationFromFile.C:19
DiscreteNucleationFromFile::_reader
MooseUtils::DelimitedFileReader _reader
CSV file to read.
Definition: DiscreteNucleationFromFile.h:43
DiscreteNucleationInserterBase::_global_nucleus_list
NucleusList & _global_nucleus_list
the global list of all nuclei over all processors
Definition: DiscreteNucleationInserterBase.h:47
DiscreteNucleationFromFile::DiscreteNucleationFromFile
DiscreteNucleationFromFile(const InputParameters &parameters)
Definition: DiscreteNucleationFromFile.C:32
DiscreteNucleationInserterBase::_update_required
bool _update_required
is a map update required
Definition: DiscreteNucleationInserterBase.h:53
registerMooseObject
registerMooseObject("PhaseFieldApp", DiscreteNucleationFromFile)
DiscreteNucleationFromFile::_tol
const Real _tol
tolerance for determining insertion time
Definition: DiscreteNucleationFromFile.h:52
DiscreteNucleationFromFile::initialize
void initialize() override
Definition: DiscreteNucleationFromFile.C:100
DiscreteNucleationInserterBase::NucleusLocation
std::pair< Real, Point > NucleusLocation
A nucleus has an expiration time and a location.
Definition: DiscreteNucleationInserterBase.h:31
DiscreteNucleationFromFile.h
DiscreteNucleationInserterBase::_changes_made
NucleusChanges _changes_made
count the number of nucleus insertions and deletions
Definition: DiscreteNucleationInserterBase.h:50