Browse Source

Cleaning up code, gravity seems to be working.

iss2
Trevor Irons 8 years ago
parent
commit
292884a867

+ 4
- 3
examples/VTKGsphere.cpp View File

@@ -113,15 +113,16 @@ int main(int argc, char**argv) {
113 113
                 phi->InsertTuple1( in, (1./3.) * (R*R*R) * (costheta / (raddist*raddist))  );
114 114
             }
115 115
         } else if (std::string(argv[3]) == "gravity") {
116
-            double mass = 4./3. * PI * (R*R*R); // volume * density (1)
116
+            double mass = (4.*PI/3.) * (R*R*R); // volume * density (1)
117
+            double G = 1/(4.*PI);
117 118
             if (raddist < R) {
118
-                phi->InsertTuple1( in, mass * (( 3*(R*R) - raddist*raddist )/(2*(R*R*R))) ); // (1./3.)*point[2] );
119
+                phi->InsertTuple1( in, mass * G * (( 3*(R*R) - raddist*raddist )/(2*(R*R*R))) ); // (1./3.)*point[2] );
119 120
                 //phi->InsertTuple1( in,  (2*PI/3)*(3*R*R-raddist*raddist)  ); // (1./3.)*point[2] );
120 121
             } else {
121 122
                 //phi->InsertTuple1( in, 0);
122 123
                 //double costheta = point[2]/raddist ;
123 124
                 //phi->InsertTuple1( in, -(1./3.)*(R*R*R) * ( costheta / (rho*rho) )  );
124
-                phi->InsertTuple1( in, mass/raddist );
125
+                phi->InsertTuple1( in, G * mass/raddist );
125 126
             }
126 127
         }
127 128
     }

+ 4
- 4
examples/borehole/sphere.geo View File

@@ -7,11 +7,11 @@
7 7
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 8
  */
9 9
 
10
-radius = 4.25;     // Radius of the damn thing
11
-lc = radius/5;     //  0.25;   // Target element size
10
+radius = 2.25;     // Radius of the damn thing
11
+lc = radius/6;     //  0.25;   // Target element size
12 12
 
13 13
 // Total Solution Space
14
-Box = 5*radius; // The down side of potential
14
+Box = 3*radius; // The down side of potential
15 15
 X0 = -Box;
16 16
 X1 =  Box;
17 17
 Y0 = -Box;
@@ -19,7 +19,7 @@ Y1 =  Box;
19 19
 Z0 = -Box;
20 20
 Z1 =  Box;
21 21
 
22
-cellSize=radius/10; ///10;
22
+cellSize=radius/6; ///10;
23 23
 dd = 0 ; //  1e-5; //cellSize; // .01;
24 24
 pio2=Pi/2;
25 25
 

+ 1
- 1
examples/borehole/sphere.sh View File

@@ -2,7 +2,7 @@
2 2
 gmsh  -3 -format vtk -o sphere.vtk  sphere.geo 
3 3
 gmsh  -2 -format stl -o sphereBox.stl  sphereBox.geo 
4 4
 ../ResampleWithDataset  sphere.vtk  sphereBox.stl  MergedSphere.vtu 
5
-../VTKGsphere MergedSphere.vtu   4.25  gravity  sphereGrav.vtu
5
+../VTKGsphere MergedSphere.vtu   2.25  gravity  sphereGrav.vtu
6 6
 ../FEM4EllipticPDE_bhmag  sphereGrav.vtu  sphereGrav.vtu   sphereOutGrav.vtu 
7 7
 
8 8
 #paraview --data=sphereOutGrav.vtu 

+ 2
- 2
examples/borehole/sphereBox.geo View File

@@ -9,12 +9,12 @@
9 9
 
10 10
 D0 = 10;          // Top of magnet
11 11
 D1 = 11;          // Bottom of magnet
12
-radius = 4.25;     // Radius of the damn thing
12
+radius = 2.25;     // Radius of the damn thing
13 13
 
14 14
 lc = radius;   //  0.25;   // Target element size
15 15
 
16 16
 // Total Solution Space
17
-Box = 5*radius; // The down side of potential
17
+Box = 3*radius; // The down side of potential
18 18
 X0 = -Box;
19 19
 X1 =  Box;
20 20
 Y0 = -Box;

+ 1
- 1
include/FEM4EllipticPDE.h View File

