OpenLB 1.8.1
Loading...
Searching...
No Matches
controller.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2012 Mathias J. Krause
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
30#ifndef CONTROLLER_H
31#define CONTROLLER_H
32
33#include "io/ostreamManager.h"
34
36namespace olb {
37
39namespace opti {
40
41
42template<typename S>
43class Controller {
44
45protected:
47 std::vector<S> _control;
48
49public:
50 Controller (int dimControl)
51 : _dimControl (dimControl),
52 _control (dimControl, 0.0)
53 { }
54
55 Controller (std::vector<S>& initialValues)
56 : _dimControl(initialValues.size()), _control(initialValues)
57 { }
58
59 S getControl(int i) const
60 {
61 return _control[i];
62 };
63 const std::vector<S>& getControl() const
64 {
65 return _control;
66 };
67
68 const S* getControlPointer(int i) const
69 {
70 return &_control[i];
71 }
73 {
74 return &_control[i];
75 }
76
77 int getDimControl() const
78 {
79 return _dimControl;
80 };
81
82 void setControl(const std::vector<S>& control, int dimControl)
83 {
85 _dimControl = dimControl;
86 };
87
88 void print()
89 {
90 static OstreamManager clout(std::cout,"Controller");
91
92 clout << " control={ ";
93 for (int i=0; i<_dimControl; i++) {
94 clout << _control[i] << " ";
95 }
96 clout << "}" << std::endl;
97
98 };
99
100 void writeToFile(const char* fname=nullptr)
101 {
102 static OstreamManager clout(std::cout,"Controller");
103 std::ofstream file;
104 if (fname==nullptr) {
105 static int iter(0);
106 std::stringstream number;
107 number << std::setw(3) << std::setfill('0') << iter++;
108 file.open(("Controller" + number.str() + ".txt").c_str());
109 }
110 else {
111 file.open(fname);
112 }
113
114 file << _dimControl << std::endl;
115 for (int i=0; i<_dimControl; i++) {
116 file << _control[i] << std::endl;
117 }
118 file.close();
119 };
120
121 void readFromFile(const char* fname)
122 {
123 static OstreamManager clout(std::cout,"Controller");
124 std::ifstream file;
125 // if (file.open(fname)) {
126 file.open(fname);
127 int dim;
128 file >> dim;
129 if (dim!=_dimControl) {
130 clout << "Error: dimensions do not match! dim_controller=" << _dimControl << "; dim_file=" << dim << std::endl;
131 assert(false);
132 }
133 else {
134 for (int i=0; i<_dimControl; i++) {
135 double tmpVal;
136 file >> tmpVal;
137 _control[i] = tmpVal;
138 }
139 }
140 file.close();
141 // }
142 }
143};
144
145
146} // namespace opti
147
148} // namespace olb
149
150#endif
class for marking output with some text
const S * getControlPointer(int i) const
Definition controller.h:68
Controller(std::vector< S > &initialValues)
Definition controller.h:55
std::vector< S > _control
Definition controller.h:47
int getDimControl() const
Definition controller.h:77
Controller(int dimControl)
Definition controller.h:50
void writeToFile(const char *fname=nullptr)
Definition controller.h:100
S * getControlPointer(int i)
Definition controller.h:72
S getControl(int i) const
Definition controller.h:59
const std::vector< S > & getControl() const
Definition controller.h:63
void readFromFile(const char *fname)
Definition controller.h:121
void setControl(const std::vector< S > &control, int dimControl)
Definition controller.h:82
Top level namespace for all of OpenLB.
Optimization Code.