Browse Source

Fixed positioning bug in octree. Looks good now.

master
Trevor Irons 7 years ago
parent
commit
25f37979be
3 changed files with 64 additions and 16 deletions
  1. 1
    1
      examples/KernelV0.cpp
  2. 8
    0
      include/KernelV0.h
  3. 55
    15
      src/KernelV0.cpp

+ 1
- 1
examples/KernelV0.cpp View File

30
 		earth->SetLayerThickness( (VectorXr(1) << 10).finished() );
30
 		earth->SetLayerThickness( (VectorXr(1) << 10).finished() );
31
 
31
 
32
     // Transmitter loops
32
     // Transmitter loops
33
-    auto Tx1 = CircularLoop(60, 15, 50, 50);
33
+    auto Tx1 = CircularLoop(60, 15, 100, 100);
34
     auto Tx2 = CircularLoop(60, 15, 55, 50);
34
     auto Tx2 = CircularLoop(60, 15, 55, 50);
35
     //auto Tx1 = CircularLoop(60, 15, 0, 0); // was 60
35
     //auto Tx1 = CircularLoop(60, 15, 0, 0); // was 60
36
 
36
 

+ 8
- 0
include/KernelV0.h View File

29
 #include "vtkHyperOctree.h"
29
 #include "vtkHyperOctree.h"
30
 #include "vtkHyperOctreeCursor.h"
30
 #include "vtkHyperOctreeCursor.h"
31
 #include "vtkXMLHyperOctreeWriter.h"
31
 #include "vtkXMLHyperOctreeWriter.h"
32
+#include "vtkDoubleArray.h"
32
 #endif
33
 #endif
33
 
34
 
34
 namespace Lemma {
35
 namespace Lemma {
185
          */
186
          */
186
         bool EvaluateKids2(  const Vector3r& size, const int& level, const Vector3r& cpos,
187
         bool EvaluateKids2(  const Vector3r& size, const int& level, const Vector3r& cpos,
187
                             const Complex& parentVal, vtkHyperOctree* octree, vtkHyperOctreeCursor* curse );
188
                             const Complex& parentVal, vtkHyperOctree* octree, vtkHyperOctreeCursor* curse );
189
+
190
+        void GetPosition( vtkHyperOctreeCursor* Cursor, Real* p );
188
         #endif
191
         #endif
189
 
192
 
190
         // ====================  DATA MEMBERS  =========================
193
         // ====================  DATA MEMBERS  =========================
191
 
194
 
192
         Complex                                   SUM;
195
         Complex                                   SUM;
193
 
196
 
197
+        Real                                      VOLSUM;
198
+
194
         Real                                      tol=1e-3;
199
         Real                                      tol=1e-3;
195
 
200
 
196
         int                                       nleaves;
201
         int                                       nleaves;
197
 
202
 
203
+        Vector3r   Size;
204
+        Vector3r   Origin;
205
+
198
         std::shared_ptr< LayeredEarthEM >         SigmaModel = nullptr;
206
         std::shared_ptr< LayeredEarthEM >         SigmaModel = nullptr;
199
 
207
 
200
         std::map< std::string , std::shared_ptr< PolygonalWireAntenna > >  TxRx;
208
         std::map< std::string , std::shared_ptr< PolygonalWireAntenna > >  TxRx;

+ 55
- 15
src/KernelV0.cpp View File

118
                 // TODO query for method, altough with flat antennae, this is fastest
118
                 // TODO query for method, altough with flat antennae, this is fastest
119
                 EMEarths.back()->SetHankelTransformMethod(ANDERSON801);
119
                 EMEarths.back()->SetHankelTransformMethod(ANDERSON801);
120
         }
120
         }
121
-        IntegrateOnOctreeGrid( 1e-7, vtkOutput );
121
+        IntegrateOnOctreeGrid( 1e-1, vtkOutput );
122
 
122
 
123
     }
123
     }
124
 
124
 