@@ -134,7 +134,7 @@ namespace Lemma {
134 134
             void Solve(const std::string& fname);
135 135
 
136 136
             /** Performs solution */
137
-            void SolveOLD(const std::string& fname);
137
+            void SolveOLD2(const std::string& fname);
138 138
 
139 139
             // ====================  ACCESS        =======================
140 140
 

+ 35
- 89
src/FEM4EllipticPDE.cpp View File

@@ -205,30 +205,38 @@ namespace Lemma {
205 205
         //ConstructLoadVector();
206 206
 
207 207
         std::cout << "\nSolving" << std::endl;
208
-        std::cout << "   rows\tcols\n";
209
-        std::cout << "A: "  << A.rows() << "\t" << A.cols() << std::endl;
210
-        std::cout << "g: "  << g.rows() << "\t" << g.cols() << std::endl;
208
+
209
+        std::cout << std::setw(5) << "  " << std::setw(14) << "rows"     << std::setw(14) << "cols"     << std::endl;
210
+        std::cout << std::setw(5) << "  " << std::setw(14) << "--------" << std::setw(14) << "--------" << std::endl;
211
+        std::cout << std::setw(5) << "A:" << std::setw(14) << A.rows()   << std::setw(14) << A.cols()   << std::endl;
212
+        std::cout << std::setw(5) << "g:" << std::setw(14) << g.rows()   << std::setw(14) << g.cols()   << std::endl;
211 213
 
212 214
         ////////////////////////////////////////////////////////////
213 215
         // Solving:
214 216
         //Eigen::SimplicialCholesky<Eigen::SparseMatrix<Real>, Eigen::Lower > chol(A);  // performs a Cholesky factorization of A
215 217
         //VectorXr u = chol.solve(g);
216
-
217
-        //Eigen::SparseLU<Eigen::SparseMatrix<Real, Eigen::ColMajor>, Eigen::COLAMDOrdering<int> > solver;
218
-        //solver.analyzePattern(A);
219
-        //solver.factorize(A);
220
-        //VectorXr u = solver.solve(g);
221
-
222
-        //#ifdef CGSOLVE
223
-        //Eigen::ConjugateGradient<Eigen::SparseMatrix<Real, Eigen::Lower >  Eigen::DiagonalPreconditioner > cg;
224
-        //Eigen::ConjugateGradient< Eigen::SparseMatrix<Real>  > cg(A);
218
+        //#define LUSOLVE
219
+        #ifdef LUSOLVE
220
+        Eigen::SparseLU<Eigen::SparseMatrix<Real, Eigen::ColMajor>, Eigen::COLAMDOrdering<int> > solver;
221
+        std::cout << "LU analyze pattern" << std::endl;
222
+        solver.analyzePattern(A);
223
+        std::cout << "LU factorizing" << std::endl;
224
+        solver.factorize(A);
225
+        std::cout << "LU solving" << std::endl;
226
+        solver.factorize(A);
227
+        VectorXr u = solver.solve(g);
228
+        #endif
229
+
230
+        #define CGSOLVE
231
+        #ifdef CGSOLVE
232
+        // TODO try IncompleteLUT preconditioner
225 233
         Eigen::BiCGSTAB<Eigen::SparseMatrix<Real> > cg(A);
226 234
         //cg.setMaxIterations(3000);
227 235
         //cg.setTolerance(1e-28);
228 236
         VectorXr u = cg.solve(g);
229 237
         std::cout << "#iterations:     " << cg.iterations() << std::endl;
230 238
         std::cout << "estimated error: " << cg.error()      << std::endl;
231
-        //#endif
239
+        #endif
232 240
 
233 241
         vtkDoubleArray *gArray = vtkDoubleArray::New();
234 242
         vtkDoubleArray *uArray = vtkDoubleArray::New();
@@ -284,7 +292,6 @@ namespace Lemma {
284 292
             GCell = true;
285 293
         }
286 294
 
287
-
288 295
         // Here we iterate over all of the cells
289 296
         for (int ic=0; ic < vtkGrid->GetNumberOfCells(); ++ic) {
290 297
 
@@ -315,19 +322,8 @@ namespace Lemma {
315 322
                 ID[3] = Ids->GetId(3);
316 323
 
317 324
             Real sigma_bar(0);
318
-            /*
319
-            if (GCell) {
320
-                sigma_bar = vtkGrid->GetCellData()->GetScalars("G")->GetTuple1(ic);
321
-            } else {
322
-                sigma_bar  = vtkGrid->GetPointData()->GetScalars("G")->GetTuple1(ID[0]);
323
-                sigma_bar += vtkGrid->GetPointData()->GetScalars("G")->GetTuple1(ID[1]);
324
-                sigma_bar += vtkGrid->GetPointData()->GetScalars("G")->GetTuple1(ID[2]);
325
-                sigma_bar += vtkGrid->GetPointData()->GetScalars("G")->GetTuple1(ID[3]);
326
-                sigma_bar /= 4.;
327
-            }
328
-            */
329
-
330 325
             // TEST VOID IN SIGMA!! TODO DON"T KEEP THIS
326
+            /*
331 327
             Real xc = C.col(1).array().mean();
332 328
             Real yc = C.col(2).array().mean();
333 329
             Real zc = C.col(3).array().mean();
@@ -336,40 +332,26 @@ namespace Lemma {
336 332
             } else {
337 333
                 sigma_bar = 1.;
338 334
             }
335
+            */
339 336
             sigma_bar = 1.;
340 337
 
341 338
 
342 339
             for (int ii=0; ii<4; ++ii) {
343 340
                 int bbi = vtkGrid->GetPointData()->GetScalars("HomogeneousDirichlet")->GetTuple(ID[ii])[0];
344 341
                 if (bbi) {
342
+                    /* Dirichlet boundary */
345 343
                     coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[ii], 1));
346 344
                 } else {
347 345
                     for (int jj=0; jj<4; ++jj) {
348
-                        /* homogeneous Dirichlet boundary */
349
-                        //if (jj==ii && C(ii,3)==13.5) {
350
-                        //if (jj==ii && ((bb-1.)<1e-8) ) {
351
-                            // Apply Homogeneous Dirichlet Boundary conditions
352
-                        //    Real bb = vtkGrid->GetPointData()->GetScalars("HomogeneousDirichlet")->GetTuple(ID[ii])[0];
353
-                        //    Real bdry = (1./(BndryH*BndryH))*BndrySigma*bb; // + sum;
354
-                            //Real bdry = GradPhi.col(ii).tail<3>().dot(GradPhi.col(ii).tail<3>())*BndrySigma*bb; // + sum;
355
-                        //    coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], bdry + GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>() ) * V * sigma_bar ) );
356
-                            //coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], 1));
357
-                            //break;
358
-                        //} else {
359 346
                         coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj],        GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>() ) * V * sigma_bar ) );
360
-                        //}
361
-                    /* */
362
-                    /* Inhomogeneous Dirichlet */
363
-                    //coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>())*V*sigma_bar ));
364
-                    // Stiffness matrix no longer contains boundary conditions...
365
-                    //coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>() ) * V * sigma_bar ) );
347
+                    }
366 348
                 }
