ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
src
sol
sampler
ROL_UserInputGenerator.hpp
Go to the documentation of this file.
1
// @HEADER
2
// *****************************************************************************
3
// Rapid Optimization Library (ROL) Package
4
//
5
// Copyright 2014 NTESS and the ROL contributors.
6
// SPDX-License-Identifier: BSD-3-Clause
7
// *****************************************************************************
8
// @HEADER
9
10
#ifndef ROL_USERINPUTGENERATOR_HPP
11
#define ROL_USERINPUTGENERATOR_HPP
12
13
#include "
ROL_SampleGenerator.hpp
"
14
#include "ROL_ParameterList.hpp"
15
#include "
ROL_BatchManager.hpp
"
16
#include <fstream>
17
#include <iostream>
18
#include <string>
19
20
namespace
ROL
{
21
22
template
<
typename
Real>
23
class
UserInputGenerator
:
public
SampleGenerator
<Real> {
24
private
:
25
int
nSamp_
;
26
27
void
sample
(std::string file_pt, std::string file_wt,
28
int
n,
int
dim
,
29
const
ROL::Ptr<
BatchManager<Real>
> &bman) {
30
nSamp_
= n;
31
// Read in full point data and weight data
32
std::fstream input_pt, input_wt;
33
input_pt.open(file_pt.c_str(),std::ios::in);
34
input_wt.open(file_wt.c_str(),std::ios::in);
35
if
( !input_pt.is_open() || !input_wt.is_open() ) {
36
if
( !input_pt.is_open() ) {
37
if
( bman->batchID() == 0 ) {
38
std::cout <<
"CANNOT OPEN "
<< file_pt.c_str() << std::endl;
39
}
40
}
41
if
( !input_wt.is_open() ) {
42
if
( bman->batchID() == 0 ) {
43
std::cout <<
"CANNOT OPEN "
<< file_wt.c_str() << std::endl;
44
}
45
}
46
}
47
else
{
48
std::vector<std::vector<Real>> pt(n);
49
std::vector<Real> wt(n,0.0);
50
std::vector<Real> point(
dim
,0.0);;
51
for
(
int
i = 0; i < n; i++) {
52
for
(
int
j = 0; j <
dim
; j++) {
53
input_pt >> point[j];
54
}
55
pt[i] = point;
56
input_wt >> wt[i];
57
}
58
// Get process rankd and number of processes
59
int
rank = bman->batchID();
60
int
nProc = bman->numBatches();
61
// Separate samples across processes
62
int
frac = n/nProc;
63
int
rem = n%nProc;
64
int
N = frac;
65
if
( rank < rem ) N++;
66
std::vector<std::vector<Real>> my_pt(N);
67
std::vector<Real> my_wt(N,0.0);
68
int
index = 0;
69
for
(
int
i = 0; i < N; i++) {
70
index = i*nProc + rank;
71
my_pt[i] = pt[index];
72
my_wt[i] = wt[index];
73
}
74
SampleGenerator<Real>::setPoints
(my_pt);
75
SampleGenerator<Real>::setWeights
(my_wt);
76
}
77
input_pt.close();
78
input_wt.close();
79
}
80
81
public
:
82
UserInputGenerator
(ROL::ParameterList &parlist,
83
const
ROL::Ptr<
BatchManager<Real>
> &bman)
84
:
SampleGenerator
<Real>(bman) {
85
ROL::ParameterList &list
86
= parlist.sublist(
"SOL"
).sublist(
"Sample Generator"
).sublist(
"User Input"
);
87
if
( list.isParameter(
"Points File"
) &&
88
list.isParameter(
"Weights File"
) &&
89
list.isParameter(
"Number of Samples"
) &&
90
list.isParameter(
"Dimension"
) ) {
91
std::string file_pt = list.get(
"Points File Name"
,
"points.txt"
);
92
std::string file_wt = list.get(
"Weights File Name"
,
"weights.txt"
);
93
int n = list.get(
"Number of Samples"
,100);
94
int dim = list.get(
"Dimension"
,4);
95
sample(file_pt,file_wt,n,dim,bman);
96
}
97
else
{
98
ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
99
">>> (ROL::UserInputGenerator): ParameterList does not contain sufficient information."
);
100
}
101
}
102
103
UserInputGenerator
(std::string file_pt,
104
std::string file_wt,
105
int
n,
106
int
dim
,
107
const
ROL::Ptr<
BatchManager<Real>
> &bman)
108
:
SampleGenerator
<Real>(bman) {
109
sample
(file_pt,file_wt,n,
dim
,bman);
110
}
111
112
void
refine
(
void
) {}
113
114
int
numGlobalSamples
(
void
)
const
{
115
return
nSamp_
;
116
}
117
};
118
119
}
120
121
#endif
ROL_BatchManager.hpp
ROL_SampleGenerator.hpp
ROL::BatchManager
Definition
ROL_BatchManager.hpp:19
ROL::SampleGenerator::setPoints
void setPoints(std::vector< std::vector< Real > > &p)
Definition
ROL_SampleGenerator.hpp:28
ROL::SampleGenerator::SampleGenerator
SampleGenerator(const ROL::Ptr< BatchManager< Real > > &bman)
Definition
ROL_SampleGenerator.hpp:39
ROL::SampleGenerator::setWeights
void setWeights(std::vector< Real > &w)
Definition
ROL_SampleGenerator.hpp:32
ROL::UserInputGenerator::UserInputGenerator
UserInputGenerator(std::string file_pt, std::string file_wt, int n, int dim, const ROL::Ptr< BatchManager< Real > > &bman)
Definition
ROL_UserInputGenerator.hpp:103
ROL::UserInputGenerator::numGlobalSamples
int numGlobalSamples(void) const
Definition
ROL_UserInputGenerator.hpp:114
ROL::UserInputGenerator::UserInputGenerator
UserInputGenerator(ROL::ParameterList &parlist, const ROL::Ptr< BatchManager< Real > > &bman)
Definition
ROL_UserInputGenerator.hpp:82
ROL::UserInputGenerator::nSamp_
int nSamp_
Definition
ROL_UserInputGenerator.hpp:25
ROL::UserInputGenerator::sample
void sample(std::string file_pt, std::string file_wt, int n, int dim, const ROL::Ptr< BatchManager< Real > > &bman)
Definition
ROL_UserInputGenerator.hpp:27
ROL::UserInputGenerator::refine
void refine(void)
Definition
ROL_UserInputGenerator.hpp:112
ROL
Definition
ROL_ElementwiseVector.hpp:27
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0