ARTS  2.3.1285(git:92a29ea9-dirty)
interactive_workspace.cc
Go to the documentation of this file.
2 #include "matpackI.h"
3 #include "matpackII.h"
4 #include "matpackIII.h"
5 #include "matpackIV.h"
6 #include "matpackV.h"
7 #include "matpackVI.h"
8 #include "matpackVII.h"
9 #include "methods.h"
11 #include "agenda_class.h"
12 #include "agenda_record.h"
13 
15 extern std::string string_buffer;
16 
17 namespace global_data {
18 extern map<String, Index> AgendaMap;
20 extern map<String, Index> WsvGroupMap;
22 }
23 
26 
27 Index get_wsv_id(const char *);
28 
30 std::vector<Callback *> InteractiveWorkspace::callbacks_{};
31 
32 void callback_getaway(Workspace &ws, const MRecord &mr) {
33  InteractiveWorkspace &iws = *reinterpret_cast<InteractiveWorkspace *>(&ws);
34 #pragma omp critical
35  { iws.execute_callback(static_cast<Index>(mr.SetValue())); }
36 }
37 
38 MdRecord callback_mr = MdRecord("APICallback",
39  "",
40  ArrayOfString(),
41  ArrayOfString(),
42  ArrayOfString(),
43  ArrayOfString(),
44  ArrayOfString(),
45  ArrayOfString(),
46  ArrayOfString(),
47  ArrayOfString(),
48  ArrayOfString(),
49  ArrayOfString(),
50  false,
51  false,
52  false,
53  false,
54  false);
55 
57  const Index agenda_verbosity)
58  : Workspace() {
60  verbosity_at_launch.set_screen_verbosity(verbosity);
61  verbosity_at_launch.set_agenda_verbosity(agenda_verbosity);
62  // No report file is used for the C interface
63  verbosity_at_launch.set_file_verbosity(0);
64 
65  // Set names of agendas.
68  const Index WsvAgendaGroupIndex = WsvGroupMap.find("Agenda")->second;
69  for (const auto & agenda_iterator : AgendaMap) {
70  const auto & wsv_iterator = Workspace::WsvMap.find(agenda_iterator.first);
71  if (!(wsv_iterator == Workspace::WsvMap.end())
72  && (wsv_data[wsv_iterator->second].Group() == WsvAgendaGroupIndex)) {
73  Agenda &agenda = *reinterpret_cast<Agenda *>(this->operator[](wsv_iterator->second));
74  agenda.set_name(agenda_iterator.first);
75  }
76  }
77 }
78 
85  define_md_map();
89  assert(check_agenda_data());
93 
94  // Add getaway for callbacks.
95  size_t n_methods = md_data.size();
96  getaways[n_methods] = &callback_getaway;
97  md_data.push_back(callback_mr);
98 }
99 
101  // Need to check size of stack as agenda definitions may have
102  // added variables.
103  if (wsv_data.size() != ws.size()) {
104  resize();
105  }
106  try {
107  a->execute(*this);
108  } catch (const std::exception &e) {
109  string_buffer = e.what();
110  return string_buffer.c_str();
111  }
112  return nullptr;
113 }
114 
116  long id, const ArrayOfIndex &output, const ArrayOfIndex &input) {
117  const MdRecord &m = md_data[id];
118 
119  // Need to check size of stack as agenda definitions may have
120  // added variables.
121  if (wsv_data.size() != ws.size()) {
122  resize();
123  }
124 
125  // Check if all input variables are initialized.
126  for (Index i : input) {
127  if (!is_initialized(i)) {
128  string_buffer = "Method " + m.Name() + " needs input " +
129  wsv_data[i].Name() + " but it is uninitialized.";
130  return string_buffer.c_str();
131  }
132  }
133 
134  // Make sure verbosity is set.
135  Index wsv_id_verbosity = get_wsv_id("verbosity");
136  Verbosity &verbosity = *((Verbosity *)this->operator[](wsv_id_verbosity));
137  verbosity.set_main_agenda(true);
138 
139  CREATE_OUTS;
140 
141  if (m.SetMethod()) {
142  swap(output[0], input[0]);
143  return nullptr;
144  }
145 
146  TokVal t{};
147  Agenda a{};
148  try {
149  MRecord mr(id, output, input, t, a);
150  if (mr.isInternal()) {
151  out3 << "- " + m.Name() + "\n";
152  } else {
153  out1 << "- " + m.Name() + "\n";
154  }
155  getaways[id](*this, mr);
156  } catch (const std::exception &e) {
157  string_buffer = e.what();
158  return string_buffer.c_str();
159  }
160  return nullptr;
161 }
162 
165  Agenda &dst = *reinterpret_cast<Agenda *>(this->operator[](id));
166  String old_name = dst.name();
167  dst = src;
168  if (old_name != "") {
169  dst.set_name(old_name);
170  }
171  dst.check(*this, verbosity_at_launch);
172 }
173 
175  *reinterpret_cast<Index *>(this->operator[](id)) = src;
176 }
177 
179  *reinterpret_cast<Numeric *>(this->operator[](id)) = src;
180 }
181 
183  *reinterpret_cast<String *>(this->operator[](id)) = src;
184 }
185 
187  Index id, size_t n, const char *const *src) {
188  ArrayOfString *dst = reinterpret_cast<ArrayOfString *>(this->operator[](id));
189  dst->resize(n);
190  for (size_t i = 0; i < n; ++i) {
191  dst->operator[](i) = String(src[i]);
192  }
193 }
194 
196  size_t n,
197  const Index *src) {
198  ArrayOfIndex *dst = reinterpret_cast<ArrayOfIndex *>(this->operator[](id));
199  dst->resize(n);
200  for (size_t i = 0; i < n; ++i) {
201  dst->operator[](i) = src[i];
202  }
203 }
204 
206  size_t n,
207  const Numeric *src) {
208  Vector *dst = reinterpret_cast<Vector *>(this->operator[](id));
209  dst->resize(n);
210  for (size_t i = 0; i < n; ++i) {
211  dst->operator[](i) = src[i];
212  }
213 }
214 
216  size_t m,
217  size_t n,
218  const Numeric *src) {
219  Matrix *dst = reinterpret_cast<Matrix *>(this->operator[](id));
220  dst->resize(m, n);
221  for (size_t i = 0; i < n * m; ++i) {
222  dst->get_c_array()[i] = src[i];
223  }
224 }
225 
227  Index id, size_t l, size_t m, size_t n, const Numeric *src) {
228  Tensor3 *dst = reinterpret_cast<Tensor3 *>(this->operator[](id));
229  dst->resize(l, m, n);
230  for (size_t i = 0; i < l * n * m; ++i) {
231  dst->get_c_array()[i] = src[i];
232  }
233 }
234 
236  Index id, size_t k, size_t l, size_t m, size_t n, const Numeric *src) {
237  Tensor4 *dst = reinterpret_cast<Tensor4 *>(this->operator[](id));
238  dst->resize(k, l, m, n);
239  for (size_t i = 0; i < k * l * m * n; ++i) {
240  dst->get_c_array()[i] = src[i];
241  }
242 }
243 
245  size_t k,
246  size_t l,
247  size_t m,
248  size_t n,
249  size_t o,
250  const Numeric *src) {
251  Tensor5 *dst = reinterpret_cast<Tensor5 *>(this->operator[](id));
252  dst->resize(k, l, m, n, o);
253  for (size_t i = 0; i < k * l * m * n * o; ++i) {
254  dst->get_c_array()[i] = src[i];
255  }
256 }
257 
259  size_t k,
260  size_t l,
261  size_t m,
262  size_t n,
263  size_t o,
264  size_t p,
265  const Numeric *src) {
266  Tensor6 *dst = reinterpret_cast<Tensor6 *>(this->operator[](id));
267  dst->resize(k, l, m, n, o, p);
268  for (size_t i = 0; i < k * l * m * n * o * p; ++i) {
269  dst->get_c_array()[i] = src[i];
270  }
271 }
272 
274  size_t k,
275  size_t l,
276  size_t m,
277  size_t n,
278  size_t o,
279  size_t p,
280  size_t q,
281  const Numeric *src) {
282  Tensor7 *dst = reinterpret_cast<Tensor7 *>(this->operator[](id));
283  dst->resize(k, l, m, n, o, p, q);
284  for (size_t i = 0; i < k * l * m * n * o * p * q; ++i) {
285  dst->get_c_array()[i] = src[i];
286  }
287 }
288 
290  Index m,
291  Index n,
292  Index nnz,
293  const Numeric *src,
294  const int *row_inds,
295  const int *col_inds) {
296  Sparse *dst = reinterpret_cast<Sparse *>(this->operator[](id));
297  *dst = Sparse(m, n);
298 
299  Vector elements(nnz);
300  ArrayOfIndex row_indices(nnz), column_indices(nnz);
301 
302  for (size_t i = 0; i < (size_t)nnz; ++i) {
303  elements[i] = src[i];
304  row_indices[i] = row_inds[i];
305  column_indices[i] = col_inds[i];
306  }
307 
308  dst->insert_elements(nnz, row_indices, column_indices, elements);
309 }
310 
313  std::copy(ws.begin(), ws.end(), ws_new.begin());
314  std::swap(ws, ws_new);
315 }
316 
318  this->operator[](i);
319  this->pop_free(i);
320  this->operator[](i);
321 }
322 
323 Index InteractiveWorkspace::add_variable(Index group_id, const char *name) {
324 
325  if (wsv_data.size() != ws.size()) {
326  resize();
327  }
328 
329  Index id = static_cast<Index>(ws.size());
330 
331  ws.push_back(stack<WsvStruct *>());
332  push(ws.size() - 1, nullptr);
333  ws.back().top()->wsv = workspace_memory_handler.allocate(group_id);
334  ws.back().top()->auto_allocated = true;
335  ws.back().top()->initialized = false;
336 
337  String s;
338  if (name) {
339  s = String(name);
340  } else {
341  std::stringstream stream;
342  stream << "anonymous_variable_" << n_anonymous_variables_;
343  s = stream.str();
344  }
345 
346  wsv_data.push_back(WsvRecord(s.c_str(), "Created by C API.", group_id));
347  WsvMap[s] = id;
348 
350  return id;
351 }
352 
354  WsvStruct *wsvs;
355  while (ws[i].size()) {
356  wsvs = ws[i].top();
357  if (wsvs->auto_allocated && wsvs->wsv) {
358  workspace_memory_handler.deallocate(group_id, wsvs->wsv);
359  }
360  delete (wsvs);
361  ws[i].pop();
362  }
363  ws.erase(ws.begin() + i);
364 
365  WsvMap.erase(wsv_data[i].Name());
366  wsv_data.erase(wsv_data.begin() + i);
368 }
369 
371  if (is_initialized(i) && is_initialized(j)) {
372  std::swap(ws[i], ws[j]);
373  } else if (!is_initialized(i) && is_initialized(j)) {
374  ws[i].push(ws[j].top());
375  ws[j].pop();
376  } else if (is_initialized(i) && !is_initialized(j)) {
377  ws[j].push(ws[i].top());
378  ws[i].pop();
379  }
380 }
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
static Array< WsvRecord > wsv_data
Global WSV data.
Definition: workspace_ng.h:58
void copy(ConstComplexIterator1D origin, const ConstComplexIterator1D &end, ComplexIterator1D target)
Copy data between begin and end to target.
Definition: complex.cc:478
void execute_callback(Index callback_id)
void define_md_map()
Define MdMap.
Definition: methods_aux.cc:473
The Agenda class.
Definition: agenda_class.h:44
String name() const
Agenda name.
const char * execute_agenda(const Agenda *a)
Execute agenda.
Index nelem() const
Number of elements.
Definition: array.h:195
void resize(Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackV.cc:1743
This file contains all declarations of the ARTS C API.
void check(Workspace &ws, const Verbosity &verbosity)
Checks consistency of an agenda.
Definition: agenda_class.cc:80
map< String, Index > AgendaMap
The map associated with agenda_data.
Verbosity verbosity_at_launch
The global message verbosity settings:
Definition: messages.cc:34
void set_tensor7_variable(Index id, size_t k, size_t l, size_t m, size_t n, size_t o, size_t p, size_t q, const Numeric *src)
Deep-copy of Tensor5 variable into workspace.
void initialize()
Reset the size of the workspace.
Definition: workspace_ng.h:111
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackI.cc:735
The Vector class.
Definition: matpackI.h:860
The Sparse class.
Definition: matpackII.h:60
The Tensor4 class.
Definition: matpackIV.h:421
void initialize()
Initialization dispatch functions.
void(* getaways[])(Workspace &, const MRecord &)
Definition: auto_md.cc:21480
bool SetMethod() const
Definition: methods.h:105
void pop_free(Index i)
Remove the topmost WSV from its stack and free its memory.
bool is_initialized(Index i)
Checks existence of the given WSV.
Definition: workspace_ng.h:118
void set_agenda_verbosity(Index v)
Definition: messages.h:70
void set_index_variable(Index id, const Index &src)
Deep-copy of Index variable into workspace.
The Tensor7 class.
Definition: matpackVII.h:2382
All information for one workspace method.
Definition: methods.h:41
void set_matrix_variable(Index id, size_t m, size_t n, const Numeric *src)
Deep-copy of Matrix variable into workspace.
void define_species_map()
MdRecord callback_mr
void initialize_variable(Index id)
Initialize workspace variable.
const String & Name() const
Definition: methods.h:88
basic_stringstream< char, string_char_traits< char >, alloc > stringstream
Definition: sstream.h:205
void define_agenda_map()
void define_agenda_data()
Definition: agendas.cc:44
std::string string_buffer
Definition: arts_api.cc:22
static size_t n_anonymous_variables_
WorkspaceMemoryHandler workspace_memory_handler
The workspace memory handler Defined in workspace_ng.cc.
void expand_md_data_raw_to_md_data()
Expand supergeneric methods.
Definition: methods_aux.cc:410
The Tensor3 class.
Definition: matpackIII.h:339
void swap(ComplexVector &v1, ComplexVector &v2)
Swaps two objects.
Definition: complex.cc:731
InteractiveWorkspace(const Index verbosity=1, const Index agenda_verbosity=0)
void set_agenda_variable(Index id, const Agenda &src)
Set agenda variable into workspace.
void set_vector_variable(Index id, size_t n, const Numeric *src)
Deep-copy of Vector variable into workspace.
Header file for sparse matrices.
void resize()
Resize workspace stack.
static void define_wsv_data()
Define workspace variables.
Definition: workspace.cc:39
void set_tensor5_variable(Index id, size_t k, size_t l, size_t m, size_t n, size_t o, const Numeric *src)
Deep-copy of Tensor5 variable into workspace.
static void initialize()
Workspace intialization.
#define CREATE_OUTS
Definition: messages.h:209
Declarations for agendas.
void * operator[](Index i)
Retrieve a pointer to the given WSV.
void set_tensor6_variable(Index id, size_t k, size_t l, size_t m, size_t n, size_t o, size_t p, const Numeric *src)
Deep-copy of Tensor6 variable into workspace.
void set_file_verbosity(Index v)
Definition: messages.h:72
void set_main_agenda(bool main_agenda)
Definition: messages.h:73
Method runtime data.
Definition: agenda_class.h:121
bool check_agenda_data()
Check that agendas.cc and workspace.cc are consistent.
void set_numeric_variable(Index id, const Numeric &src)
Deep-copy of Numeric variable into workspace.
void set_sparse_variable(Index id, Index m, Index n, Index nnz, const Numeric *src, const int *row_indices, const int *column_indices)
Deep-copy of Sparse matrix into workspace.
void set_array_of_index_variable(Index id, size_t n, const Index *src)
Deep-copy of ArrayOfIndex variable into workspace.
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
void * allocate(Index group_index)
Allocate workspace WSV of given group.
The Matrix class.
Definition: matpackI.h:1193
Interactive ARTS workspace.
void execute(Workspace &ws) const
Execute an agenda.
Array< String > ArrayOfString
An array of Strings.
Definition: mystring.h:283
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackVII.cc:5012
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackV.cc:675
Implementation of Matrix, Vector, and such stuff.
void resize(Index p, Index r, Index c)
Resize function.
Definition: matpackIII.cc:664
void define_species_data()
void set_tensor3_variable(Index id, size_t l, size_t m, size_t n, const Numeric *src)
Deep-copy of Tensor3 variable into workspace.
void define_md_raw_map()
Define MdRawMap.
Definition: methods_aux.cc:509
void deallocate(Index group_index, void *ptr)
Getaway function to call the deallocation function for the WSV group with the given Index...
This can be used to make arrays out of anything.
Definition: array.h:40
bool isInternal() const
Indicates the origin of this method.
Definition: agenda_class.h:169
void push(Index i, void *wsv)
Push a new WSV onto its stack.
void define_wsv_group_names()
Define the array of workspace variable group names.
Definition: groups.cc:77
void set_name(const String &nname)
Set agenda name.
static map< String, Index > WsvMap
Global map associated with wsv_data.
Definition: workspace_ng.h:61
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackVI.cc:1734
void resize(Index n)
Resize function.
Definition: matpackI.cc:404
void set_tensor4_variable(Index id, size_t k, size_t l, size_t m, size_t n, const Numeric *src)
Deep-copy of Tensor4 variable into workspace.
void set_array_of_string_variable(Index id, size_t n, const char *const *src)
Deep-copy of ArrayOfString variable into workspace.
The Tensor6 class.
Definition: matpackVI.h:1088
const TokVal & SetValue() const
Definition: agenda_class.h:158
const map< String, Index > WsvGroupMap
The map associated with wsv_group_names.
Definition: groups.cc:41
void define_md_data_raw()
Definition: methods.cc:191
static std::vector< Callback * > callbacks_
const char * execute_workspace_method(long id, const ArrayOfIndex &output, const ArrayOfIndex &input)
Execute workspace method.
Array< stack< WsvStruct * > > ws
Workspace variable container.
Definition: workspace_ng.h:49
Workspace class.
Definition: workspace_ng.h:40
void resize(Index v, Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackVI.cc:2175
#define q
const Array< MdRecord > md_data
Lookup information for workspace methods.
static void define_wsv_map()
Map WSV names to indices.
Definition: workspace_ng.cc:48
Index add_variable(Index group_id, const char *name)
Push a stack for a new variable to the workspace.
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIII.cc:321
void callback_getaway(Workspace &ws, const MRecord &mr)
void erase_variable(Index i, Index group_id)
Remove a variable stack from the workspace.
This class contains all static information for one workspace variable.
Definition: wsv_aux.h:56
constexpr Rational end(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the largest M for a polarization type of this transition.
Definition: zeemandata.h:108
The WorkspaceMemoryHandler.
void resize(Index l, Index v, Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackVII.cc:5484
This stores arbitrary token values and remembers the type.
Definition: token.h:40
void swap(Index i, Index j)
Remove a variable stack from the workspace.
Handling of workspace memory.
void resize(Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackIV.cc:1064
Declaration of the class MdRecord.
void insert_elements(Index nnz, const ArrayOfIndex &rowind, const ArrayOfIndex &colind, ConstVectorView data)
Insert vector of elements with given row and column indices.
Definition: matpackII.cc:337
my_basic_string< char > String
The String type for ARTS.
Definition: mystring.h:280
Declarations for AgRecord, storing lookup information for one agenda.
Index get_wsv_id(const char *)
Get index of WSV.
Definition: workspace.cc:5771
The Tensor5 class.
Definition: matpackV.h:506
void set_string_variable(Index id, const char *src)
Deep-copy of String variable into workspace.
void set_screen_verbosity(Index v)
Definition: messages.h:71
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1056
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIV.cc:358