OpenLB 1.7
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>
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 const& getControl(int i) const
60 {
61 return _control[i];
62 };
63 S& getControl(int i)
64 {
65 return _control[i];
66 };
67 const std::vector<S>& getControl()
68 {
69 return _control;
70 };
71
72 int getDimControl() const
73 {
74 return _dimControl;
75 };
76
77 void setControl(const std::vector<S>& control, int dimControl)
78 {
80 _dimControl = dimControl;
81 };
82
83 void print()
84 {
85 static OstreamManager clout(std::cout,"Controller");
86
87 clout << " control={ ";
88 for (int i=0; i<_dimControl; i++) {
89 clout << _control[i] << " ";
90 }
91 clout << "}" << std::endl;
92
93 };
94
95 void writeToFile(const char* fname=nullptr)
96 {
97 static OstreamManager clout(std::cout,"Controller");
98 std::ofstream file;
99 if (fname==nullptr) {
100 static int iter(0);
101 std::stringstream number;
102 number << std::setw(3) << std::setfill('0') << iter++;
103 file.open(("Controller" + number.str() + ".txt").c_str());
104 }
105 else {
106 file.open(fname);
107 }
108
109 file << _dimControl << std::endl;
110 for (int i=0; i<_dimControl; i++) {
111 file << _control[i] << std::endl;
112 }
113 file.close();
114 };
115
116 void readFromFile(const char* fname)
117 {
118 static OstreamManager clout(std::cout,"Controller");
119 std::ifstream file;
120 // if (file.open(fname)) {
121 file.open(fname);
122 int dim;
123 file >> dim;
124 if (dim!=_dimControl) {
125 clout << "Error: dimensions do not match! dim_controller=" << _dimControl << "; dim_file=" << dim << std::endl;
126 assert(false);
127 }
128 else {
129 for (int i=0; i<_dimControl; i++) {
130 double tmpVal;
131 file >> tmpVal;
132 _control[i] = tmpVal;
133 }
134 }
135 file.close();
136 // }
137 }
138};
139
140
141} // namespace opti
142
143} // namespace olb
144
145#endif
class for marking output with some text
const std::vector< S > & getControl()
Definition controller.h:67
S const & getControl(int i) const
Definition controller.h:59
Controller(std::vector< S > &initialValues)
Definition controller.h:55
std::vector< S > _control
Definition controller.h:47
int getDimControl() const
Definition controller.h:72
Controller(int dimControl)
Definition controller.h:50
void writeToFile(const char *fname=nullptr)
Definition controller.h:95
S & getControl(int i)
Definition controller.h:63
void readFromFile(const char *fname)
Definition controller.h:116
void setControl(const std::vector< S > &control, int dimControl)
Definition controller.h:77
Top level namespace for all of OpenLB.
Optimization Code.