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