JEMRIS  2.8.1
open-source MRI simulations
Sample.h
Go to the documentation of this file.
1 
5 /*
6  * JEMRIS Copyright (C)
7  * 2006-2015 Tony Stoecker
8  * 2007-2015 Kaveh Vahedipour
9  * 2009-2015 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 
41 class CoilArray;
42 
43 using std::string;
44 using std::ofstream;
45 
46 using namespace std;
47 XERCES_CPP_NAMESPACE_USE
48 
49 
53 template <class T = double>
54 struct 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 
69  ~Ensemble () {
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 
141  inline void Init (const size_t ndim, const size_t* dims, const size_t live) {
142 
143  m_dims.resize(ndim);
144  std::copy (dims, dims+ndim, m_dims.begin());
145 
146  // Add spatial dimensions
147  m_dims[0] += 4;
148 
149  // Set number of live spins (Generally less spins may have M0 > 0)
150  m_nspins = live;
151 
152  Allocate();
153  Zero();
154 
155  }
156 
157 
165  inline void Init (const std::vector<size_t>& dims, const size_t live) {
166 
167  m_dims = dims;
168  // Add spatial dimensions
169  m_dims[0] += 4;
170 
171  // Set number of live spins (Generally less spins may have M0 > 0)
172  m_nspins = live;
173 
174  Allocate();
175  Zero();
176 
177  }
178 
179 
183  inline void Allocate () {
184  m_data.resize(NSpins() * NProps());
185  }
186 
187 
191  inline void Zero () {
192  m_data.assign(m_data.size(), 0.);
193  }
194 
195 
201  inline void Init (const size_t live) {
202  m_nspins = live;
203  Allocate();
204  Zero();
205  }
206 
212  inline void Init (const size_t nprops, const size_t live) {
213  m_dims.push_back(nprops);
214  m_nspins = live;
215  Allocate();
216  Zero();
217  }
218 
224  inline const size_t* Dims () const {
225  return m_dims.data();
226  }
227 
228 
234  inline T& operator[] (const size_t pos) {
235  assert(pos >= 0);
236  assert(pos < m_data.size());
237  return m_data[pos];
238  }
239 
245  inline T operator[] (size_t pos) const {
246  assert(pos >= 0);
247  assert(pos < m_data.size());
248  return m_data[pos];
249  }
250 
256  inline T* Data () {
257  return m_data.data();
258  }
259 
260 
261  typename std::vector<T>::iterator At (const size_t n) {
262  return m_data.begin() + n;
263  }
264 
265  typename std::vector<T>::const_iterator At (const size_t n) const {
266  return m_data.begin() + n;
267  }
268 };
269 
270 
274 //template <class T = double>
275 struct Spin {
276  size_t size;
278 };
279 
280 
282 class Sample {
283 
284 
285  public:
286 
290  Sample ();
291 
299  Sample (const string& file, const int multiple = 1);
300 
308  Sample (const size_t size);
309 
313  virtual ~Sample ();
314 
319  virtual void Prepare (const std::string& fname = "");
320 
324  void ClearSpins ();
325 
331  void CreateSpins (const size_t size);
332 
338  void CreateSpins (const size_t nprops, const size_t size);
339 
345  size_t GetSize () const;
346 
352  inline size_t GetNProps () const {return m_ensemble.NProps();};
353 
359  inline const size_t* GetSampleDims () const {return m_ensemble.Dims();};
360 
367  Sample* GetSubSample (const int n, const size_t size);
368 
375  void GetValues (const size_t l, double* val) ;
376 
382  double* GetResolution() { return &m_res[0]; };
383 
389  void InitRandGenerator (int val=1) { m_rng = RNG( val * (long ) clock() ); };
390 
396  void SetR2Prime (double val) {m_r2prime = val;};
397 
403  void SetPositionRandomness (double val) {m_pos_rand_perc = val;};
404 
414  double GetDeltaB (size_t pos = -1);
415 
422  virtual IO::Status Populate (const string& file);
423 
427  double* GetSpinsData() {return m_ensemble.Data();};
428 
432  void SetReorderStrategy(string strat);
433 
437  void ReorderSample();
438 
443  void DumpRestartInfo(CoilArray* RxCA);
444 
449  void ReportSpin(int beg,int end,int value) {for (int i=beg; i<=end; i++) m_spin_state[i] = value;}
450 
455  int ReadSpinsState();
456 
461  void ClearSpinsState();
462 
467  void GetScatterVectors(int *sendcount, int *displ,int size);
468 
473  void GetNextPacket(int &noSpins, int &NextSpinToSend, int SlaveId);
474 
478  int SpinsLeft();
479 
483  void SetTimeInterval(double val) {m_sent_interval=val;};
484 
485  bool IsRestart() {return m_is_restart;};
486 
487  void GetHelper (double* target);
488 
489  double* GetHelper ();
490 
491  size_t GetHelperSize ();
492 
493  // HACK
494  int GetNoSpinCompartments ();
495 
496  void SetNoSpinCompartments (int n);
497 
498  void CreateHelper (const size_t l);
499 
500  void CopyHelper (double* out);
501 
502  virtual void CropEnumerate ();
503 
504 
505  protected:
506  void MultiplySample(int multiple);
509 
510 
511  vector<size_t> m_index;
512  vector<double> m_res;
513  vector<double> m_offset;
515  RNG m_rng;
516  double m_r2prime;
520 
521 // data for sending sample in parallel mode:
522  int m_max_paket_size;
525 
526 // bookkeeping for restart:
527  vector<char> m_spin_state;
529  vector<int> m_spins_sent;
530  vector<int> m_last_offset_sent;
531  vector<timeval> m_last_time;
535  double m_no_spins_done;
536 
537  vector<double> m_helper;
538  int m_no_spin_compartments;
539 
540 };
541 
542 #endif /*SAMPLE_H_*/
Implementation of JEMRIS Declarations.
std::vector< size_t > m_dims
Definition: Sample.h:56
Ensemble< double > * data
Definition: Sample.h:277
bool m_is_restart
Definition: Sample.h:528
RNG m_rng
Definition: Sample.h:515
void SetR2Prime(double val)
Set the random local field fluctuations.
Definition: Sample.h:396
vector< double > m_res
Definition: Sample.h:512
const size_t * Dims() const
Dimensions.
Definition: Sample.h:224
Ensemble< double > m_ensemble
Definition: Sample.h:508
vector< timeval > m_last_time
Definition: Sample.h:531
double m_r2prime
Definition: Sample.h:516
size_t size
Definition: Sample.h:276
const size_t * GetSampleDims() const
Definition: Sample.h:359
size_t m_nspins
Definition: Sample.h:58
void Init(const size_t nprops, const size_t live)
Initialize data store number of non-zero spins only (MPI slaves)
Definition: Sample.h:212
vector< int > m_spins_sent
Definition: Sample.h:529
size_t Size() const
Number of raw data elements.
Definition: Sample.h:79
void Zero()
Zero data.
Definition: Sample.h:191
int m_next_spin_to_send
Definition: Sample.h:524
void Clear()
Clear data store and dimensions.
Definition: Sample.h:117
SampleReorderStrategyInterface * m_reorder_strategy
Definition: Sample.h:519
vector< double > m_offset
Definition: Sample.h:513
double m_total_cpu_time
Definition: Sample.h:534
base class for different sample reorder strategies.
Definition: SampleReorderStrategyInterface.h:16
size_t NProps() const
Innermost data dimension containing physical parameters of every isochromat.
Definition: Sample.h:99
double * GetSpinsData()
Definition: Sample.h:427
Coil configuration and sensitivities.
Definition: CoilArray.h:40
T * Data()
Access to data.
Definition: Sample.h:256
std::vector< T > m_data
Definition: Sample.h:57
void Allocate()
Allocate RAM.
Definition: Sample.h:183
size_t NSpins() const
Number of spins in data store.
Definition: Sample.h:89
Ensemble()
Construct.
Definition: Sample.h:63
Spin ensemble.
Definition: Sample.h:54
double m_sent_interval
Definition: Sample.h:532
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:141
double * GetResolution()
Get grid resolution.
Definition: Sample.h:382
int m_min_paket_size
Definition: Sample.h:523
vector< int > m_last_offset_sent
Definition: Sample.h:530
void InitRandGenerator(int val=1)
Initialize the randome number generator.
Definition: Sample.h:389
void ClearData()
Clear data store and dimensions.
Definition: Sample.h:127
void ReportSpin(int beg, int end, int value)
Utility function for restart: mark spins which have been calculated.
Definition: Sample.h:449
void Init(const size_t live)
Initialize data store number of non-zero spins only (MPI slaves)
Definition: Sample.h:201
Status
Definition: Declarations.h:148
double m_pos_rand_perc
Definition: Sample.h:517
a container of spins
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:165
size_t GetNProps() const
Definition: Sample.h:352
size_t & NProps()
Reference to innermost data dimensions.
Definition: Sample.h:109
void SetPositionRandomness(double val)
Set the position randomness in per cent of the cartesian resolution.
Definition: Sample.h:403
void SetTimeInterval(double val)
Definition: Sample.h:483
The Sample is the object to simulate. It contains the spins.
Definition: Sample.h:282
~Ensemble()
Destruct.
Definition: Sample.h:69

-- last change 17.06.2016 | Tony Stoecker | Imprint --