Minsky
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 "ecolab.h"
25 #include "classdesc_access.h"
26 #include "str.h"
27 #include "CSVDialog.h"
28 #include "latexMarkup.h"
29 #include "slider.h"
30 #include "valueId.h"
31 #include "variableSummary.h"
32 #include <regex>
33 #include <utility>
34 #include <boost/locale.hpp>
35 using namespace boost::locale::conv;
36 
37 namespace minsky
38 {
39  class VariableValue;
40  class VariableValues;
41  class Group;
42  typedef std::shared_ptr<Group> GroupPtr;
43  using namespace civita;
44 
45  struct VariableValueData: public civita::ITensorVal, public Slider
46  {
47  using ITensorVal::operator=;
48 
50  TensorVal tensorInit;
53 
56  bool unitsCached=false; // optimisation to prevent evaluating this units value more than once
57  void setUnits(const std::string& x) {units=Units(x);}
58 
60  std::string detailedText, tooltip;
61 
62  bool godleyOverridden=false;
63  std::string name; // name of this variable
64  classdesc::Exclude<std::weak_ptr<Group>> m_scope;
65 
68 
69  };
70 
72  {
74  private:
76  int m_idx=-1;
77  double& valRef();
78  const double& valRef() const;
79 
80  friend class VariableManager;
81  friend struct SchemaHelper;
82 
83  // values are always live
84  ITensor::Timestamp timestamp() const override {return Timestamp::clock::now();}
85 
86  protected: // protected to allow creation of zero/one constants.
88  std::string m_init;
89  public:
91  bool lhs() const {
92  return m_type==flow || m_type==tempFlow;}
94  bool temp() const {
95  return type()==tempFlow || type()==undefined;}
97  bool isFlowVar() const {
98  return m_type!=stock && m_type!=integral && m_type!=undefined;
99  }
100  bool isZero() const {
101  return m_type==constant && (init().empty() || init()=="0");
102  }
103 
104  VariableType::Type type() const {return m_type;}
105 
108  // name valueAt to allow TCL exposure
109  double valueAt(size_t i) const {
110  if (i>=size())
111  throw std::runtime_error("Index out of range");
112  return operator[](i);
113  }
114  double value() const {return valueAt(0);}
116  double setValue(size_t i, double x) {
117  if (i>=size())
118  throw std::runtime_error("Index out of range");
119  return operator[](i)=x;
120  }
121  double setValue(double x) {return setValue(0,x);}
122  int idx() const {return m_idx;}
123  void reset_idx() {m_idx=-1;}
124 
125  double operator[](std::size_t i) const override {return *(&valRef()+i);}
126  double& operator[](std::size_t i) override;
127 
128  const Index& index(Index&& i) override {
129  std::size_t prevNumElems = size();
130  m_index=i;
131  if (idx()==-1 || !idxInRange() || (prevNumElems<size()))
132  allocValue();
133  assert(idxInRange());
134  return m_index;
135  }
136  using ITensorVal::index;
137 
138  template <class T>
139  void hypercube_(T x) {
140  assert(idx()==-1||idxInRange());
141  std::size_t prevNumElems = size();
142  ITensor::hypercube(x);
143  if (idx()==-1 || (prevNumElems<size()))
144  allocValue();
145  assert(idxInRange());
146  }
147 
148  bool idxInRange() const;
149 
150  const Hypercube& hypercube() const override;
151  const Hypercube& hypercube(const Hypercube& hc) override
152  {hypercube_(hc); return m_hypercube;}
153  const Hypercube& hypercube(Hypercube&& hc) override
154  {hypercube_(hc); return m_hypercube;}
155  using ITensorVal::hypercube;
156 
157  VariableValue(VariableType::Type type=VariableType::undefined, const std::string& name="", const GroupPtr& group=GroupPtr()):
158  m_type(type)
159  {
160  this->name=utf_to_utf<char>(name);
161  m_scope=scope(group,name);
162  }
163 
165  m_type=type;
166  init(vv.init());
167  }
168 
169  VariableValue(const VariableValue&)=delete;
170  void operator=(const VariableValue&)=delete;
171 
172  using ITensorVal::operator=;
173  VariableValue& operator=(TensorVal const&);
174  VariableValue& operator=(const ITensor& x) override;
175 
177  VariableValue& allocValue();
178 
179  std::string valueId() const {return valueIdFromScope(m_scope.lock(),canonicalName(name));}
180 
182  void exportAsCSV(const std::string& filename, const std::string& comment="", bool tabular=false) const;
183 
184  Summary summary() const;
185 
186  const std::string& init() const {return m_init;}
187  const std::string& init(const std::string& x);
188 
190  void sliderSet(double x);
192  void incrSlider(double step);
193 
195  void adjustSliderBounds();
196  };
197 
198  struct ValueVector
199  {
202  static std::vector<double,CIVITA_ALLOCATOR<double>> stockVars;
206  static std::vector<double,CIVITA_ALLOCATOR<double>> flowVars;
207  };
208 
209 }
210 
211 #include "variableValue.cd"
212 #include "variableValue.xcd"
213 
214 #ifdef _CLASSDESC
215 #pragma omit pack minsky::VariableValue
216 #pragma omit unpack minsky::VariableValue
217 #endif
218 
219 namespace classdesc_access
220 {
221  // nobble these as we're not using them
222  template <>
223  struct access_pack<minsky::VariableValue>:
224  public classdesc::NullDescriptor<classdesc::pack_t> {};
225  template <>
226  struct access_unpack<minsky::VariableValue>:
227  public classdesc::NullDescriptor<classdesc::unpack_t> {};
228 }
229 
230 #endif
double value() const
set the value at the ith location
classdesc::Exclude< std::weak_ptr< Group > > m_scope
Definition: variableValue.h:64
bool isFlowVar() const
returns true if variable&#39;s data is allocated on the flowVariables vector
Definition: variableValue.h:97
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:57
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:52
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:67
struct TCLcmd::trap::init_t init
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
TensorVal tensorInit
when init is a tensor of values, this overrides the init string
Definition: variableValue.h:50
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:94
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:55
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
step
Definition: minsky.tcl:1289
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:91
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:88
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:84
std::string valueId() const
std::shared_ptr< Group > GroupPtr
Definition: port.h:32
double setValue(double x)
Definition: group.tcl:84