Skip to content
Permalink
Browse files

Merge pull request #12935 from permcody/dyanmic_point_sampler

Dynamic point sampler
  • Loading branch information...
lindsayad committed Feb 19, 2019
2 parents 47cda10 + 0ed0d2d commit 7d4ae5d3e12404c26f681282c6cfe1ada678e079
@@ -66,7 +66,7 @@ class MultiAppTransfer : public Transfer
std::shared_ptr<MultiApp> _multi_app;

/// Whether we're transferring to or from the MultiApp
MooseEnum _direction;
const MooseEnum _direction;

/**
* This method will fill information into the convenience member variables
@@ -52,6 +52,11 @@ class LineValueSampler : public PointSamplerBase
Real getValue(Point p) const;

protected:
const Point _start_point;
const Point _end_point;

unsigned int & _num_points;

/// Vector connecting the start and end points of the line segment
const RealVectorValue _line_vector;

@@ -36,6 +36,9 @@ class PointSamplerBase : public GeneralVectorPostprocessor,
virtual void execute();
virtual void finalize();

void setPointsVector(const std::vector<Point> & points);
void transferPointsVector(std::vector<Point> && points);

protected:
/**
* Find the local element that contains the point. This will attempt to use a cached element to
@@ -23,6 +23,8 @@ class PointValueSampler : public PointSamplerBase
{
public:
PointValueSampler(const InputParameters & parameters);

virtual void initialize() override;
};

#endif
@@ -30,18 +30,17 @@ validParams<LineValueSampler>()

LineValueSampler::LineValueSampler(const InputParameters & parameters)
: PointSamplerBase(parameters),
_line_vector(getParam<Point>("end_point") - getParam<Point>("start_point")),
_start_point(getParam<Point>("start_point")),
_end_point(getParam<Point>("end_point")),
_num_points(
declareRestartableData<unsigned int>("num_points", getParam<unsigned int>("num_points"))),
_line_vector(_end_point - _start_point),
_line_vector_norm(_line_vector.norm())
{
Point start_point = getParam<Point>("start_point");
Point end_point = getParam<Point>("end_point");

if (MooseUtils::absoluteFuzzyEqual(_line_vector_norm, 0.0))
mooseError("LineValueSampler: `start_point` and `end_point` must be different.");

unsigned int num_points = getParam<unsigned int>("num_points");

generatePointsAndIDs(start_point, end_point, num_points, _points, _ids);
generatePointsAndIDs(_start_point, _end_point, _num_points, _points, _ids);
}

void
@@ -144,6 +144,18 @@ PointSamplerBase::finalize()
SamplerBase::finalize();
}

void
PointSamplerBase::setPointsVector(const std::vector<Point> & points)
{
_points = points;
}

void
PointSamplerBase::transferPointsVector(std::vector<Point> && points)
{
_points = std::move(points);
}

const Elem *
PointSamplerBase::getLocalElemContainingPoint(const Point & p)
{
@@ -9,6 +9,8 @@

#include "PointValueSampler.h"

#include <numeric>

registerMooseObject("MooseApp", PointValueSampler);

template <>
@@ -27,9 +29,21 @@ PointValueSampler::PointValueSampler(const InputParameters & parameters)
: PointSamplerBase(parameters)
{
_points = getParam<std::vector<Point>>("points");
}

_ids.resize(_points.size());

for (unsigned int i = 0; i < _points.size(); i++)
_ids[i] = i;
void
PointValueSampler::initialize()
{
// Generate new Ids if the point vector has grown (non-negative counting numbers)
if (_points.size() > _ids.size())
{
auto old_size = _ids.size();
_ids.resize(_points.size());
std::iota(_ids.begin() + old_size, _ids.end(), old_size);
}
// Otherwise sync the ids array to be smaller if the point vector has been shrunk
else if (_points.size() < _ids.size())
_ids.resize(_points.size());

PointSamplerBase::initialize();
}
@@ -0,0 +1,35 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#ifndef DYNAMICPOINTVALUESAMPLER_H
#define DYNAMICPOINTVALUESAMPLER_H

// MOOSE includes
#include "LineValueSampler.h"

// Forward Declarations
class DynamicPointValueSampler;

template <>
InputParameters validParams<DynamicPointValueSampler>();

class DynamicPointValueSampler : public LineValueSampler
{
public:
DynamicPointValueSampler(const InputParameters & parameters);

virtual void initialize() override;

protected:
const unsigned int _adder;

const bool _use_transfer;
};

#endif
@@ -0,0 +1,53 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "DynamicPointValueSampler.h"

registerMooseObject("MooseTestApp", DynamicPointValueSampler);

template <>
InputParameters
validParams<DynamicPointValueSampler>()
{
InputParameters params = validParams<LineValueSampler>();

params.addParam<unsigned int>(
"num_points_adder", 2, "The number of new points to add each iteration");

params.addParam<bool>(
"test_transfer_points_vector",
false,
"Set to true to use the transferPointsVector API (Default setPointsVector).");

return params;
}

DynamicPointValueSampler::DynamicPointValueSampler(const InputParameters & parameters)
: LineValueSampler(parameters),
_adder(getParam<unsigned int>("num_points_adder")),
_use_transfer(getParam<bool>("test_transfer_points_vector"))
{
}

void
DynamicPointValueSampler::initialize()
{
_num_points = _num_points + _adder;

std::vector<Point> points;
generatePointsAndIDs(_start_point, _end_point, _num_points, points, _ids);

// We don't need to use the public API here, but we are doing it for testing
if (_use_transfer)
transferPointsVector(std::move(points));
else
setPointsVector(points);

LineValueSampler::initialize();
}
@@ -0,0 +1,120 @@
[Mesh]
type = GeneratedMesh
nx = 5
ny = 5
dim = 2
[]

[Variables]
[u]
[]
[]

[Functions]
[forcing_func]
type = ParsedFunction
value = alpha*alpha*pi*pi*sin(alpha*pi*x)
vars = 'alpha'
vals = '4'
[]

[u_func]
type = ParsedGradFunction
value = sin(alpha*pi*x)
grad_x = alpha*pi*sin(alpha*pi*x)
vars = 'alpha'
vals = '4'
[]
[]

[Kernels]
[diff]
type = CoefDiffusion
variable = u
coef = 0.1
[]

[forcing]
type = BodyForce
variable = u
function = forcing_func
[]

[time]
type = TimeDerivative
variable = u
[]
[]

[BCs]
[left]
type = DirichletBC
variable = 'u'
boundary = 'left'
value = 0
[]

[right]
type = DirichletBC
variable = 'u'
boundary = 'right'
value = 0
[]
[]

[Executioner]
type = Transient
num_steps = 7
dt = 0.1

petsc_options_iname = '-pc_type -pc_hypre_type'
petsc_options_value = 'hypre boomeramg'
[]

[Postprocessors]
[l2_error]
type = ElementL2Error
variable = u
function = u_func
[]

[dofs]
type = NumDOFs
[]
[]

[Adaptivity]
max_h_level = 3
marker = error
[Indicators]
[jump]
type = GradientJumpIndicator
variable = u
[]
[]
[Markers]
[error]
type = ErrorFractionMarker
indicator = jump
coarsen = 0.1
refine = 0.3
[]
[]
[]

[VectorPostprocessors]
[dynamic_line_sampler]
type = DynamicPointValueSampler
variable = u
start_point = '0 0.5 0'
end_point = '1 0.5 0'
num_points = 6
sort_by = x
execute_on = 'initial timestep_end'
[]
[]

[Outputs]
exodus = true
csv = true
[]
@@ -0,0 +1,11 @@
x,y,z,id,u
0,0.5,0,0,0
0.11111111111111,0.5,0,0.11111111111111,2.3595199239821
0.22222222222222,0.5,0,0.22222222222222,3.0116751915442
0.33333333333333,0.5,0,0.33333333333333,-3.1656281665735
0.44444444444444,0.5,0,0.44444444444444,-3.8177834341356
0.55555555555556,0.5,0,0.55555555555556,3.8177834341355
0.66666666666667,0.5,0,0.66666666666667,3.1656281665734
0.77777777777778,0.5,0,0.77777777777778,-3.0116751915443
0.88888888888889,0.5,0,0.88888888888889,-2.3595199239821
1,0.5,0,1,-1.8861072096e-15
@@ -0,0 +1,21 @@
x,y,z,id,u
0,0.5,0,0,0
0.052631578947368,0.5,0,0.052631578947368,5.9788294723389
0.10526315789474,0.5,0,0.10526315789474,9.4011636294063
0.15789473684211,0.5,0,0.15789473684211,8.8365135832746
0.21052631578947,0.5,0,0.21052631578947,4.5300836212104
0.26315789473684,0.5,0,0.26315789473684,-1.7013726574824
0.31578947368421,0.5,0,0.31578947368421,-7.2587341323989
0.36842105263158,0.5,0,0.36842105263158,-9.8015204019932
0.42105263157895,0.5,0,0.42105263157895,-8.2408102762147
0.47368421052632,0.5,0,0.47368421052632,-3.2024426012765
0.52631578947368,0.5,0,0.52631578947368,3.2024426012618
0.57894736842105,0.5,0,0.57894736842105,8.2408102761943
0.63157894736842,0.5,0,0.63157894736842,9.8015204019903
0.68421052631579,0.5,0,0.68421052631579,7.2587341323924
0.73684210526316,0.5,0,0.73684210526316,1.701372657481
0.78947368421053,0.5,0,0.78947368421053,-4.5300836212134
0.84210526315789,0.5,0,0.84210526315789,-8.8365135832819
0.89473684210526,0.5,0,0.89473684210526,-9.4011636294108
0.94736842105263,0.5,0,0.94736842105263,-5.9788294723449
1,0.5,0,1,-3.3543535025412e-16
@@ -0,0 +1,23 @@
[Tests]
[dynamic_point_sampler]
type = CSVDiff
input = dynamic_point_sampler.i
csvdiff = 'dynamic_point_sampler_out_dynamic_line_sampler_0001.csv dynamic_point_sampler_out_dynamic_line_sampler_0006.csv'

issues = "#12934"
design = "LineValueSampler.md"
requirement = "The system shall support dynamic numbers of sample points during the simulation."
[]

[dynamic_point_sampler_transfer]
type = CSVDiff
input = dynamic_point_sampler.i
csvdiff = 'dynamic_point_sampler_out_dynamic_line_sampler_0001.csv dynamic_point_sampler_out_dynamic_line_sampler_0006.csv'
cli_args = 'VectorPostprocessors/dynamic_line_sampler/test_transfer_points_vector=true'
prereq = 'dynamic_point_sampler'

issues = "#12934"
design = "LineValueSampler.md"
requirement = "The system shall support dynamic numbers of sample points during the simulation with move semantics of the points vector."
[]
[]

0 comments on commit 7d4ae5d

Please sign in to comment.
You can’t perform that action at this time.