Skip to content

Commit 34b2b85

Browse files
author
Bozo Vazic
committed
csv sampler code and test closes #31810
1 parent 8334cde commit 34b2b85

File tree

8 files changed

+303
-0
lines changed

8 files changed

+303
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# CSVPointValueSampler
2+
3+
!syntax description /VectorPostprocessors/CSVPointValueSampler
4+
5+
!alert note
6+
If the csv point value sampler is used with a discontinuous variable on the edge/face of a 2D/3D element, then the value from the element with the lowest ID will be returned.
7+
8+
The CSV output, with rows for each sampled point, contains the columns listed below. The vectors declared by `CSVPointValueSampler`
9+
share the same names as the column headers.
10+
11+
- the id of the sampled points
12+
13+
- the values of the variable(s) requested
14+
15+
- the x, y and z coordinates of the requested sampled points
16+
17+
The sampled points and the values are written to a csv file at every time step. The sorting order of the points can be changed using the sort_by parameter which takes x, y, z or id (increasing distance from start point) as input.
18+
Id can be supplied and/or sorted by the `point_id` from the csv file.
19+
20+
## Example input syntax
21+
22+
In this example, the variables `param1` and `param2` are sampled at the points read in from the csv file using a `CSVPointValueSampler`.
23+
24+
!listing test/tests/vectorpostprocessors/csv_point_value_sampler/csv_point_sampler.i block=VectorPostprocessors/point_sample
25+
26+
!syntax parameters /VectorPostprocessors/CSVPointValueSampler
27+
28+
!syntax inputs /VectorPostprocessors/CSVPointValueSampler
29+
30+
!syntax children /VectorPostprocessors/CSVPointValueSampler
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#pragma once
11+
12+
#include "PointVariableSamplerBase.h"
13+
14+
/**
15+
* Class for sampling variable(s) at csv imported points
16+
*/
17+
class CSVPointValueSampler : public PointVariableSamplerBase
18+
{
19+
public:
20+
static InputParameters validParams();
21+
22+
CSVPointValueSampler(const InputParameters & parameters);
23+
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "CSVPointValueSampler.h"
2+
#include "DelimitedFileReader.h"
3+
4+
registerMooseObject("MooseApp", CSVPointValueSampler);
5+
6+
InputParameters
7+
CSVPointValueSampler::validParams()
8+
{
9+
InputParameters params = PointVariableSamplerBase::validParams();
10+
params.addClassDescription("Sample a variable at points specified in a csv file.");
11+
params.addRequiredParam<FileName>("points_file", "CSV file with point coordinates (x, y, z).");
12+
params.addRequiredParam<std::string>("point_xcoord",
13+
"x coordinate column name from csv file being read in.");
14+
params.addRequiredParam<std::string>("point_ycoord",
15+
"y coordinate column name from csv file being read in.");
16+
params.addRequiredParam<std::string>("point_zcoord",
17+
"z coordinate column name from csv file being read in.");
18+
params.addParam<std::string>("point_id", "id column name from csv file being read in.");
19+
20+
return params;
21+
}
22+
23+
CSVPointValueSampler::CSVPointValueSampler(const InputParameters & parameters)
24+
: PointVariableSamplerBase(parameters)
25+
{
26+
std::string idName;
27+
if (isParamValid("point_id"))
28+
idName = getParam<std::string>("point_id");
29+
30+
std::string xName = getParam<std::string>("point_xcoord");
31+
std::string yName = getParam<std::string>("point_ycoord");
32+
std::string zName = getParam<std::string>("point_zcoord");
33+
34+
bool found_x = false;
35+
bool found_y = false;
36+
bool found_z = false;
37+
38+
std::vector<Real> point_xcoord;
39+
std::vector<Real> point_ycoord;
40+
std::vector<Real> point_zcoord;
41+
42+
MooseUtils::DelimitedFileReader reader(getParam<FileName>("points_file"));
43+
reader.read();
44+
45+
auto const & names = reader.getNames();
46+
auto const & data = reader.getData();
47+
48+
for (std::size_t i = 0; i < names.size(); ++i)
49+
{
50+
if (names[i] == xName)
51+
{
52+
point_xcoord = data[i];
53+
found_x = true;
54+
}
55+
else if (names[i] == yName)
56+
{
57+
point_ycoord = data[i];
58+
found_y = true;
59+
}
60+
else if (names[i] == zName)
61+
{
62+
point_zcoord = data[i];
63+
found_z = true;
64+
}
65+
else if (names[i] == idName)
66+
{
67+
_ids = data[i];
68+
}
69+
}
70+
if (!found_x || !found_y || !found_z)
71+
paramError("points_file",
72+
"Column with name '",
73+
xName,
74+
" or ",
75+
yName,
76+
" or ",
77+
zName,
78+
"' missing from measurement file");
79+
80+
const std::size_t nvals = point_xcoord.size();
81+
_points.resize(nvals);
82+
for (const auto & i : make_range(nvals))
83+
{
84+
const Point point(point_xcoord[i], point_ycoord[i], point_zcoord[i]);
85+
_points[i] = point;
86+
}
87+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[Mesh]
2+
[gmg]
3+
type = GeneratedMeshGenerator
4+
dim = 2
5+
nx = 10
6+
xmin = 0
7+
xmax = 1
8+
ny = 10
9+
ymin = 0
10+
ymax = 1
11+
[]
12+
parallel_type = REPLICATED
13+
[]
14+
15+
[AuxVariables]
16+
[params1]
17+
order = FIRST
18+
family = LAGRANGE
19+
[]
20+
[params2]
21+
order = CONSTANT
22+
family = MONOMIAL
23+
[]
24+
[]
25+
26+
[AuxKernels]
27+
[params1]
28+
type = ParsedAux
29+
variable = params1
30+
expression = 'x*y*t'
31+
use_xyzt = true
32+
[]
33+
[params2]
34+
type = ParsedAux
35+
variable = params2
36+
expression = 'x*x*t'
37+
use_xyzt = true
38+
[]
39+
[]
40+
41+
[VectorPostprocessors]
42+
[point_sample]
43+
type=CSVPointValueSampler
44+
point_xcoord = x
45+
point_ycoord = y
46+
point_zcoord = z
47+
point_id = id
48+
points_file = 'gold/csv_point_sampler_out_point_sample_0001.csv'
49+
sort_by = id
50+
variable = 'params1 params2'
51+
[]
52+
[point_sample_x]
53+
type=CSVPointValueSampler
54+
point_xcoord = x
55+
point_ycoord = y
56+
point_zcoord = z
57+
point_id = id
58+
points_file = 'gold/csv_point_sampler_out_point_sample_0001.csv'
59+
sort_by = x
60+
variable = 'params1 params2'
61+
[]
62+
[]
63+
64+
[Outputs]
65+
csv = true
66+
execute_on = timestep_end
67+
[]
68+
69+
[Executioner]
70+
type = Transient
71+
dt = 1.0
72+
num_steps = 2
73+
solve_type = NEWTON
74+
[]
75+
76+
[Problem]
77+
solve = false
78+
[]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
id,params1,params2,x,y,z
2+
0,0,0.9025,1,0,0
3+
0.070710678118655,0.0475,0.9025,0.95,0.05,0
4+
0.14142135623731,0.09,0.7225,0.9,0.1,0
5+
0.21213203435596,0.1275,0.7225,0.85,0.15,0
6+
0.28284271247462,0.16,0.5625,0.8,0.2,0
7+
0.35355339059327,0.1875,0.5625,0.75,0.25,0
8+
0.42426406871193,0.21,0.4225,0.7,0.3,0
9+
0.49497474683058,0.2275,0.4225,0.65,0.35,0
10+
0.56568542494924,0.24,0.3025,0.6,0.4,0
11+
0.63639610306789,0.2475,0.3025,0.55,0.45,0
12+
0.70710678118655,0.25,0.2025,0.5,0.5,0
13+
0.7778174593052,0.2475,0.2025,0.45,0.55,0
14+
0.84852813742386,0.24,0.1225,0.4,0.6,0
15+
0.91923881554251,0.2275,0.1225,0.35,0.65,0
16+
0.98994949366117,0.21,0.0625,0.3,0.7,0
17+
1.0606601717798,0.1875,0.0625,0.25,0.75,0
18+
1.1313708498985,0.16,0.0225,0.2,0.8,0
19+
1.2020815280171,0.1275,0.0225,0.15,0.85,0
20+
1.2727922061358,0.09,0.0025,0.1,0.9,0
21+
1.3435028842544,0.0475,0.0025,0.05,0.95,0
22+
1.4142135623731,0,0.0025,0,1,0
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
id,params1,params2,x,y,z
2+
0,0,1.805,1,0,0
3+
0.070710678118655,0.095,1.805,0.95,0.05,0
4+
0.14142135623731,0.18,1.445,0.9,0.1,0
5+
0.21213203435596,0.255,1.445,0.85,0.15,0
6+
0.28284271247462,0.32,1.125,0.8,0.2,0
7+
0.35355339059327,0.375,1.125,0.75,0.25,0
8+
0.42426406871193,0.42,0.845,0.7,0.3,0
9+
0.49497474683058,0.455,0.845,0.65,0.35,0
10+
0.56568542494924,0.48,0.605,0.6,0.4,0
11+
0.63639610306789,0.495,0.605,0.55,0.45,0
12+
0.70710678118655,0.5,0.405,0.5,0.5,0
13+
0.7778174593052,0.495,0.405,0.45,0.55,0
14+
0.84852813742386,0.48,0.245,0.4,0.6,0
15+
0.91923881554251,0.455,0.245,0.35,0.65,0
16+
0.98994949366117,0.42,0.125,0.3,0.7,0
17+
1.0606601717798,0.375,0.125,0.25,0.75,0
18+
1.1313708498985,0.32,0.045,0.2,0.8,0
19+
1.2020815280171,0.255,0.045,0.15,0.85,0
20+
1.2727922061358,0.18,0.005,0.1,0.9,0
21+
1.3435028842544,0.095,0.005,0.05,0.95,0
22+
1.4142135623731,0,0.005,0,1,0
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
id,params1,params2,x,y,z
2+
1.4142135623731,0,0.005,0,1,0
3+
1.3435028842544,0.095,0.005,0.05,0.95,0
4+
1.2727922061358,0.18,0.005,0.1,0.9,0
5+
1.2020815280171,0.255,0.045,0.15,0.85,0
6+
1.1313708498985,0.32,0.045,0.2,0.8,0
7+
1.0606601717798,0.375,0.125,0.25,0.75,0
8+
0.98994949366117,0.42,0.125,0.3,0.7,0
9+
0.91923881554251,0.455,0.245,0.35,0.65,0
10+
0.84852813742386,0.48,0.245,0.4,0.6,0
11+
0.7778174593052,0.495,0.405,0.45,0.55,0
12+
0.70710678118655,0.5,0.405,0.5,0.5,0
13+
0.63639610306789,0.495,0.605,0.55,0.45,0
14+
0.56568542494924,0.48,0.605,0.6,0.4,0
15+
0.49497474683058,0.455,0.845,0.65,0.35,0
16+
0.42426406871193,0.42,0.845,0.7,0.3,0
17+
0.35355339059327,0.375,1.125,0.75,0.25,0
18+
0.28284271247462,0.32,1.125,0.8,0.2,0
19+
0.21213203435596,0.255,1.445,0.85,0.15,0
20+
0.14142135623731,0.18,1.445,0.9,0.1,0
21+
0.070710678118655,0.095,1.805,0.95,0.05,0
22+
0,0,1.805,1,0,0
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[Tests]
2+
issues = '#31810'
3+
design = 'CSVPointValueSampler.md'
4+
[test]
5+
type = 'CSVDiff'
6+
allow_warnings = true
7+
input = 'csv_point_sampler.i'
8+
csvdiff = 'csv_point_sampler_out_point_sample_0001.csv csv_point_sampler_out_point_sample_0002.csv csv_point_sampler_out_point_sample_x_0002.csv'
9+
requirement = 'The system shall support the ability to sample field variables at user specified points imported via csv file.'
10+
[]
11+
[error]
12+
type = 'RunException'
13+
allow_warnings = true
14+
input = 'csv_point_sampler.i'
15+
cli_args = "VectorPostprocessors/point_sample/point_xcoord=dummy"
16+
expect_err = "Column with name 'dummy or y or z' missing from measurement file"
17+
requirement = 'The system shall report error when column requested is not named in csv file.'
18+
[]
19+
[]

0 commit comments

Comments
 (0)