JEMRIS 2.9.2
open-source MRI simulations
Loading...
Searching...
No Matches
Attribute.h
Go to the documentation of this file.
1
8/*
9 * JEMRIS Copyright (C)
10 * 2006-2025 Tony Stoecker
11 * 2007-2018 Kaveh Vahedipour
12 * 2009-2019 Daniel Pflugfelder
13 *
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29
30
31#ifndef ATTRIBUTE_H_
32#define ATTRIBUTE_H_
33
34#include "Declarations.h"
35
36#include <stdexcept>
37#include <map>
38#include <vector>
39#include <typeinfo>
40#include <cmath>
41
42#include <iostream>
43#include <iomanip>
44
45#include "StrX.h"
46#include <ginac/ginac.h>
47#include <xercesc/dom/DOM.hpp>
48
49using namespace std;
50
51XERCES_CPP_NAMESPACE_USE
52
53//declaration of the Prototype
54class Prototype;
55
56typedef double (*FUNCP_4P) (double, double, double, double);
57
62class Attribute {
63
64 public:
65
70 Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const double& val) { Initialize(name,proto,pub,obs,val); };
71 Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const int& val) { Initialize(name,proto,pub,obs,val); };
72 Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const long& val) { Initialize(name,proto,pub,obs,val); };
73 Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const unsigned& val) { Initialize(name,proto,pub,obs,val); };
74 Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const bool& val) { Initialize(name,proto,pub,obs,val); };
75 Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const std::string& val) { Initialize(name,proto,pub,obs,val); };
76 Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const PulseAxis& val) { Initialize(name,proto,pub,obs,val); };
77
82 Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs) {
83 m_public = true;
84 m_observable = false;
85 m_dynamic = false;
86 m_name = name;
87 m_prototype = proto;
88 m_symbol_name = "NA";
89 m_formula = "NA";
90 m_ginac_excomp = false;
91 m_num_fp = -1;
92 m_cur_fp = -1;
93 m_nlgfp = NULL;
94 m_diff = -1;
95 m_sym_diff = "NA";
96 m_complex = false;
97 m_imaginary = 0.0;
98 m_address = NULL;
99 m_datatype = "";
100 m_backup = NULL;
101
102 }
103
108 virtual ~Attribute ();
109
115 bool IsPublic() const { return m_public; }
116
122 void SetPublic(bool val) { m_public = val; }
123
129 bool IsObservable() const { return m_observable; }
130
136 void SetObservable(bool val) { m_observable = val; }
137
144 bool IsDynamic() const { return m_dynamic; }
145
151 void SetDynamic(bool val) { m_dynamic = val; }
152
158 int GetDiff() { return m_diff; }
159
165 double GetImaginary() { return m_imaginary; }
166
172 bool IsComplex() const { return m_complex; }
173
180 void SetDiff(int val=0, std::string sym="diff") { m_diff = val; m_sym_diff = sym; }
181
187 std::string GetName() const { return m_name; }
188
194 std::string GetTypeID() const { return m_datatype; }
195
201 void* GetAddress() const { return m_address; }
202
208 Prototype* GetPrototype() const { return m_prototype; }
209
215 std::string GetSymbol() { return m_symbol_name; };
216
222 std::string GetFormula() { return m_formula; };
223
237 bool SetMember (std::string expr, const std::vector<Attribute*>& obs_attribs, const std::vector<std::string>& obs_attrib_keyword, bool verbose = false);
238
246 void EvalExpression ();
247
251 std::vector<Attribute*> GetObservers (){ return m_observers; };
252
256 std::vector<Attribute*> GetSubjects (){ return m_subjects; };
257
276 double EvalCompiledExpression (double const val, std::string const attrib );
277
289 double EvalCompiledNLGExpression (double const x, double const y, double const z, double const g );
290
295
300
305
310
315
316
317 /* pure template Functions need header implementation! */
318
324 template <typename T> bool Notify (const T& val) {
325
326 if ( !NewState(val) ) return false;
327
328 //initiate re-evaluation and preparation of the observers
329 for (unsigned int i=0; i<m_observers.size(); i++) {
330
331 //increase the counter for the observer's function pointer
332 if (m_observers.at(i)->GetNumberFunctionPointers()>0) {
333 m_observers.at(i)->StepCurrentFunctionPointer( );
334 }
335 //cout << "DEBUG " << GetName() << " notified " << m_observers.at(i)->GetName() << endl;
336 m_observers.at(i)->EvalExpression();
337 m_observers.at(i)->UpdatePrototype();
338 }
339
340 return true;
341 }
342
349 template <typename T> bool NewState(const T& val) {
350 if ( *((T*) m_backup) == val ) return false;
351 *((T*) m_backup) = val;
352 return true;
353 };
354
360 void AttachObserver (Attribute* attrib);
361
367 void AttachSubject (Attribute* attrib);
368
369
370 private:
371
377 void UpdatePrototype ();
378
379
380 /* pure template Functions need header implementation */
381
387 template <typename T> void WriteMember (const T& val) {
388 *((T*) m_address) = val;
389 //Notify(val);
390 }
391
397 template <typename T> T GetMember (){
398 T d = *((T*) m_address);
399 if (std::isnan(d)) return 0;
400 return d;
401 }
402
412 template <typename T> void Initialize(const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const T& val){
413 m_public = pub;
414 m_observable = obs;
415 m_dynamic = false;
416 m_name = name;
417 m_prototype = proto;
418 m_symbol_name = "x";
419 m_formula = "";
420 m_ginac_excomp = true;
421 m_compiled.push_back(false);
422 m_num_fp = 0;
423 m_cur_fp = 0;
424 m_nlgfp = NULL;
425 m_diff = 0;
426 m_sym_diff = "diff";
427 m_complex = false;
428 m_imaginary = 0.0;
429 m_address = ((void*) &val);
430 m_datatype = typeid(T*).name();
431 T* p = new T(val);
432 m_backup = (void*) p;
433 }
434
435 bool m_public;
438 std::string m_name;
439 void* m_address;
440 std::string m_datatype;
441 void* m_backup;
443 std::string m_symbol_name;
444 std::string m_sym_diff;
445 std::string m_formula;
446 GiNaC::ex m_expression;
447 GiNaC::lst m_symlist;
449 unsigned int m_num_fp;
450 unsigned int m_cur_fp;
451 std::vector<bool> m_compiled;
452 std::vector<GiNaC::FUNCP_1P> m_fp;
453 std::vector<GiNaC::FUNCP_1P> m_fpi;
454 FUNCP_4P m_nlgfp;
455 int m_diff;
457 double m_imaginary;
458 std::vector<Attribute*> m_subjects;
459 std::vector<Attribute*> m_observers;
460};
461
462#endif /* ATTRIBUTE_H_ */
Implementation of JEMRIS Declarations.
Implementation of JEMRIS StrX.
Attribute class. Attributes are private member variables of a Prototype which are accessible through ...
Definition Attribute.h:62
double m_imaginary
The imaginary part of complex expression evaluation.
Definition Attribute.h:457
Prototype * m_prototype
Pointer to the Prototype object, which instantiated this attribute.
Definition Attribute.h:442
bool m_observable
Indicating whether the attribute is observable.
Definition Attribute.h:436
bool IsDynamic() const
Check, if this attribute is dynamic. This is needed for exporting static XML Files of the Sequence.
Definition Attribute.h:144
bool m_dynamic
Indicating whether the attribute dynamically changes its value in runtime.
Definition Attribute.h:437
std::vector< Attribute * > GetSubjects()
Return the subjects.
Definition Attribute.h:256
bool m_complex
If symbolic expressions are complex, the imaginary part is considered.
Definition Attribute.h:456
std::vector< Attribute * > GetObservers()
Return the observers.
Definition Attribute.h:251
Prototype * GetPrototype() const
Get the pointer to the Prototype to which this attribute belongs.
Definition Attribute.h:208
void SetDiff(int val=0, std::string sym="diff")
Set the number of symbolic differentiations of the attribute's expression.
Definition Attribute.h:180
unsigned int m_num_fp
Number of GiNaC expression function pointers owned by this attribute.
Definition Attribute.h:449
void SetObservable(bool val)
Set this attribute to be observable by other attributes.
Definition Attribute.h:136
GiNaC::lst m_symlist
GiNaC list of all symbols involved in the calculation.
Definition Attribute.h:447
virtual ~Attribute()
Destructor deletes local copies of Prototype member variables.
Definition Attribute.cpp:37
bool m_public
Indicating whether the attribute is accessible through XML.
Definition Attribute.h:435
bool NewState(const T &val)
Check the state of the value represented by this attribute.
Definition Attribute.h:349
int GetNumberFunctionPointers()
Return the total number of function pointers.
Definition Attribute.h:299
bool Notify(const T &val)
Notify all observers.
Definition Attribute.h:324
void StepCurrentFunctionPointer()
Increase the counter to the current function pointer by one.
Definition Attribute.h:314
int m_diff
Number of symbolic differentiations of the attribute's expression.
Definition Attribute.h:455
GiNaC::ex m_expression
GiNaC Mathematical expression of the attribute.
Definition Attribute.h:446
bool IsObservable() const
Check, if this attribute is observable by other attributes.
Definition Attribute.h:129
void WriteMember(const T &val)
write the member variable of this attribute
Definition Attribute.h:387
int GetDiff()
Get the number of symbolic differentiations of the attribute's expression.
Definition Attribute.h:158
void UpdatePrototype()
Update a Prototype which holds an observing Attribute.
Definition Attribute.cpp:71
std::string m_sym_diff
GiNaC symbol name for symbolic derivative.
Definition Attribute.h:444
void EvalExpression()
Evaluate the GiNaC expression of this attribute.
Definition Attribute.cpp:156
std::string m_formula
Mathematical formula of the attribute (in XML) for GiNaC evaluation.
Definition Attribute.h:445
void SetPublic(bool val)
Set this attribute to be public for manipulation through XML.
Definition Attribute.h:122
void * m_address
Pointer to the Prototype member variable, which is represented by this attribute.
Definition Attribute.h:439
std::string GetFormula()
Get the GiNaC formula of this attribute.
Definition Attribute.h:222
void ResetCurrentFunctionPointer()
Set the counter to the current function pointer to zero.
Definition Attribute.h:309
bool HasGinacExCompiler()
True, if GiNaC external compiler is available on this system.
Definition Attribute.h:294
std::vector< bool > m_compiled
True, if GiNaC expression is compiled successfully in run time.
Definition Attribute.h:451
T GetMember()
get the member variable of this attribute
Definition Attribute.h:397
bool IsPublic() const
Check, if this attribute is public for manipulation through XML.
Definition Attribute.h:115
std::string GetSymbol()
Get the GiNaC symbol of this attribute.
Definition Attribute.h:215
std::string m_name
Name of the attribute (in XML).
Definition Attribute.h:438
void SetDynamic(bool val)
Set this attribute to be dynamic.
Definition Attribute.h:151
void Initialize(const std::string &name, Prototype *proto, const bool &pub, const bool &obs, const T &val)
Initialize private members.
Definition Attribute.h:412
std::vector< Attribute * > m_subjects
Vector of attributes under observation by this attribute.
Definition Attribute.h:458
Attribute(const std::string &name, Prototype *proto, const bool &pub, const bool &obs, const double &val)
Constructors: must provide name (XML), Prototype, public/observable status, and the member variable.
Definition Attribute.h:70
double EvalCompiledExpression(double const val, std::string const attrib)
Evaluate the compiled GiNaC expression of this attribute.
Definition Attribute.cpp:207
void AttachSubject(Attribute *attrib)
Append a new subject observed by this this attribute.
Definition Attribute.cpp:59
std::string m_datatype
Type of the Prototype member variable, which is represented by this attribute.
Definition Attribute.h:440
bool IsComplex() const
Check, if this attribute's evaluation is complex.
Definition Attribute.h:172
void * GetAddress() const
Get the pointer to the value represented by this attribute.
Definition Attribute.h:201
bool SetMember(std::string expr, const std::vector< Attribute * > &obs_attribs, const std::vector< std::string > &obs_attrib_keyword, bool verbose=false)
Set the Prototype's private member represented by this attribute. The function performs the following...
Definition Attribute.cpp:86
void * m_backup
Backup value of the Prototype member variable, which is represented by this attribute.
Definition Attribute.h:441
std::vector< GiNaC::FUNCP_1P > m_fpi
Function pointers to GiNaC expression evaluation of imaginary part.
Definition Attribute.h:453
double GetImaginary()
Get the imaginary part the attribute's evaluation.
Definition Attribute.h:165
std::vector< GiNaC::FUNCP_1P > m_fp
Function pointers to GiNaC expression evaluation.
Definition Attribute.h:452
Attribute(const std::string &name, Prototype *proto, const bool &pub, const bool &obs)
Constructor for an unobservable Attribute which does not represent a member variable.
Definition Attribute.h:82
std::string m_symbol_name
GiNaC symbol name of the attribute.
Definition Attribute.h:443
int GetCurrentFunctionPointer()
Return the counter to the current function pointer.
Definition Attribute.h:304
bool m_ginac_excomp
True, if GiNaC external compiler is available on this system.
Definition Attribute.h:448
unsigned int m_cur_fp
Current GiNaC expression function pointer.
Definition Attribute.h:450
void AttachObserver(Attribute *attrib)
Append a new observer of this attribute.
Definition Attribute.cpp:48
FUNCP_4P m_nlgfp
Function pointer to GiNaC expression evaluation of nonlinear gradients.
Definition Attribute.h:454
double EvalCompiledNLGExpression(double const x, double const y, double const z, double const g)
Evaluate the compiled GiNaC expression of the NLG attribute (nonlinear gradients)
Definition Attribute.cpp:273
std::vector< Attribute * > m_observers
Vector of attributes observing this attribute
Definition Attribute.h:459
std::string GetName() const
Get the name of the attribute.
Definition Attribute.h:187
std::string GetTypeID() const
Get the type ID of the attribute.
Definition Attribute.h:194
Prototype super class.
Definition Prototype.h:71

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