1 |
/* |
2 |
************************************************************ |
3 |
* Copyright 2006 by ACcESS MNRF * |
4 |
* * |
5 |
* http://www.access.edu.au * |
6 |
* Primary Business: Queensland, Australia * |
7 |
* Licensed under the Open Software License version 3.0 * |
8 |
* http://www.opensource.org/licenses/osl-3.0.php * |
9 |
* * |
10 |
************************************************************ |
11 |
*/ |
12 |
|
13 |
#include "DataVariable.h" |
14 |
|
15 |
using namespace std; |
16 |
using namespace escript; |
17 |
|
18 |
namespace escript { |
19 |
|
20 |
DataVariable::DataVariable() : |
21 |
op(nullop), |
22 |
leftArg(0), |
23 |
rightArg(0), |
24 |
opBuffer(0) |
25 |
{ |
26 |
// cout << "DataVariable default constructor." << endl; |
27 |
} |
28 |
|
29 |
DataVariable::DataVariable(Data* data) : |
30 |
op(idop), |
31 |
leftArg(data), |
32 |
rightArg(0), |
33 |
opBuffer(0) |
34 |
{ |
35 |
|
36 |
int numSamples = leftArg->getNumSamples(); |
37 |
int numValsPerSample = leftArg->getNumDataPointsPerSample() * leftArg->getDataPointSize(); |
38 |
int bufSize = numSamples * numValsPerSample; |
39 |
|
40 |
// cout << "DataVariable constructor. " << bufSize << endl; |
41 |
opBuffer = new double[bufSize]; |
42 |
for (int i=0; i<bufSize; i++) { |
43 |
opBuffer[i] = 0.0; |
44 |
} |
45 |
} |
46 |
|
47 |
DataVariable::~DataVariable() |
48 |
{ |
49 |
// cout << "DataVariable destructor"; |
50 |
|
51 |
if (opBuffer!=0) { |
52 |
// cout << ": freeing buffer"; |
53 |
delete[] opBuffer; |
54 |
} |
55 |
|
56 |
// cout << "." << endl; |
57 |
} |
58 |
|
59 |
Data |
60 |
DataVariable::evaluate() |
61 |
{ |
62 |
// cout << "DataVariable evaluate." << endl; |
63 |
|
64 |
if (op==idop) { |
65 |
|
66 |
return *leftArg; |
67 |
|
68 |
} else { |
69 |
|
70 |
Data expTemp(0,leftArg->getDataPointShape(),leftArg->getFunctionSpace(),true); |
71 |
|
72 |
int numSamples = leftArg->getNumSamples(); |
73 |
int numValsPerSample = leftArg->getNumDataPointsPerSample() * leftArg->getDataPointSize(); |
74 |
|
75 |
for (int sampleNo=0; sampleNo<numSamples; sampleNo++) { |
76 |
|
77 |
double* sampleBuf = evaluate_samp(sampleNo); |
78 |
double* sampleLoc = expTemp.getSampleData(sampleNo); |
79 |
|
80 |
for (int i=0; i<numValsPerSample; i++) { |
81 |
sampleLoc[i] = sampleBuf[i]; |
82 |
} |
83 |
|
84 |
} |
85 |
|
86 |
return expTemp; |
87 |
|
88 |
} |
89 |
|
90 |
throw DataException("DataVariable: nothing to evaluate"); |
91 |
return *leftArg; |
92 |
|
93 |
} |
94 |
|
95 |
double* |
96 |
DataVariable::evaluate_samp(int sampleNo) |
97 |
{ |
98 |
|
99 |
// cout << "DataVariable(" << sampleNo << ") evaluate." << endl; |
100 |
|
101 |
if (op==idop) { |
102 |
|
103 |
return leftArg->getSampleData(sampleNo); |
104 |
|
105 |
} else { |
106 |
|
107 |
int numValsPerSample = leftArg->getNumDataPointsPerSample() * leftArg->getDataPointSize(); |
108 |
int offset = sampleNo * numValsPerSample; |
109 |
|
110 |
double* leftBuff = leftArg->getSampleData(sampleNo); |
111 |
double* rightBuff = rightArg->evaluate_samp(sampleNo); |
112 |
|
113 |
if (op == sumop) { |
114 |
for (int i=0; i<numValsPerSample; i++) { |
115 |
opBuffer[offset+i] = leftBuff[i] + rightBuff[i]; |
116 |
} |
117 |
} |
118 |
|
119 |
if (op == diffop) { |
120 |
for (int i=0; i<numValsPerSample; i++) { |
121 |
opBuffer[offset+i] = leftBuff[i] - rightBuff[i]; |
122 |
} |
123 |
} |
124 |
|
125 |
return opBuffer+offset; |
126 |
|
127 |
} |
128 |
|
129 |
throw DataException("DataVariable: nothing to evaluate(i)"); |
130 |
return 0; |
131 |
|
132 |
} |
133 |
|
134 |
void |
135 |
DataVariable::sum(DataVariable* right) |
136 |
{ |
137 |
// cout << "DataVariable sum." << endl; |
138 |
if (leftArg != 0) { |
139 |
if (leftArg->getNumDataPointsPerSample() == right->leftArg->getNumDataPointsPerSample() && |
140 |
leftArg->getDataPointSize() == right->leftArg->getDataPointSize() && |
141 |
leftArg->getNumSamples() == right->leftArg->getNumSamples()) { |
142 |
op=sumop; |
143 |
rightArg=right; |
144 |
} else { |
145 |
throw DataException("DataVariable: incompatible operands"); |
146 |
} |
147 |
} else { |
148 |
throw DataException("DataVariable: cannot apply op to empty object."); |
149 |
} |
150 |
} |
151 |
|
152 |
void |
153 |
DataVariable::diff(DataVariable* right) |
154 |
{ |
155 |
cout << "DataVariable diff." << endl; |
156 |
if (leftArg != 0) { |
157 |
if (leftArg->getNumDataPointsPerSample() == right->leftArg->getNumDataPointsPerSample() && |
158 |
leftArg->getDataPointSize() == right->leftArg->getDataPointSize() && |
159 |
leftArg->getNumSamples() == right->leftArg->getNumSamples()) { |
160 |
op=diffop; |
161 |
rightArg=right; |
162 |
} else { |
163 |
throw DataException("DataVariable: incompatible operands"); |
164 |
} |
165 |
} else { |
166 |
throw DataException("DataVariable: cannot apply op to empty object."); |
167 |
} |
168 |
} |
169 |
|
170 |
} // end of namespace |