Minsky: 3.17.0
variableValue.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2012
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef VARIABLE_VALUE
20 #define VARIABLE_VALUE
21 #include "variableType.h"
22 #include "tensorInterface.h"
23 #include "tensorVal.h"
24 #include "classdesc_access.h"
25 #include "str.h"
26 #include "CSVDialog.h"
27 #include "latexMarkup.h"
28 #include "slider.h"
29 #include "valueId.h"
30 #include "variableSummary.h"
31 #include <regex>
32 #include <utility>
33 #include <boost/locale.hpp>
34 using namespace boost::locale::conv;
35 
36 namespace minsky
37 {
38  class VariableValue;
39  class VariableValues;
40  class Group;
41  typedef std::shared_ptr<Group> GroupPtr;
42  using namespace civita;
43 
44  struct VariableValueData: public civita::ITensorVal, public Slider
45  {
46  using ITensorVal::operator=;
47 
49  TensorVal tensorInit;
52 
55  bool unitsCached=false; // optimisation to prevent evaluating this units value more than once
56  void setUnits(const std::string& x) {units=Units(x);}
57 
59  std::string detailedText, tooltip;
60 
61  bool godleyOverridden=false;
62  std::string name; // name of this variable
63  classdesc::Exclude<std::weak_ptr<Group>> m_scope;
64 
67 
68  };
69 
71  {
73  private:
75  int m_idx=-1;
76  double& valRef();
77  const double& valRef() const;
78 
79  friend class VariableManager;
80  friend struct SchemaHelper;
81 
82  // values are always live
83  ITensor::Timestamp timestamp() const override {return Timestamp::clock::now();}
84 
85  protected: // protected to allow creation of zero/one constants.
87  std::string m_init;
88  public:
90  bool lhs() const {
91  return m_type==flow || m_type==tempFlow;}
93  bool temp() const {
94  return type()==tempFlow || type()==undefined;}
96  bool isFlowVar() const {
97  return m_type!=stock && m_type!=integral && m_type!=undefined;
98  }
99  bool isZero() const {
100  return m_type==constant && (init().empty() || init()=="0");
101  }
102 
103  VariableType::Type type() const {return m_type;}
104 
107  // name valueAt to allow TCL exposure
108  double valueAt(size_t i) const {
109  if (i>=size())
110  throw std::runtime_error("Index out of range");
111  return operator[](i);
112  }
113  double value() const {return valueAt(0);}
115  double setValue(size_t i, double x) {
116  if (i>=size())
117  throw std::runtime_error("Index out of range");
118  return operator[](i)=x;
119  }
120  double setValue(double x) {return setValue(0,x);}
121  int idx() const {return m_idx;}
122  void reset_idx() {m_idx=-1;}
123 
124  double operator[](std::size_t i) const override {return *(&valRef()+i);}
125  double& operator[](std::size_t i) override;
126 
127  const Index& index(Index&& i) override {
128  std::size_t prevNumElems = size();
129  m_index=i;
130  if (idx()==-1 || !idxInRange() || (prevNumElems<size()))
131  allocValue();
132  assert(idxInRange());
133  return m_index;
134  }
135  using ITensorVal::index;
136 
137  template <class T>
138  void hypercube_(T x) {
139  assert(idx()==-1||idxInRange());
140  std::size_t prevNumElems = size();
141  ITensor::hypercube(x);
142  if (idx()==-1 || (prevNumElems<size()))
143  allocValue();
144  assert(idxInRange());
145  }
146 
147  bool idxInRange() const;
148 
149  const Hypercube& hypercube() const override;
150  const Hypercube& hypercube(const Hypercube& hc) override
151  {hypercube_(hc); return m_hypercube;}
152  const Hypercube& hypercube(Hypercube&& hc) override
153  {hypercube_(hc); return m_hypercube;}
154  using ITensorVal::hypercube;
155 
156  VariableValue(VariableType::Type type=VariableType::undefined, const std::string& name="", const GroupPtr& group=GroupPtr()):
157  m_type(type)
158  {
159  this->name=utf_to_utf<char>(name);
160  m_scope=scope(group,name);
161  }
162 
164  m_type=type;
165  init(vv.init());
166  }
167 
168  VariableValue(const VariableValue&)=delete;
169  void operator=(const VariableValue&)=delete;
170 
171  using ITensorVal::operator=;
172  VariableValue& operator=(TensorVal const&);
173  VariableValue& operator=(const ITensor& x) override;
174 
176  VariableValue& allocValue();
177 
178  std::string valueId() const {return valueIdFromScope(m_scope.lock(),canonicalName(name));}
179 
181  void exportAsCSV(const std::string& filename, const std::string& comment="", bool tabular=false) const;
182 
183  Summary summary() const;
184 
185  const std::string& init() const {return m_init;}
186  const std::string& init(const std::string& x);
187 
189  void sliderSet(double x);
191  void incrSlider(double step);
192 
194  void adjustSliderBounds();
195  };
196 
197  struct ValueVector
198  {
201  static std::vector<double,CIVITA_ALLOCATOR<double>> stockVars;
205  static std::vector<double,CIVITA_ALLOCATOR<double>> flowVars;
206  };
207 
208 }
209 
210 #include "variableValue.cd"
211 #include "variableValue.xcd"
212 
213 #ifdef _CLASSDESC
214 #pragma omit pack minsky::VariableValue
215 #pragma omit unpack minsky::VariableValue
216 #endif
217 
218 namespace classdesc_access
219 {
220  // nobble these as we're not using them
221  template <>
222  struct access_pack<minsky::VariableValue>:
223  public classdesc::NullDescriptor<classdesc::pack_t> {};
224  template <>
225  struct access_unpack<minsky::VariableValue>:
226  public classdesc::NullDescriptor<classdesc::unpack_t> {};
227 }
228 
229 #endif
double value() const
set the value at the ith location
classdesc::Exclude< std::weak_ptr< Group > > m_scope
Definition: variableValue.h:63
bool isFlowVar() const
returns true if variable&#39;s data is allocated on the flowVariables vector
Definition: variableValue.h:96
const Hypercube & hypercube(Hypercube &&hc) override
static std::vector< double, CIVITA_ALLOCATOR< double > > stockVars
vector of variables that are integrated via Runge-Kutta. These variables label the columns of the God...
void setUnits(const std::string &x)
Definition: variableValue.h:56
double valueAt(size_t i) const
VariableValue(VariableType::Type type=VariableType::undefined, const std::string &name="", const GroupPtr &group=GroupPtr())
TensorPtr rhs
when the RHS is attached to a tensor expression, this is a reference to it
Definition: variableValue.h:51
size_t scope(const string &name)
extract scope from a qualified variable name
Definition: valueId.cc:83
VariableType::Type type() const
value at the ith location of the vector/tensor. Default, (i=0) is right for scalar quantities ...
summary for the variable tab (aka summary tab).
const std::string & init() const
CSVDialog csvDialog
for importing CSV files
Definition: variableValue.h:66
TensorVal tensorInit
when init is a tensor of values, this overrides the init string
Definition: variableValue.h:49
std::chrono::time_point< std::chrono::high_resolution_clock > Timestamp
double operator[](std::size_t i) const override
bool temp() const
variable is a temporary
Definition: variableValue.h:93
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
VariableValue(VariableType::Type type, const VariableValue &vv)
#define CLASSDESC_ACCESS(type)
Units units
dimension units of this value
Definition: variableValue.h:54
const Hypercube & hypercube(const Hypercube &hc) override
const Index & index(Index &&i) override
data structure supporting sliders - shared between constant operations and variables ...
Definition: slider.h:28
string canonicalName(const string &name)
convert a raw name into a canonical name - this is not idempotent.
Definition: valueId.cc:63
double setValue(size_t i, double x)
bool lhs() const
variable has an input port
Definition: variableValue.h:90
std::shared_ptr< ITensor > TensorPtr
static std::vector< double, CIVITA_ALLOCATOR< double > > flowVars
variables defined as a simple function of the stock variables, also known as lhs variables. These variables appear in the body of the Godley table
std::string m_init
the initial value of this variable
Definition: variableValue.h:87
string valueIdFromScope(const GroupPtr &scope, const std::string &name)
value Id from scope and canonical name name
Definition: valueId.cc:128
ITensor::Timestamp timestamp() const override
Definition: variableValue.h:83
std::string valueId() const
std::shared_ptr< Group > GroupPtr
Definition: port.h:32
double setValue(double x)