-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLooping.cc
385 lines (350 loc) · 10.9 KB
/
Looping.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Components for looping over arrays
#include "NMRsim.h"
#include "Parser.h"
static size_t array_loop=0;
bool update_vars=true;
size_t ndims=-1;
size_t nacqdims=-1;
LIST<size_t> sum_ns,array_ns;
size_t sum_n0=-1;
size_t array_n0=-1;
//size_t nD_n0=-1;
LIST<size_t> array_skips,arrayinds;
bool active2D=false; //!< true if nD sequence
LIST<size_t> skips(MAX_DIMENSIONS,1U); //!< only used for reporting (better merged)
LIST<int> ns(MAX_DIMENSIONS,0); //!< only used for reporting (better merged)
LIST<double> sws(MAX_DIMENSIONS,0.0);
//LIST<double> sfrqs(MAX_DIMENSIONS,0.0);
SystemVariable<int*> v_ni("ni",&(ns.front()),V_ISFIXED | V_ISCONST);
SystemVariable<double*> v_sw1("sw1",&(sws.front()),1.0,V_ISFIXED | V_ISCONST);
int row_index=-1;
int var_index=-1;
int sum_index=-1;
void ensure_array()
{
if (array_ns.empty() && (array_n0>1))
array_ns.create(1,array_n0);
array_loop=array_n0 ? array_n0 : 1;
}
//!< almighty fudge - changing data set size doesn't sit naturally with looping etc.
void update_array_loop(size_t nr)
{
if ( (array_ns.size()>1) || have_virtualdimensions)
error_abort("Processing which changes number of rows is currently incompatible with multi-dimensional data and/or virtual dimensions");
array_loop=array_n0=nr;
if (array_ns.size()==1)
array_ns.front()=nr;
}
size_t array_iter::size() const
{
if (!array_loop)
throw InternalError("array_iter::size");
return array_loop;
}
array_iter::array_iter(bool updatevarsv)
: updatevars_(updatevarsv), iter(array_ns)
{
var_index=-1;
row_index=-1;
finished=false;
if (array_loop==0)
throw InternalError("array_iter: array_loop not set");
// if (updatevars_)
// varinds.create(ndims-1);
}
ThreadWarning<> arrayambiguity_warning("list length is ambiguous; values will be cycled with increments",&NMRsim_once_warning);
//Warning<> arrayincommensurate_warning("variable array length incommensurate with data array length",&NMRsim_once_warning);
static void gettranslation(size_t& skipfac,size_t& modfac,size_t dim,size_t arraylen)
{
if (dim) { //virtual dimension
skipfac=array_skips(dim-1);
modfac=array_ns(dim-1);
}
else {
skipfac=active2D ? array_skips.front() : 1; // if no explicit tag use indirect dimension skip if nD
modfac=array_n0;
}
//const bool suppress=!allowwarnings();
char buf[256];
if (skipfac>1) {
const size_t actrows=(modfac+skipfac-1)/skipfac; //!< rather wasteful re-evaluating this each time...
if ((arraylen==skipfac) && (arraylen==actrows)) {
std::cerr << "Ambiguity in dimension " << dim << ": " << "array of length " << arraylen << " could cycle with quadrature factor or number of increments - resolve by specifying values for each row\n";
error_abort();
}
if (arraylen==skipfac) {
modfac=skipfac;
skipfac=1;
}
else {
if (arraylen==modfac)
skipfac=1;
else {
if (arraylen==actrows)
modfac=actrows;
else {
snprintf(buf,sizeof(buf)," (array length=%lu)",(unsigned long)arraylen);
arrayambiguity_warning.raise(buf);
skipfac=1;
return; //!< not sure about this...
}
}
}
}
// if (suppress)
// return;
bool incom=false;
// const size_t totfac=modfac*skipfac;
if (arraylen<modfac)
incom=(modfac % arraylen);
else
incom=(arraylen % modfac);
if (incom) {
std::cerr << "Variable array length (" << arraylen << ") doesn't match data array size (" << modfac << "). Lists of incompatible length (check for earlier warnings)?\n";
error_abort();
// arrayincommensurate_warning.raise(buf,true);
}
if (arraylen<modfac)
modfac=arraylen;
// modfac=skipfac*arraylen; //!< no wrap-around
}
bool getindex(size_t& transindex, size_t tag, size_t arraylen)
{
if (arraylen==1) {
transindex=0;
return false;
}
size_t usei;
if (tag) {
if (arrayinds.empty())
throw InternalError("getindex: called before row iterator set up");
usei=arrayinds(tag-1);
}
else {
if (row_index<0) //!< change from var_index 17/10/13 - var index not always updated
throw InternalError("getindex: row_index not valid");
usei=row_index;
}
size_t skipfac,modfac;
gettranslation(skipfac,modfac,tag,arraylen);
bool update=true;
if (skipfac>1) {
if (usei % skipfac)
update=false;
usei/=skipfac;
}
transindex=usei % modfac;
if ((verbose & VER_GEN) && (verbose_level>1)) {
if (update)
std::cout << "Translated global row index " << usei << " into parameter index " << transindex << " (actual index: " << usei << " modulation factor: " << modfac << " skip factor: " << skipfac << ")\n";
else
std::cout << "No update for current row index " << usei << '\n';
}
return update;
}
size_t VariableBase::summationindex() const
{
if (!isarray())
throw InternalError("VariableBase::summationindex");
size_t which=0;
if (array.size()>1) {
if (sum_index<0)
// error_abort("attempt to use sum array || outside valid region (processing command belongs in initialproc?)");
error_abort("attempt to use sum array || outside valid region");
which=(sumtag_ ? suminds(sumtag_-1) : sum_index) % array.size();
}
return which;
}
// const BaseList<double> VariableBase::getsummation() const
// {
// assert(isarray());
// size_t which=0;
// if (array.size()>1) {
// if (sum_index<0)
// throw Failed("sum_index used outside valid region");
// which=(sumtag_ ? suminds(sumtag_-1) : sum_index) % array.size();
// }
// return array(which);
// }
std::pair<size_t,bool> VariableBase::getoffset() const
{
const size_t sblock=summationindex();
const size_t bsize=array.size(sblock);
if (bsize==arraylength_)
return std::pair<size_t,bool>(array.offset(sblock),true);
size_t transindex;
const size_t tag = arraytags_.empty() ? 0 : arraytags_(sblock);
const bool needup=getindex(transindex,tag,bsize);
return std::pair<size_t,bool>(array.offset(sblock,transindex),needup);
}
const BaseList<double> VariableBase::get_row(size_t i) const
{
return BaseList<double>(arraylength_, const_cast<double*> (&(array.row()(i*arraylength_))));
}
BaseList<double> VariableBase::get_row(size_t i)
{
if (!nochecks) {
const size_t offset=(i+1)*arraylength_;
if (offset>array.items())
throw BadIndex("VariableBase::get_row",offset,array.items());
}
return BaseList<double>(arraylength_, &(array.row()(i*arraylength_)));
}
void VariableBase::set_current_row(const BaseList<double>& vs)
{
if (!isarray())
throw InternalError("VariableBase::set_current_row");
std::pair<size_t,bool> res(getoffset());
BaseList<double> currow(get_row(res.first));
currow=vs;
value_=vs; //!< make sure current value is also updated
}
bool VariableBase::updateindex()
{
static LIST<double> tmp;
if (!isarray())
throw InternalError("VariableBase::updateindex");
if ((array.size()>1) && (sum_index<0))
return false; //!< don't update summation arrays outside region where valid
std::pair<size_t,bool> res(getoffset());
// if ((verbose & VER_GEN) & (verbose_level>1))
// std::cout << "Index: " << res.first << " Update: " << (res.second ? "Yes\n" : "No\n");
if (res.second) {
if (arraylength_==1) {
double& val(array.row()(res.first));
if (!(arrayexprs_.empty())) {
const Expression* exprp=arrayexprs_(res.first);
if (exprp) {
exprp->get(tmp);
const size_t ind=exproffsets_(res.first);
if (ind>=tmp.size()) {
std::cerr << "Expression in array has returned fewer elements (" << tmp.size() << ") than required (" << (ind+1) << ")\n";
error_abort();
}
val=tmp(ind);
}
}
value_.create(1,val);
}
else {
if (!(arrayexprs_.empty()))
throw InternalError("can't have expression array with array_length!=1");
value_=get_row(res.first);
}
return true;
}
return false;
}
size_t getarrayindex(size_t i)
{
size_t res=arrayinds(i);
if (active2D)
res/=array_skips(i);
return res;
}
bool array_iter::next(const DataStore* datap)
{
if (finished)
return false;
row_index++;
if (datap)
current_data_row.create(datap->row(row_index));
else
current_data_row.clear(); //!< can't be valid
// const bool forceupdate=(optupdate==0) || !active2D;
update_vars=(row_index==0) || !active2D || !optupdate;
if (updatevars_) {
NMRSIM_EXPECT(iter.next(arrayinds));
// if (active2D) {
// for (size_t i=ndims-1;i--;) {
// // if ((arrayinds(i) % array_skips(i))==0) {
// varinds(i)=arrayinds(i)/array_skips(i);
// //update_vars=true;
// //}
// }
// }
// else
// varinds=arrayinds;
// bool updateexpr=forceupdate;
var_index++;
//if ((verbose & VER_GEN) && (verbose_level>1))
// std::cout << "Parameter index: " << var_index << " indices: " << varinds << '\n';
// update_variables_expressions(varpars,var_index,varinds);
const varpars_t::iterator end=varpars.end();
varpars_t::iterator start=varpars.begin();
while (start!=end) {
if (start->variable().updateindex()) {
start->rawupdate();
update_vars=true; //!< flag that at least one variable has changed
}
++start;
}
// update_variables(varpars,var_index,varinds);
// "Updating expressions" redundant if followed by updates
if ((verbose & VER_GEN) && (!update_vars || (verbose_level>1)))
std::cout << "Updating expressions: " << (update_vars ? "Yes\n" : "No\n");
if (update_vars)
update_expressions();
}
if (update_vars)
update_auxiliary_vars();
finished=(row_index==array_loop-1);
return true;
}
bool accumulator::add(size_t n)
{
if ((length_==1) || (length_<0))
length_=n;
else {
if ((n!=1) && (n!=length_)) {
failed_=false;
return false;
}
}
return true;
}
size_t accumulator::operator()() const
{
if (length_<0)
throw Undefined("accumulator");
if (failed_)
throw Failed("accumulator: incompatible list sizes");
return length_;
}
void ensure_dimension(size_t i)
{
ScratchList<bool,MAX_DIMENSIONS> done(MAX_DIMENSIONS,false);
if (done(i))
return;
char vname[6];
snprintf(vname,sizeof(vname),"n%lu",(unsigned long)i);
add_systemvarmap(*(new SystemVariable<int*>(vname,&(ns(i-1)),V_ISFIXED | V_ISCONST)));
snprintf(vname,sizeof(vname),"sw%lu",(unsigned long)i);
add_systemvarmap(*(new SystemVariable<double*>(vname,&(sws(i-1)),1.0,V_ISFIXED | V_ISCONST)));
}
void raw_set_n(int dim, int n, int ni_skip)
{
ns(dim-1)=n; //!< only used for reporting
skips(dim-1)=ni_skip;
}
void set_n(int dim, int n, int ni_skip)
{
ensure_dimension(dim);
array_dims.set(dim,n,ni_skip);
raw_set_n(dim,n,ni_skip);
active2D=true;
}
void parse_swn(int n)
{
ensure_dimension(n);
double& cursw(sws(n-1));
if (cursw)
error_abort("cannot change indirect dimension spectral width");
cursw=parse_double();
if (cursw<=0.0)
error_abort("spectral width cannot be <=0!");
// if (are_left())
// sfrqs(n-1)=parse_double();
if (n==1)
v_sw1.isconstant(true); //!< flag that sw1 is now fixed
}