129
     void KernelV0::IntegrateOnOctreeGrid( const Real& tolerance, bool vtkOutput) {
129
     void KernelV0::IntegrateOnOctreeGrid( const Real& tolerance, bool vtkOutput) {
130
 
130
 
131
         this->tol = tolerance;
131
         this->tol = tolerance;
132
-        Vector3r                Size;
133
-            Size << 100,100,100;
134
-        Vector3r                Origin;
132
+        //Vector3r                Size;
133
+            Size << 200,200,100;
134
+        //Vector3r                Origin;
135
             Origin << 0,0,0;
135
             Origin << 0,0,0;
136
-        Vector3r                cpos;
137
-            cpos << 50,50,50;
136
+        Vector3r                cpos;  // centre position
137
+            //cpos << 100,100,50;
138
+            cpos = (Size-Origin).array() / 2.;
138
         int                     maxlevel;
139
         int                     maxlevel;
139
 
140
 
140
         SUM = 0;
141
         SUM = 0;
142
+        VOLSUM = 0;
141
         nleaves = 0;
143
         nleaves = 0;
142
         if (!vtkOutput) {
144
         if (!vtkOutput) {
143
             EvaluateKids( Size, 0, cpos, 1e6 );
145
             EvaluateKids( Size, 0, cpos, 1e6 );
150
             vtkHyperOctreeCursor* curse = oct->NewCellCursor();
152
             vtkHyperOctreeCursor* curse = oct->NewCellCursor();
151
                 curse->ToRoot();
153
                 curse->ToRoot();
152
             EvaluateKids2( Size, 0, cpos, 1e6, oct, curse );
154
             EvaluateKids2( Size, 0, cpos, 1e6, oct, curse );
155
+
156
+            // Fill in leaf data
157
+            vtkDoubleArray* kr = vtkDoubleArray::New();
158
+                kr->SetNumberOfComponents(1);
159
+                kr->SetName("Re($K_0$)");
160
+                kr->SetNumberOfTuples( oct->GetNumberOfLeaves() );
161
+            vtkDoubleArray* ki = vtkDoubleArray::New();
162
+                ki->SetNumberOfComponents(1);
163
+                ki->SetName("Im($K_0$)");
164
+                ki->SetNumberOfTuples( oct->GetNumberOfLeaves() );
165
+            for (auto leaf : LeafDict) {
166
+                kr->InsertTuple1( leaf.first, std::real(leaf.second) );
167
+                ki->InsertTuple1( leaf.first, std::imag(leaf.second) );
168
+            }
169
+            oct->GetLeafData()->AddArray(kr);
170
+            oct->GetLeafData()->AddArray(ki);
171
+
153
             auto write = vtkXMLHyperOctreeWriter::New();
172
             auto write = vtkXMLHyperOctreeWriter::New();
154
                 //write.SetDataModeToAscii()
173
                 //write.SetDataModeToAscii()
155
                 write->SetInputData(oct);
174
                 write->SetInputData(oct);
156
                 write->SetFileName("octree.vto");
175
                 write->SetFileName("octree.vto");
157
                 write->Write();
176
                 write->Write();
158
                 write->Delete();
177
                 write->Delete();
178
+
179
+            kr->Delete();
180
+            ki->Delete();
159
             curse->Delete();
181
             curse->Delete();
160
             oct->Delete();
182
             oct->Delete();
161
         #else
183
         #else
163
         #endif
185
         #endif
164
 
186
 
165
         }
187
         }
166
-        std::cout << "SUM\t" << SUM << "\t" << 100*100*100 << "\t" << SUM - Complex(100.*100.*100.) <<  std::endl;
188
+        std::cout << "\nVOLSUM=" << VOLSUM << "\tActual=" <<  Size(0)*Size(1)*Size(2) << "\tDifference=" << VOLSUM - (Size(0)*Size(1)*Size(2)) <<  std::endl;
167
         std::cout << "nleaves\t" << nleaves << std::endl;
189
         std::cout << "nleaves\t" << nleaves << std::endl;
190
+        std::cout << "KSUM\t" << SUM << std::endl;
168
 
191
 
169
     }
192
     }
170
 
193
 
174
     //--------------------------------------------------------------------------------------
197
     //--------------------------------------------------------------------------------------
175
     Complex KernelV0::f( const Vector3r& r, const Real& volume, const Vector3cr& Bt ) {
198
     Complex KernelV0::f( const Vector3r& r, const Real& volume, const Vector3cr& Bt ) {
176
         //std::cout << volume*Bt.norm() << std::endl;
199
         //std::cout << volume*Bt.norm() << std::endl;
200
+        //return Complex(volume*Bt.norm());
177
         return Complex(volume*Bt.norm());
201
         return Complex(volume*Bt.norm());
178
         //return Complex(volume);
202
         //return Complex(volume);
179
     }
203
     }
187
 
211
 
188
         // Next level step, interested in one level below
212
         // Next level step, interested in one level below
189
         // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
213
         // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
190
-        Vector3r step = size.array() / (Real)(1 << (level+2) );
214
+        Vector3r step  = size.array() / (Real)(1 << (level+1) );
215
+        Vector3r step2 = size.array() / (Real)(1 << (level+2) );
191
 
216
 
192
-        Real vol = step(0)*step(1)*step(2);     // volume of each child
217
+        Real vol = (step2(0)*step2(1)*step2(2));     // volume of each child
193
 
218
 
194
         Vector3r pos =  cpos - step/2.;
219
         Vector3r pos =  cpos - step/2.;
195
         Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
220
         Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
227
 
252
 
228
         Complex ksum = kvals.sum();     // Kernel sum
253
         Complex ksum = kvals.sum();     // Kernel sum
229
         // Evaluate whether or not furthur splitting is needed
254
         // Evaluate whether or not furthur splitting is needed