367 349
             }
368
-            }
369
-            //std::cout <<  "\r" << (int)(1e2*((float)(ic) / (float)(vtkGrid->GetNumberOfCells()))) << std::flush ;
350
+            std::cout <<  "\r" << (int)(1e2*((float)(ic) / (float)(vtkGrid->GetNumberOfCells()))) << std::flush ;
370 351
         }
371 352
         A.setFromTriplets(coeffs.begin(), coeffs.end());
372
-        //std::cout << "A\n" << A << std::endl;
353
+        A.finalize();
354
+        A.makeCompressed();
373 355
     }
374 356
 
375 357
     void FEM4EllipticPDE::SetupPotential() {
@@ -379,7 +361,7 @@ namespace Lemma {
379 361
         std::cout << "\nBuilding load vector (g)" << std::endl;
380 362
         g = VectorXr::Zero(vtkGrid->GetNumberOfPoints());
381 363
         std::cout << "made g with "  << vtkGrid->GetNumberOfPoints() << " points" << std::endl;
382
-        VectorXr DB = VectorXr::Zero(vtkGrid->GetNumberOfPoints());
364
+
383 365
         for (int ic=0; ic < vtkGrid->GetNumberOfCells(); ++ic) {
384 366
 
385 367
             Eigen::Matrix<Real, 4, 4> C = Eigen::Matrix<Real, 4, 4>::Zero() ;
@@ -392,8 +374,9 @@ namespace Lemma {
392 374
             }
393 375
 
394 376
             Real V = (1./6.) * C.determinant();              // volume of tetrahedra
395
-            Eigen::Matrix<Real, 4, 4> GradPhi = C.inverse(); // nabla \phi
377
+            //Eigen::Matrix<Real, 4, 4> GradPhi = C.inverse(); // nabla \phi
396 378
 
379
+            /* The indices */
397 380
             vtkIdList* Ids = vtkGrid->GetCell(ic)->GetPointIds();
398 381
             int ID[4];
399 382
                 ID[0] = Ids->GetId(0);
@@ -401,56 +384,19 @@ namespace Lemma {
401 384
                 ID[2] = Ids->GetId(2);
402 385
                 ID[3] = Ids->GetId(3);
403 386
 
404
-            /*
405
-            Real avg(0);
406
-            Real GG[4];
407
-            for (int ii=0; ii<4; ++ii) {
408
-                GG[ii] = vtkGrid->GetPointData()->GetScalars("G")->GetTuple(ID[ii])[0];
409
-                //avg /= 4.;
410
-            }
411
-            if ( std::abs( (GG[0]+GG[1]+GG[2]+GG[3])/4. - GG[0])  < 1e-5) {
412
-                avg = GG[0];
413
-            }
414
-            */
415
-
416
-            /*
417
-            VectorXr W = VectorXr::Zero(4);
387
+            /* Fill the RHS vector with Dirichlet conditions or fuction value */
418 388
             for (int ii=0; ii<4; ++ii) {
419
-                W[ii] = vtkGrid->GetPointData()->GetScalars("HomogeneousDirichlet")->GetTuple(ID[ii])[0] *
420
-                        vtkGrid->GetPointData()->GetScalars("analytic_phi")->GetTuple(ID[ii])[0];
421
-                DB[ID[ii]] = vtkGrid->GetPointData()->GetScalars("HomogeneousDirichlet")->GetTuple(ID[ii])[0] *
422
-                             vtkGrid->GetPointData()->GetScalars("analytic_phi")->GetTuple(ID[ii])[0];
423
-            }
424
-            //auto G = GradPhi.block<3,4>(1,0).transpose()*GradPhi.block<3,4>(1,0)*W;
425
-            VectorXr G = GradPhi.block<3,4>(1,0).transpose()*GradPhi.block<3,4>(1,0)*W;
426
-            Real  sigma_bar(1.);
427
-            */
428
-
429
-            for (int ii=0; ii<4; ++ii) {
430
-            //     avg += vtkGrid->GetPointData()->GetScalars()->GetTuple(ID[ii])[0];
431
-            //     //if ( std::abs(vtkGrid->GetPointData()->GetScalars()->GetTuple(ID[ii])[0]) > 1e-3 )
432
-            //}
433
-            //TODO this seems wrong!
434
-            //avg /= 4.;
435
-                // TODO test code remove
436 389
                 if (vtkGrid->GetPointData()->GetScalars("HomogeneousDirichlet")->GetTuple(ID[ii])[0]) {
437 390
                     g(ID[ii]) += vtkGrid->GetPointData()->GetScalars("analytic_phi")->GetTuple(ID[ii])[0];
438 391
                 } else {
439
-                    g(ID[ii]) += (PI*V)*(vtkGrid->GetCellData()->GetScalars("G")->GetTuple(ic)[0]); // Why 3.0??
392
+                    g(ID[ii]) += (V/4.)*(vtkGrid->GetCellData()->GetScalars("G")->GetTuple(ic)[0]); // Why 3.0??
440 393
                 }
441
-                //if (std::abs(C(ii,3)-13.5) > 1e-5) {
442
-                //    g(ID[ii]) -= G[ii]*(V)*sigma_bar;
443
-                //}
444
-                //g(ID[ii]) +=  V/4*avg;
445
-                  //g(ID[ii]) +=  6.67 *(V/4.) * avg;
446 394
             }
447
-            //g(ID[0]) +=  (V/4.) * avg;
448
-        }
449
-        //g -= A*DB;
450 395
 
396
+        }
451 397
     }
452 398
 
453
-    void FEM4EllipticPDE::SolveOLD(const std::string& fname) {
399
+    void FEM4EllipticPDE::SolveOLD2(const std::string& fname) {
454 400
 
455 401
         Real r0[3];
456 402
         Real r1[3];

Loading…
Cancel
Save