JEMRIS 2.9.1
open-source MRI simulations
Loading...
Searching...
No Matches
Sample.h
Go to the documentation of this file.
1
5/*
6 * JEMRIS Copyright (C)
7 * 2006-2023 Tony Stoecker
8 * 2007-2018 Kaveh Vahedipour
9 * 2009-2019 Daniel Pflugfelder
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27#ifndef SAMPLE_H_
28#define SAMPLE_H_
29
30#include <stdexcept>
31#include <string>
32#include <fstream>
33#include <cstdlib>
34#include <iostream>
35#include <xercesc/dom/DOM.hpp>
36#include "rng.h"
37#include "Declarations.h"
38#include "sys/time.h"
39
41class CoilArray;
42
43using std::string;
44using std::ofstream;
45
46using namespace std;
47XERCES_CPP_NAMESPACE_USE
48
49
53template <class T = double>
54struct Ensemble {
55
56 std::vector<size_t> m_dims;
57 std::vector<T> m_data;
58 size_t m_nspins;
63 Ensemble () : m_nspins(0) {}
64
65
70 Clear();
71 }
72
73
79 inline size_t Size () const {
80 return m_data.size();
81 }
82
83
89 inline size_t NSpins () const {
90 return m_nspins;
91 }
92
93
99 inline size_t NProps () const {
100 return m_dims[0];
101 }
102
103
109 inline size_t& NProps () {
110 return m_dims[0];
111 }
112
113
117 inline void Clear () {
118 m_nspins = 0;
119 m_dims.clear();
120 m_data.clear();
121 };
122
123
127 inline void ClearData () {
128 m_nspins = 0;
129 //m_dims.clear();
130 m_data.clear();
131 };
132
133
137 inline void ResizeDims (const size_t ndim) {
138 m_dims.resize(ndim);
139 };
140
141
149 inline void Init (const size_t ndim, const size_t* dims, const size_t live) {
150
151 m_dims.resize(ndim);
152 std::copy (dims, dims+ndim, m_dims.begin());
153
154 // Add spatial dimensions + ID
155 m_dims[0] += 4;
156
157 // Set number of live spins (Generally less spins may have M0 > 0)
158 m_nspins = live;
159
160 Allocate();
161 Zero();
162
163 }
164
165
173 inline void Init (const std::vector<size_t>& dims, const size_t live) {
174
175 m_dims = dims;
176 // Add spatial dimensions + ID
177 m_dims[0] += 4;
178
179 // Set number of live spins (Generally less spins may have M0 > 0)
180 m_nspins = live;
181
182 Allocate();
183 Zero();
184
185 }
186
187
191 inline void Allocate () {
192 m_data.resize(NSpins() * NProps());
193 }
194
195
199 inline void Zero () {
200 m_data.assign(m_data.size(), 0.);
201 }
202
203
209 inline void Init (const size_t live) {
210 m_nspins = live;
211 Allocate();
212 Zero();
213 }
214
220 inline void Init (const size_t nprops, const size_t live) {
221 m_dims.push_back(nprops);
222 m_nspins = live;
223 Allocate();
224 Zero();
225 }
226
232 inline const vector<size_t> Dims () const {
233 return m_dims;
234 }
235
236
242 inline void SetDims (vector<size_t> d ) {
243 m_dims.resize(d.size());
244 for (int i=0;i<d.size();i++) m_dims[i]=d[i];
245 }
246
247
253 inline T& operator[] (const size_t pos) {
254 assert(pos >= 0);
255 assert(pos < m_data.size());
256 return m_data[pos];
257 }
258
264 inline T operator[] (size_t pos) const {
265 assert(pos >= 0);
266 assert(pos < m_data.size());
267 return m_data[pos];
268 }
269
275 inline T* Data () {
276 return m_data.data();
277 }
278
279
280 typename std::vector<T>::iterator At (const size_t n) {
281 return m_data.begin() + n;
282 }
283
284 typename std::vector<T>::const_iterator At (const size_t n) const {
285 return m_data.begin() + n;
286 }
287};
288
289
293//template <class T = double>
294struct Spin {
295 size_t size;
297};
298
299
301class Sample {
302
303
304 public:
305
309 Sample ();
310
318 Sample (const string& file, const int multiple = 1);
319
327 Sample (const size_t size);
328
332 virtual ~Sample ();
333
338 virtual void Prepare (const std::string& fname = "");
339
343 void ClearSpins ();
344
350 void CreateSpins (const size_t size);
351
357 void CreateSpins (const size_t nprops, const size_t size);
358
364 size_t GetSize () const;
365
371 inline size_t GetNProps () const {return m_ensemble.NProps();};
372
378 inline vector<size_t> GetSampleDims () const {return m_ensemble.Dims();};
379
385 inline void SetSampleDims (vector<size_t> d) {m_ensemble.SetDims(d);};
386
392 inline const int GetSampleDimsSize () const { return m_ensemble.Dims().size(); }
393
394
395
402 Sample* GetSubSample (const int n, const size_t size);
403
410 void GetValues (const size_t l, double* val) ;
411
417 double* GetResolution() { return &m_res[0]; };
418
424 void InitRandGenerator (int val=1) { m_rng = RNG( val * (long ) clock() ); };
425
431 void SetR2Prime (double val) {m_r2prime = val;};
432
438 void SetPositionRandomness (double val) {m_pos_rand_perc = val;};
439
449 double GetDeltaB (size_t pos = -1);
450
457 virtual IO::Status Populate (const string& file);
458
462 double* GetSpinsData() {return m_ensemble.Data();};
463
467 void SetReorderStrategy(string strat);
468
472 void ReorderSample();
473
478 void DumpRestartInfo(CoilArray* RxCA);
479
484 void ReportSpin(int beg,int end,int value) {for (int i=beg; i<=end; i++) m_spin_state[i] = value;}
485
490 int ReadSpinsState();
491
496 void ClearSpinsState();
497
502 void GetScatterVectors(int *sendcount, int *displ,int size);
503
508 void GetNextPacket(int &noSpins, int &NextSpinToSend, int SlaveId);
509
513 int SpinsLeft();
514
518 void SetTimeInterval(double val) {m_sent_interval=val;};
519
523 bool IsRestart() {return m_is_restart;};
524
525 // HACK (bit more explanation would have been helpful)
526 void GetHelper (double* target);
527
528 double* GetHelper ();
529
530 size_t GetHelperSize ();
531
532 int GetNoSpinCompartments ();
533
534 void SetNoSpinCompartments (int n);
535
536 void CreateHelper (const size_t l);
537
538 void CreateDims (const size_t l);
539
540 void CopyHelper (double* out);
541
542 virtual void CropEnumerate ();
543
544
545 protected:
546 void MultiplySample(int multiple);
549
550
551 vector<size_t> m_index;
552 vector<double> m_res;
553 vector<double> m_offset;
555 RNG m_rng;
556 double m_r2prime;
560
561// data for sending sample in parallel mode:
562 int m_max_paket_size;
565
566// bookkeeping for restart:
567 vector<char> m_spin_state;
569 vector<int> m_spins_sent;
571 vector<timeval> m_last_time;
575 double m_no_spins_done;
576
577 vector<double> m_helper;
578 int m_no_spin_compartments;
579
580};
581
582#endif /*SAMPLE_H_*/
Implementation of JEMRIS Declarations.
Status
Definition: Declarations.h:150
Coil configuration and sensitivities.
Definition: CoilArray.h:40
base class for different sample reorder strategies.
Definition: SampleReorderStrategyInterface.h:16
The Sample is the object to simulate. It contains the spins.
Definition: Sample.h:301
bool IsRestart()
true if simulation run is from a restart
Definition: Sample.h:523
SampleReorderStrategyInterface * m_reorder_strategy
Definition: Sample.h:559
void SetReorderStrategy(string strat)
Definition: Sample.cpp:358
void SetTimeInterval(double val)
Set Time interval in seconds after which new spins are sent (approx. value.)
Definition: Sample.h:518
Ensemble< double > m_ensemble
Definition: Sample.h:548
void GetValues(const size_t l, double *val)
Get values for spin l and deliver them in val.
Definition: Sample.cpp:325
double * GetResolution()
Get grid resolution.
Definition: Sample.h:417
int m_next_spin_to_send
Definition: Sample.h:564
virtual void Prepare(const std::string &fname="")
Definition: Sample.cpp:45
Sample()
Definition: Sample.cpp:102
double m_total_cpu_time
Definition: Sample.h:574
void CreateSpins(const size_t size)
create the spin structure
Definition: Sample.cpp:88
bool m_is_restart
Definition: Sample.h:568
void ClearSpinsState()
Utility function for restart: mark all spins as not simulated.
Definition: Sample.cpp:574
vector< size_t > GetSampleDims() const
Definition: Sample.h:378
RNG m_rng
Definition: Sample.h:555
vector< int > m_last_offset_sent
Definition: Sample.h:570
vector< timeval > m_last_time
Definition: Sample.h:571
double m_r2prime
Definition: Sample.h:556
void ClearSpins()
delete the spin structure
Definition: Sample.cpp:80
vector< int > m_spins_sent
Definition: Sample.h:569
Sample * GetSubSample(const int n, const size_t size)
vector< double > m_res
Definition: Sample.h:552
double GetDeltaB(size_t pos=-1)
Get off-resonance of a specific spin.
Definition: Sample.cpp:339
void SetPositionRandomness(double val)
Set the position randomness in per cent of the cartesian resolution.
Definition: Sample.h:438
void SetSampleDims(vector< size_t > d)
Definition: Sample.h:385
int SpinsLeft()
Returns No spins which still needs to be calculated.
Definition: Sample.cpp:582
int ReadSpinsState()
Utility function for restart: Read restart file after crash.
Definition: Sample.cpp:537
const int GetSampleDimsSize() const
Dimensions.
Definition: Sample.h:392
double m_sent_interval
Definition: Sample.h:572
void ReorderSample()
Definition: Sample.cpp:351
double * GetSpinsData()
Definition: Sample.h:462
int m_min_paket_size
Definition: Sample.h:563
double m_pos_rand_perc
Definition: Sample.h:557
void DumpRestartInfo(CoilArray *RxCA)
Utility function for restart: dump information to restart jemris after crash.
Definition: Sample.cpp:522
virtual IO::Status Populate(const string &file)
Definition: Sample.cpp:155
void SetR2Prime(double val)
Set the random local field fluctuations.
Definition: Sample.h:431
void ReportSpin(int beg, int end, int value)
Utility function for restart: mark spins which have been calculated.
Definition: Sample.h:484
size_t GetSize() const
Definition: Sample.cpp:320
void GetScatterVectors(int *sendcount, int *displ, int size)
utility function to send sample in parallel mode: initial samples are send via mpi_scatterv; get need...
Definition: Sample.cpp:366
vector< double > m_offset
Definition: Sample.h:553
virtual ~Sample()
Definition: Sample.cpp:110
void GetNextPacket(int &noSpins, int &NextSpinToSend, int SlaveId)
utility function to send sample in parallel mode: get next spin packet to be sent....
Definition: Sample.cpp:458
size_t GetNProps() const
Definition: Sample.h:371
void InitRandGenerator(int val=1)
Initialize the randome number generator.
Definition: Sample.h:424
Spin ensemble.
Definition: Sample.h:54
size_t Size() const
Number of raw data elements.
Definition: Sample.h:79
const vector< size_t > Dims() const
get Dimensions
Definition: Sample.h:232
void Init(const size_t live)
Initialize data store number of non-zero spins only (MPI slaves)
Definition: Sample.h:209
void ResizeDims(const size_t ndim)
Clear data store and dimensions.
Definition: Sample.h:137
T & operator[](const size_t pos)
Access to position in store.
Definition: Sample.h:253
~Ensemble()
Destruct.
Definition: Sample.h:69
size_t NSpins() const
Number of spins in data store.
Definition: Sample.h:89
void SetDims(vector< size_t > d)
set Dimensions
Definition: Sample.h:242
T * Data()
Access to data.
Definition: Sample.h:275
void Init(const std::vector< size_t > &dims, const size_t live)
Initialize data store with dimensions and number of non-zero spins.
Definition: Sample.h:173
std::vector< T > m_data
Definition: Sample.h:57
void Clear()
Clear data store and dimensions.
Definition: Sample.h:117
std::vector< size_t > m_dims
Definition: Sample.h:56
Ensemble()
Construct.
Definition: Sample.h:63
size_t & NProps()
Reference to innermost data dimensions.
Definition: Sample.h:109
void ClearData()
Clear data store and dimensions.
Definition: Sample.h:127
size_t m_nspins
Definition: Sample.h:58
void Zero()
Zero data.
Definition: Sample.h:199
size_t NProps() const
Innermost data dimension containing physical parameters of every isochromat.
Definition: Sample.h:99
void Allocate()
Allocate RAM.
Definition: Sample.h:191
void Init(const size_t ndim, const size_t *dims, const size_t live)
Initialize data store with dimensions and number of non-zero spins.
Definition: Sample.h:149
void Init(const size_t nprops, const size_t live)
Initialize data store number of non-zero spins only (MPI slaves)
Definition: Sample.h:220
a container of spins
Definition: Sample.h:294
size_t size
Definition: Sample.h:295
Ensemble< double > * data
Definition: Sample.h:296

-- last change 03.10.2023 | Tony Stoecker | Imprint | Data Protection --