230
-        if ( std::abs(ksum - parentVal) > tol || level < 5 ) {
255
+        if ( std::abs(ksum - parentVal) > tol || level < 2 ) {
231
             for (int ichild=0; ichild<8; ++ichild) {
256
             for (int ichild=0; ichild<8; ++ichild) {
232
                 Vector3r cp = pos; // Eigen complains about combining these
257
                 Vector3r cp = pos; // Eigen complains about combining these
233
                 cp += posadd.row(ichild);
258
                 cp += posadd.row(ichild);
234
                 bool isleaf = EvaluateKids( size, level+1, cp, kvals(ichild) );
259
                 bool isleaf = EvaluateKids( size, level+1, cp, kvals(ichild) );
235
                 if (isleaf) {      // Include result in final integral
260
                 if (isleaf) {      // Include result in final integral
236
                     SUM += ksum;
261
                     SUM += ksum;
262
+                    VOLSUM += 8.*vol;
237
                     nleaves += 1;
263
                     nleaves += 1;
238
                 }
264
                 }
239
             }
265
             }
251
     bool KernelV0::EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
277
     bool KernelV0::EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
252
         const Complex& parentVal, vtkHyperOctree* oct, vtkHyperOctreeCursor* curse) {
278
         const Complex& parentVal, vtkHyperOctree* oct, vtkHyperOctreeCursor* curse) {
253
 
279
 
254
-        std::cout << "\rlevel " << level << "\t" << nleaves;
255
-        std::cout.flush();
280
+        //std::cout << "\rlevel " << level << "\t" << nleaves;
281
+        //std::cout.flush();
256
 
282
 
257
         // Next level step, interested in one level below
283
         // Next level step, interested in one level below
258
         // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
284
         // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
259
-        Vector3r step = size.array() / (Real)(1 << (level+2) );
285
+        Vector3r step  = size.array() / (Real)(1 << (level+1) );
286
+        Vector3r step2 = size.array() / (Real)(1 << (level+2) );
260
 
287
 
261
-        Real vol = step(0)*step(1)*step(2);     // volume of each child
288
+        Real vol = (step2(0)*step2(1)*step2(2));     // volume of each child
262
 
289
 
263
         Vector3r pos =  cpos - step/2.;
290
         Vector3r pos =  cpos - step/2.;
264
         Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
291
         Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
295
 
322
 
296
         Complex ksum = kvals.sum();     // Kernel sum
323
         Complex ksum = kvals.sum();     // Kernel sum
297
         // Evaluate whether or not furthur splitting is needed
324
         // Evaluate whether or not furthur splitting is needed
298
-        if ( std::abs(ksum - parentVal) > tol || level < 3 ) {
325
+        if ( std::abs(ksum - parentVal) > tol || level < 2 ) {
299
             oct->SubdivideLeaf(curse);
326
             oct->SubdivideLeaf(curse);
300
             for (int ichild=0; ichild<8; ++ichild) {
327
             for (int ichild=0; ichild<8; ++ichild) {
301
                 curse->ToChild(ichild);
328
                 curse->ToChild(ichild);
302
                 Vector3r cp = pos; // Eigen complains about combining these
329
                 Vector3r cp = pos; // Eigen complains about combining these
303
                 cp += posadd.row(ichild);
330
                 cp += posadd.row(ichild);
331
+                //Real p[3];
332
+                //GetPosition(curse, p);
333
+                //std::cout << cp[0] << "\t" << p[0] << "\t" << cp[1] << "\t" << p[1] << "\t" << cp[2] << "\t" << p[2] << "\t" <<  vol<< std::endl;
304
                 bool isleaf = EvaluateKids2( size, level+1, cp, kvals(ichild), oct, curse );
334
                 bool isleaf = EvaluateKids2( size, level+1, cp, kvals(ichild), oct, curse );
305
                 if (isleaf) {  // Include result in final integral
335
                 if (isleaf) {  // Include result in final integral
306
                     LeafDict[curse->GetLeafId()] = kvals(ichild);       // VTK
336
                     LeafDict[curse->GetLeafId()] = kvals(ichild);       // VTK
307
                     SUM += ksum;
337
                     SUM += ksum;
338
+                    VOLSUM += 8*vol;
308
                     nleaves += 1;
339
                     nleaves += 1;
309
                 }
340
                 }
310
                 curse->ToParent();
341
                 curse->ToParent();
313
         }
344
         }
314
         return true;       // leaf
345
         return true;       // leaf
315
     }
346
     }
347
+
348
+    void KernelV0::GetPosition( vtkHyperOctreeCursor* Cursor, Real* p ) {
349
+        Real ratio=1.0/(1<<(Cursor->GetCurrentLevel()));
350
+        //step  = ((Size).array() / std::pow(2.,Cursor->GetCurrentLevel()));
351
+        p[0]=(Cursor->GetIndex(0)+.5)*ratio*this->Size[0]+this->Origin[0] ;//+ .5*step[0];
352
+        p[1]=(Cursor->GetIndex(1)+.5)*ratio*this->Size[1]+this->Origin[1] ;//+ .5*step[1];
353
+        p[2]=(Cursor->GetIndex(2)+.5)*ratio*this->Size[2]+this->Origin[2] ;//+ .5*step[2];
354
+    }
355
+
316
     #endif
356
     #endif
317
 
357
 
318
 } // ----  end of namespace Lemma  ----
358
 } // ----  end of namespace Lemma  ----

Loading…
Cancel
Save