main.cpp 69.7 KB
Newer Older
Simone Rossi's avatar
Simone Rossi committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <lifev/core/LifeV.hpp>
#include <lifev/electrophysiology/solver/ElectroETAMonodomainSolver.hpp>
#include <lifev/electrophysiology/solver/IonicModels/IonicMinimalModel.hpp>

#include <lifev/structure/solver/StructuralConstitutiveLawData.hpp>

#include <lifev/structure/solver/StructuralConstitutiveLaw.hpp>
#include <lifev/structure/solver/StructuralOperator.hpp>
#include <lifev/em/solver/EMStructuralOperator.hpp>

#include <lifev/em/solver/EMNeoHookeanActivatedMaterial.hpp>
#include <lifev/em/solver/EMGeneralizedActiveHolzapfelOgdenMaterial.hpp>
#include <lifev/em/solver/EMETAFunctors.hpp>

#include <lifev/core/filter/ExporterEnsight.hpp>
#ifdef HAVE_HDF5
#include <lifev/core/filter/ExporterHDF5.hpp>
#endif
#include <lifev/core/filter/ExporterEmpty.hpp>

#include <lifev/eta/fem/ETFESpace.hpp>
#include <lifev/eta/expression/Integrate.hpp>

#include <lifev/core/interpolation/RBFhtp.hpp>
#include <lifev/core/interpolation/RBFhtpVectorial.hpp>
#include <lifev/core/mesh/MeshLoadingUtility.hpp>
#include <lifev/core/mesh/MeshTransformer.hpp>
#include <lifev/core/interpolation/RBFlocallyRescaledVectorial.hpp>
#include <lifev/core/interpolation/RBFlocallyRescaledScalar.hpp>
#include <lifev/core/interpolation/RBFrescaledVectorial.hpp>
#include <lifev/core/interpolation/RBFrescaledScalar.hpp>
//#include <lifev/core/interpolation/RBFscalar.hpp>
#include <lifev/core/interpolation/RBFvectorial.hpp>

#include <lifev/bc_interface/3D/bc/BCInterface3D.hpp>
#include <sys/stat.h>
#include <lifev/core/fem/GradientRecovery.hpp>

using namespace LifeV;

void EpetraPow ( VectorEpetra& vector, const Real p )
{
	Int size = vector.epetraVector().MyLength();

	for(int j(0); j < size; j++ )
	{
	int gid = vector.blockMap().GID(j);
	vector[gid] = std::pow(vector[gid],p);
	}
}
void EpetraSqrt ( VectorEpetra& vector )
{
	Int size = vector.epetraVector().MyLength();

	for(int j(0); j < size; j++ )
	{
	int gid = vector.blockMap().GID(j);
	vector[gid] = std::sqrt(vector[gid]);
	}
}


Real bcZero (const Real& /*t*/, const Real&  /*X*/, const Real& /*Y*/, const Real& /*Z*/, const ID& /*i*/)
{
    return  0.;
}
Real d0(const Real& /*t*/, const Real&  /*X*/, const Real& /*Y*/, const Real& /*Z*/, const ID& /*i*/)
{
    return  0.;
}

Real initialStimulus(const Real& /*t*/, const Real&  X, const Real& Y, const Real& Z, const ID& /*i*/)
{
	Real nx = 0.634709388205124;
	Real ny = -0.715471794571893;
	Real nz = 0.291965928999176;
	Real x0 = 11.7459118826775;
	Real y0 = 9.96923035282421;
	Real z0 = 14.7264615540727;
	Real x1 = 14.97121791714;
	Real y1 = 6.22138875501107;
	Real z1 = 16.2257405087125;
	if( nx * ( X - x0 ) + ny * ( Y - y0 ) + nz * ( Z - z0 ) < 0 ) return 0.0;
	else if( nx * ( X - x1 ) + ny * ( Y - y1 ) + nz * ( Z - z1 ) > 0 ) return 0.0;
	else return  1.0;
}


Real FL(const Real I4f)
{
    	if(I4f > 0.87277 && I4f < 1.334)
    	{
			Real d0 = -4.333618335582119e3;
			Real d1 = 2.570395355352195e3;
			Real e1 = -2.051827278991976e3;
			Real d2 = 1.329536116891330e3;
			Real e2 = 0.302216784558222e3;
			Real d3 = 0.104943770305116e3;
			Real e3 = 0.218375174229422e3;
			Real l0 = 1.95;

			Real Force = d0/2 + d1 * std::sin(I4f * l0)
							  + e1 * std::cos(I4f * l0)
							  + d2 * std::sin(2 * I4f * l0)
							  + e2 * std::cos(2 * I4f * l0)
							  + d3 * std::sin(3 * I4f * l0)
							  + e3 * std::cos(3 * I4f * l0);
			return Force;
    	}
    	else
    		return 0.0;
}


Real FLG(const Real g)
{

    	if(g>-0.0657788 && g<0.154989)
    	{
			Real d0 = -4.333618335582119e3;
			Real d1 = 2.570395355352195e3;
			Real e1 = -2.051827278991976e3;
			Real d2 = 1.329536116891330e3;
			Real e2 = 0.302216784558222e3;
			Real d3 = 0.104943770305116e3;
			Real e3 = 0.218375174229422e3;
			Real l0 = 1.95;

			Real Force = d0/2 + d1 * std::sin( ( 1.0 + g ) * ( 1.0 + g ) * l0)
							  + e1 * std::cos( ( 1.0 + g ) * ( 1.0 + g ) * l0)
							  + d2 * std::sin( 2 * ( 1.0 + g ) * ( 1.0 + g ) * l0)
							  + e2 * std::cos( 2 * ( 1.0 + g ) * ( 1.0 + g ) * l0)
							  + d3 * std::sin( 3 * ( 1.0 + g ) * ( 1.0 + g ) * l0)
							  + e3 * std::cos( 3 * ( 1.0 + g ) * ( 1.0 + g ) * l0);
			return Force;
    	}
    	else
    		return 0.0;
}

Real initialV0left(const Real& /*t*/, const Real&  X, const Real& /*Y*/, const Real& /*Z*/, const ID& /*i*/)
{
	if( X == 0 ) return 1.0;
	else return  0.;
}

Real block(const Real& /*t*/, const Real&  X, const Real& Y, const Real& Z, const ID& /*i*/)
{ //*(coordsX<10)*(coordsY>9)*(coordsY<11.5)
	if( X <= 10.0 && Y>= 9.0 && Y<= 11.5 ) return 1.0;
	else return  0.;
}

Real fiberRotationRing(const Real& /*t*/, const Real&  X, const Real& Y, const Real&/*Z*/, const ID& i)
{
	Real R = std::sqrt( X * X + Y * Y);
	//Real teta = std::atan( Y / X );
	Real fz = 0.0;
	Real fx =  Y / R;
	Real fy = - X / R;
	Real sx = X / R;
	Real sy = Y / R;
	Real m = -1.9040;
	Real q = 3.5224;
	Real theta = m * R + q;

//	f01a f001*cos(teta)+f001*s01^2*(1-cos(teta))+s01*s02*f002*(1-cos(teta))
//	f02a s01*s02*f001*(1-cos(teta))+f002*cos(teta)+f002*s02^2*(1-cos(teta))
//	f03a s01*f002*sin(teta)-s02*f001*sin(teta)

    switch (i)
    {
        case 0:
            return  fx * std::cos(theta) + fx * sx * sx * ( 1.0  - std::cos(theta) ) + sx * sy * fy * ( 1.0  - std::cos(theta) );
            break;
        case 1:
            return sx * sy * fy *  ( 1.0  - std::cos(theta) ) + fy * std::cos(theta) + fy * sy * sy * ( 1.0  - std::cos(theta) ) ;
            break;
        case 2:
            return sx * fy * std::sin(theta) - sy * fx * std::sin(theta);
            break;
        default:
            ERROR_MSG ("This entry is not allowed: ud_functions.hpp");
            return 0.;
            break;
    }

}

static Real f0fun(const Real&, const Real& x, const Real&, const Real& , const ID& comp)
{
	Real p = 3.14159265358979;
    Real alpha = -2.0 * p/3. * x + p/3.;

    Real compx = 0.0;
    Real compy = std::cos(alpha);
    Real compz = std::sin(alpha);

//    compx = 0.0;
//    compy = 0.0;
//    compz = 1.0;
    if (comp == 0)
        return compx;
    else if (comp == 1)
        return compy;
    else
        return compz;
}


void computeI4( VectorEpetra& i4, VectorEpetra& sx, VectorEpetra& sy, VectorEpetra& sz, VectorEpetra& f )
{
	i4 *= 0.0;
	Int nLocalDof = i4.epetraVector().MyLength();
	//Int nComponentLocalDof = nLocalDof / 3;
	for (int k(0); k < nLocalDof; k++)
	{
		UInt iGID = sx.blockMap().GID(k);
		UInt jGID = sx.blockMap().GID(k + nLocalDof);
		UInt kGID = sx.blockMap().GID(k + 2 * nLocalDof);

		Real fx, fy, fz;
		Real F11 = sx[iGID] + 1.0;
                Real F12 = sy[iGID];
                Real F13 = sz[iGID];
                Real F21 = sx[jGID];
                Real F22 = sy[jGID] + 1.0;
                Real F23 = sz[jGID];
                Real F31 = sx[kGID];
                Real F32 = sy[kGID];
                Real F33 = sz[kGID] + 1.0;
                fx = F11 * f[iGID];
                fx += ( F12 * f[jGID] );
                fx += ( F13 * f[kGID] );
                fy = F21 * f[iGID];
                fy += ( F22 * f[jGID] );
                fy += ( F23 * f[kGID] );
                fz = F31 * f[iGID];
                fz += ( F32 * f[jGID] );
                fz += ( F33 * f[kGID] );
		
		Real J = F11 * (F22 * F33 - F32 * F23) - F22 * (F21 * F33 - F31 * F23) + F33 * (F21 * F32 - F31 * F22);

                i4[iGID] = fx * fx + fy * fy + fz * fz;
                i4[iGID] *= std::pow(J,-2.0/3.0);

		}


}

Real rescalingGamma(const Real&, const Real& x, const Real&, const Real& , const ID& /*comp*/)
{
	Real r = (1-x);
	Real p = 3.14159265358979;
	Real alpha = -2.0 * p/3. * x + p/3.;
	Real circumferentialShortening = (0.05-0.2) * r +0.2;
	Real max_gamma =  circumferentialShortening / std::cos(alpha);
	return max_gamma;
}


void createPositionVector(const RegionMesh<LinearTetra>& mesh,  VectorEpetra& positionVector)
{
	Int nLocalDof = positionVector.epetraVector().MyLength();
	Int nComponentLocalDof = nLocalDof / 3;
	for (int k(0); k < nComponentLocalDof; k++)
	{
		UInt iGID = positionVector.blockMap().GID(k);
		UInt jGID = positionVector.blockMap().GID(k + nComponentLocalDof);
		UInt kGID = positionVector.blockMap().GID(k + 2 * nComponentLocalDof);

		positionVector[iGID] = mesh.point(iGID).x();
		positionVector[jGID] = mesh.point(iGID).y();
		positionVector[kGID] = mesh.point(iGID).z();
	}


}

Real ComputeVolume( const RegionMesh<LinearTetra> fullMesh,
							VectorEpetra positionVector,
					  const VectorEpetra& disp,
					  int bdFlag,
					  boost::shared_ptr<Epetra_Comm>  comm)
{
	positionVector += disp;
	Real xMin(0.);
	Real xMax(0.);
	Real yMin(0.);
	Real yMax(0.);
	Real zMin(0.);
	Real zMax(0.);
	Int nLocalDof = positionVector.epetraVector().MyLength();
	Int nComponentLocalDof = nLocalDof / 3;

	for (int k(0); k < nComponentLocalDof; k++)
	{
		UInt iGID = positionVector.blockMap().GID(k);
		UInt jGID = positionVector.blockMap().GID(k + nComponentLocalDof);
		UInt kGID = positionVector.blockMap().GID(k + 2 * nComponentLocalDof);
		if( fullMesh.point ( iGID ).markerID() == bdFlag )
		{
			if(positionVector[iGID] > xMax) xMax = positionVector[iGID];
			if(positionVector[iGID] < xMin) xMin = positionVector[iGID];
			if(positionVector[jGID] > yMax) yMax = positionVector[jGID];
			if(positionVector[jGID] < yMin) yMin = positionVector[jGID];
			if(positionVector[kGID] > zMax) zMax = positionVector[kGID];
			if(positionVector[kGID] < zMin) zMin = positionVector[kGID];

		}
	}


//	Real p = 3.14159265358979;
//	Real Volume;
//	if(xDiameter > yDiameter) Volume = p * xDiameter * xDiameter / 4.0 * (zMax - zMin) / 3.0;
//	else Volume = p * yDiameter * xDiameter / 4.0 * (zMax - zMin) / 3.0;
	int numProc = comm -> NumProc();
	Real xMinGlobal(0.);
	Real xMaxGlobal(0.);
	Real yMinGlobal(0.);
	Real yMaxGlobal(0.);
	Real zMinGlobal(0.);
	Real zMaxGlobal(0.);

	if(numProc == 1)
	{
		Real xDiameter = (xMax - xMin );
		Real yDiameter = (yMax - yMin );
		Real Volume = xDiameter * yDiameter * (zMax - zMin) / 2;
		return Volume;
	}
	else
	{
		comm -> MaxAll(&xMax, &xMaxGlobal,1);
		comm -> MinAll(&xMin, &xMinGlobal,1);
		comm -> MaxAll(&yMax, &yMaxGlobal,1);
		comm -> MinAll(&yMin, &yMinGlobal,1);
		comm -> MaxAll(&zMax, &zMaxGlobal,1);
		comm -> MinAll(&zMin, &zMinGlobal,1);

		Real xDiameter = (xMaxGlobal - xMinGlobal );
		Real yDiameter = (yMaxGlobal - yMinGlobal );
		Real Volume = xDiameter * yDiameter * (zMaxGlobal - zMinGlobal) / 2;
		return Volume;
	}

}


Real evaluatePressure(Real Volume, Real dV, Real pn, Real dp_temporal, Real dV_temporal, Real Cp, Real dt = 1.0 )
{
	Real pressure;
	if( dp_temporal > 0 )
	{
		if(pn < 127000)
		{
			//Real Cp = - 5e-3; // ml / mmHG -> ml / ( dyne / cm^2)
			pressure = pn + dV_temporal / Cp;
		}
		else
		{
			Real R = 750 * 1333.22; //mmHg ms / ml
			Real C = 0.2 / 1333.22; //ml / mmHg
			pressure = ( R / ( C * R + dt ) ) * ( pn - dV_temporal );

		}
	}
	else if( dp_temporal < 0 && dV_temporal < 0)
	{
			Real R = 750 * 1333.22; //mmHg ms / ml
			Real C = 0.2 / 1333.22; //ml / mmHg
			pressure = ( R / ( C * R + dt ) ) * ( pn - dV_temporal );
	}
	else
	{
		//Real Cp = - 5e-3; // ml / mmHG -> ml / ( dyne / cm^2)
		pressure = pn + dV / Cp;
	}

	return pressure;
//	return 0.0;
}

int main (int argc, char** argv)
{

    typedef RegionMesh<LinearTetra>                         mesh_Type;
    typedef boost::shared_ptr<mesh_Type>                    meshPtr_Type;
    typedef boost::function < Real (const Real& /*t*/,
                                    const Real &   x,
                                    const Real &   y,
                                    const Real& /*z*/,
                                    const ID&   /*i*/ ) >   function_Type;
    typedef IonicMinimalModel					ionicModel_Type;
    typedef boost::shared_ptr< ionicModel_Type >  ionicModelPtr_Type;

Simone Rossi's avatar
Simone Rossi committed
398
    typedef EMMonodomainSolver< mesh_Type, ionicModel_Type >        monodomainSolver_Type;
Simone Rossi's avatar
Simone Rossi committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
    typedef boost::shared_ptr< monodomainSolver_Type >  monodomainSolverPtr_Type;
    typedef VectorEpetra				vector_Type;
    typedef boost::shared_ptr<vector_Type> vectorPtr_Type;

	typedef MatrixEpetra<Real> matrix_Type;
	typedef boost::shared_ptr<matrix_Type> matrixPtr_Type;

    typedef BCHandler                                          bc_Type;
    typedef boost::shared_ptr< bc_Type >                       bcPtr_Type;
    typedef StructuralOperator< RegionMesh<LinearTetra> >		physicalSolver_Type;
    typedef BCInterface3D< bc_Type, physicalSolver_Type >              bcInterface_Type;
    typedef boost::shared_ptr< bcInterface_Type >              bcInterfacePtr_Type;



#ifdef HAVE_MPI
    MPI_Init ( &argc, &argv );
#endif

    boost::shared_ptr<Epetra_Comm>  comm ( new Epetra_MpiComm (MPI_COMM_WORLD) );
    //*********************************************//
    // creating output folder
    //*********************************************//
    GetPot commandLine ( argc, argv );
    std::string problemFolder = commandLine.follow ( "Output", 2, "-o", "--output" );
    // Create the problem folder
    if ( problemFolder.compare ("./") )
    {
        problemFolder += "/";

        if ( comm->MyPID() == 0 )
        {
            mkdir ( problemFolder.c_str(), 0777 );
        }
    }

  	//===========================================================
  	//===========================================================
  	//				ELECTROPHYSIOLOGY
  	//===========================================================
  	//===========================================================

    if ( comm->MyPID() == 0 )
    {
        cout << "% using MPI" << endl;
    }

    //********************************************//
    // Import parameters from an xml list. Use    //
    // Teuchos to create a list from a given file //
    // in the execution directory.                //
    //********************************************//

    if ( comm->MyPID() == 0 )
    {
        std::cout << "Importing parameters list...";
    }
    Teuchos::ParameterList parameterList = * ( Teuchos::getParametersFromXmlFile ( "ParamList.xml" ) );
    if ( comm->MyPID() == 0 )
    {
        std::cout << " Done!" << endl;
    }


    //********************************************//
    // In the parameter list we need to specify   //
    // the mesh name and the mesh path.           //
    //********************************************//
    if ( comm->MyPID() == 0 )
    {
        std::cout << "Reading Mesh Name and Path...\n";
    }

    std::string meshName = parameterList.get ("mesh_name", "lid16.mesh");
    std::string meshPath = parameterList.get ("mesh_path", "./");

    Real Cp = parameterList.get ("Cp", 1e-5);

//    meshPtr_Type mesh ( new mesh_Type ( comm ) );
//    meshPtr_Type fullMesh ( new mesh_Type ( comm ) );
//    MeshUtility::fillWithFullMesh (mesh, fullMesh, meshName, meshPath);
    if ( comm->MyPID() == 0 )
        {
            std::cout << " Done!" << endl;
        }

    //********************************************//
    // We need the GetPot datafile for to setup   //
    // the preconditioner.                        //
    //********************************************//
    GetPot command_line (argc, argv);
    const string data_file_name = command_line.follow ("data", 2, "-f", "--file");
    GetPot dataFile (data_file_name);

    //********************************************//
    // Creates a new model object representing the//
    // model from Aliev and Panfilov 1996.  The   //
    // model input are the parameters. Pass  the  //
    // parameter list in the constructor          //
    //********************************************//
    if ( comm->MyPID() == 0 )
    {
        std::cout << "Building Constructor for Minimal Model with parameters ... ";
    }
    ionicModelPtr_Type  ionicModel ( new ionicModel_Type() );
    if ( comm->MyPID() == 0 )
    {
        std::cout << " Done!" << endl;
    }

    //********************************************//
    // set up the monodomain solver               //
    //********************************************//
    if ( comm->MyPID() == 0 )
    {
        std::cout << "Building Monodomain Solvers... ";
    }

    monodomainSolverPtr_Type monodomain ( new monodomainSolver_Type ( meshName, meshPath, dataFile, ionicModel ) );
    if ( comm->MyPID() == 0 )
    {
        std::cout << " Splitting solver done... ";
    }

    bool load4restart = parameterList.get("load4restart", false);
//    ionicModel -> initialize( monodomain -> globalSolution() );

    if(load4restart)
    {
    	std::string V0filename = parameterList.get("V0filename", "V0");
    	std::string V0fieldname = parameterList.get("V0fieldname", "V0");
    	ElectrophysiologyUtility::importScalarField(monodomain -> globalSolution().at(0),V0filename,V0fieldname,monodomain -> localMeshPtr() );
    	std::string V1filename = parameterList.get("V1filename", "V1");
    	std::string V1fieldname = parameterList.get("V1fieldname", "V1");
    	ElectrophysiologyUtility::importScalarField(monodomain -> globalSolution().at(1),V1filename,V1fieldname,monodomain -> localMeshPtr() );
    	std::string V2filename = parameterList.get("V2filename", "V2");
    	std::string V2fieldname = parameterList.get("V2fieldname", "V2");
    	ElectrophysiologyUtility::importScalarField(monodomain -> globalSolution().at(2),V2filename,V2fieldname,monodomain -> localMeshPtr() );
    	std::string V3filename = parameterList.get("V3filename", "V3");
    	std::string V3fieldname = parameterList.get("V3fieldname", "V3");
    	ElectrophysiologyUtility::importScalarField(monodomain -> globalSolution().at(3),V3filename,V3fieldname,monodomain -> localMeshPtr() );
    }
    else{

		monodomain -> setInitialConditions();

//		for(int i(0); i < ionicModel -> Size(); i++ )
//		{
//		std::cout << "Norm Inf variable " << i  << " = " <<  (  *( monodomain -> globalSolution().at(i) ) ).normInf() << std::endl;
//		}

		function_Type f = &initialStimulus;
		monodomain -> setPotentialFromFunction(f);

	        vectorPtr_Type aux( new vector_Type( ( monodomain -> globalSolution().at(3) ) -> map() ) );

		bool lbbb = parameterList.get ("lbbb", false);
		if(lbbb==false)	ElectrophysiologyUtility::setValueOnBoundary( *aux, monodomain -> fullMeshPtr(), 1.0, 1 );
		ElectrophysiologyUtility::setValueOnBoundary( *aux, monodomain -> fullMeshPtr(), 1.0, 2 );
		ElectrophysiologyUtility::setValueOnBoundary( *aux, monodomain -> fullMeshPtr(), 1.0, 4 );
		*aux *= *(monodomain -> globalSolution().at(0));
		monodomain -> setPotentialPtr(aux);





    }
		vectorPtr_Type bl( new vector_Type( ( monodomain -> globalSolution().at(3) ) -> map() ) );
		function_Type fff = &block;
		monodomain -> feSpacePtr() -> interpolate(
				static_cast<FESpace<RegionMesh<LinearTetra>, MapEpetra>::function_Type>(fff),
				*bl, 0.0);

    std::cout << "Norm Inf potential = " <<  (  *( monodomain -> globalSolution().at(0) ) ).normInf() << std::endl;

    monodomain -> setParameters ( parameterList );

    //********************************************//
    // Creating exporters to save the solution    //
    //********************************************//
    ExporterHDF5< RegionMesh <LinearTetra> > expElectro;

    for(int pid(0); pid < 4 ; pid ++){
    if ( comm->MyPID() == pid )
    {
        cout << "\nExporter setup:  " ;
    }
    }
    monodomain -> setupExporter ( expElectro, parameterList.get ("ElectroOutputFile", "ElectroOutput") );
    expElectro.setPostDir ( problemFolder );
    if ( comm->MyPID() == 0 )
    {
        cout << "\nExport at 0:  " ;
    }

    monodomain -> exportSolution ( expElectro, 0.0 );

    if ( comm->MyPID() == 0 )
    {
        cout << "\nsolve system:  " ;
    }


    //********************************************//
    // Activation time						      //
    //********************************************//
    vectorPtr_Type activationTimeVector( new vector_Type( monodomain -> potentialPtr() -> map() ) );
    *activationTimeVector = -1.0;

    ExporterHDF5< RegionMesh <LinearTetra> > activationTimeExporter;
    activationTimeExporter.setMeshProcId(monodomain -> localMeshPtr(), monodomain -> commPtr() ->MyPID());
    activationTimeExporter.addVariable(ExporterData<mesh_Type>::ScalarField, "Activation Time",
    				monodomain -> feSpacePtr(), activationTimeVector, UInt(0));
    activationTimeExporter.setPrefix("ActivationTime");
    activationTimeExporter.setPostDir(problemFolder);
  	//===========================================================
  	//===========================================================
  	//				SOLID MECHANICS
  	//===========================================================
  	//===========================================================



    if ( comm->MyPID() == 0 )
    {
        std::cout << "monodomain: passed!" << std::endl;
    }

    typedef FESpace< RegionMesh<LinearTetra>, MapEpetra >               solidFESpace_Type;
    typedef boost::shared_ptr<solidFESpace_Type>                        solidFESpacePtr_Type;

    typedef ETFESpace< RegionMesh<LinearTetra>, MapEpetra, 3, 1 >       scalarETFESpace_Type;
    typedef boost::shared_ptr<scalarETFESpace_Type>                      scalarETFESpacePtr_Type;
    typedef ETFESpace< RegionMesh<LinearTetra>, MapEpetra, 3, 3 >       solidETFESpace_Type;
    typedef boost::shared_ptr<solidETFESpace_Type>                      solidETFESpacePtr_Type;
    if ( comm->MyPID() == 0 )
    {
        std::cout << "\n\ninitialization bc handler" << std::endl;
    }


    for(int pid(0); pid < 4 ; pid ++){
    if ( comm->MyPID() == pid )
    {
        std::cout << "\nparameters" << std::endl;
    }
    }
    Real rho, poisson, young, bulk, alpha, gammai, mu;
    rho     = dataFile ( "solid/physics/density", 1. );
    young   = dataFile ( "solid/physics/young",   1. );
    poisson = dataFile ( "solid/physics/poisson", 1. );
    bulk    = dataFile ( "solid/physics/bulk",    1. );
    alpha   = dataFile ( "solid/physics/alpha",   1. );
    gammai   = dataFile ( "solid/physics/gamma",   1. );
    mu   = dataFile ( "solid/physics/mu",   1. );
  //  M_gammaf  = dataFile ( "solid/physics/gammaf",  0. );

    if ( comm->MyPID() == 0 )
        {
    std::cout << "density = " << rho     << std::endl
              << "young   = " << young   << std::endl
              << "poisson = " << poisson << std::endl
              << "bulk    = " << bulk    << std::endl
              << "alpha   = " << alpha   << std::endl
              << "gamma   = " << gammai   << std::endl;
        }

    for(int pid(0); pid < 4 ; pid ++){
    if ( comm->MyPID() == pid )
    {
        std::cout << "\ninitialization constitutive law" << std::endl;
    }
    }
    boost::shared_ptr<StructuralConstitutiveLawData> dataStructure (new StructuralConstitutiveLawData( ) );
    dataStructure->setup (dataFile);

    if ( comm->MyPID() == 0 )
    {
        std::cout << "setup spaces" << std::endl;
    }

    meshPtr_Type fullSolidMesh;
    meshPtr_Type localSolidMesh;

    std::string solidMeshName = parameterList.get ("solid_mesh_name", "no_solid_mesh");
    bool usingDifferentMeshes = true;
    if(solidMeshName == "no_solid_mesh" || solidMeshName == meshName )
    	{
    	usingDifferentMeshes = false;
    	}
    else
    {
        if ( comm->MyPID() == 0 )
        {
            std::cout << "\nI am using different meshes: " << meshName << " for the electro and " << solidMeshName << " for the solid\n\n" << std::endl;
        }
    }

    if( usingDifferentMeshes  )
    {
    	fullSolidMesh.reset(new mesh_Type ( comm ) );
    	localSolidMesh.reset(new mesh_Type ( comm ) );
    	MeshUtility::loadMesh (localSolidMesh, fullSolidMesh,  solidMeshName,  parameterList.get ("solid_mesh_path", "") );
    }
    else
    {
    	fullSolidMesh = monodomain -> fullMeshPtr();
    	localSolidMesh = monodomain -> localMeshPtr();
    }

    std::string dOrder =  dataFile ( "solid/space_discretization/order", "P1");
    solidFESpacePtr_Type dFESpace ( new solidFESpace_Type (localSolidMesh, dOrder, 3, comm) );
    solidFESpacePtr_Type aFESpace ( new solidFESpace_Type (monodomain -> localMeshPtr(), dOrder, 1, comm) );
    solidETFESpacePtr_Type dETFESpace ( new solidETFESpace_Type (localSolidMesh, & (dFESpace->refFE() ), & (dFESpace->fe().geoMap() ), comm) );
    scalarETFESpacePtr_Type aETFESpace ( new scalarETFESpace_Type (monodomain -> localMeshPtr(), & (aFESpace->refFE() ), & (aFESpace->fe().geoMap() ), comm) );
    solidFESpacePtr_Type solidaFESpace ( new solidFESpace_Type (localSolidMesh, "P1", 1, comm) );

    for(int pid(0); pid < 4 ; pid ++){
    if ( comm->MyPID() == pid )
    {
        std::cout << "\nsetup boundary conditions" << std::endl;
    }
    }
    bcInterfacePtr_Type                     solidBC( new bcInterface_Type() );
    solidBC->createHandler();
    solidBC->fillHandler ( data_file_name, "solid" );

    //M_FSIoperator->setSolidBC ( M_solidBC->handler() );
    for(int pid(0); pid < 4 ; pid ++){
    if ( comm->MyPID() == pid )
    {
        std::cout << "\nsetup structural operator" << std::endl;
    }
    }
    //! 1. Constructor of the structuralSolver
     EMStructuralOperator< RegionMesh<LinearTetra> > solid;
     solid.setup (dataStructure,
                  dFESpace,
                  dETFESpace,
                  solidBC -> handler(),
                  comm);
     for(int pid(0); pid < 4 ; pid ++){
     if ( comm->MyPID() == pid )
     {
         std::cout << "\ninitial guess" << std::endl;
     }
     }
     solid.setDataFromGetPot (dataFile);

     if(load4restart)
     {
     	std::string Dfilename = parameterList.get("Gfilename", "G");
     	std::string Dfieldname = parameterList.get("Gfieldname", "G");
     	ElectrophysiologyUtility::importVectorField( solid.displacementPtr(), Dfilename, Dfieldname,  localSolidMesh );
     }
    	//===========================================================
    	//===========================================================
    	//				FIBERS
    	//===========================================================
    	//===========================================================
     for(int pid(0); pid < 4 ; pid ++){
     if ( comm->MyPID() == pid )
     {
         std::cout << "\nreading fibers ... " << std::endl;
     }
     }


     function_Type fibersDirection = &f0fun;
     vectorPtr_Type solidFibers( new vector_Type( dFESpace -> map() ) );
     MPI_Barrier(MPI_COMM_WORLD);
     dFESpace -> interpolate ( static_cast< FESpace< RegionMesh<LinearTetra>, MapEpetra >::function_Type > ( fibersDirection ), *solidFibers , 0);
     ElectrophysiologyUtility::normalize(*solidFibers);

     MPI_Barrier(MPI_COMM_WORLD);


     if ( comm->MyPID() == 0 )
     {
         std::cout << "\nread fibers" << std::endl;
     }

     ElectrophysiologyUtility::importVectorField(solidFibers,  parameterList.get ("solid_fiber_file", ""),  parameterList.get ("solid_fiber_field", ""), localSolidMesh );
     //template<typename Mesh> inline void importVectorField(  boost::shared_ptr<VectorEpetra> vector, const std::string& fileName, const std::string& fieldName, boost::shared_ptr< Mesh > localMesh  )

//     std::vector<Real> fvec(3, 0.0);
//     fvec.at(0)  = parameterList.get ("fiber_X", 1.0);
//     fvec.at(1)  = parameterList.get ("fiber_Y", 0.0);
//     fvec.at(2)  = parameterList.get ("fiber_Z", 0.0);
//     HeartUtility::setupFibers(*solidFibers, fvec);
     solid.activeMaterial() -> setFiberVector( *solidFibers );


//     Real sx = 1.0;//parameterList.get ("sheet_X", 1.0);
//     Real sy = 0.0;//parameterList.get ("sheet_Y", 0.0);
//     Real sz = 0.0;//parameterList.get ("sheet_Z", 0.0);;
     vectorPtr_Type solidSheets( new vector_Type( solidFibers -> map() ) );
     ElectrophysiologyUtility::importVectorField(solidSheets,  parameterList.get ("solid_sheets_file", ""),  parameterList.get ("solid_sheets_field", ""), localSolidMesh );

     solid.activeMaterial()->setSheetVector(*solidSheets);

     if ( comm->MyPID() == 0 )
     {
         std::cout << "\nset fibers" << std::endl;
     }

//     solid.activeMaterial() -> setFiberVector( *solidFibers );

//     monodomain -> setupFibers();

     vectorPtr_Type gammaf( new vector_Type( ( monodomain -> globalSolution().at(3) ) -> map() ) );
     vectorPtr_Type gammas( new vector_Type( ( monodomain -> globalSolution().at(3) ) -> map() ) );
     vectorPtr_Type gamman( new vector_Type( ( monodomain -> globalSolution().at(3) ) -> map() ) );
     vectorPtr_Type solidGammaf;
     vectorPtr_Type emDisp;
     solidFESpacePtr_Type electroFiberFESpace;
     solidETFESpacePtr_Type electrodETFESpace;
     if(usingDifferentMeshes)
     {



    	 electroFiberFESpace.reset ( new solidFESpace_Type (monodomain -> localMeshPtr(), "P1", 3, comm) );
    	 electrodETFESpace.reset ( new solidETFESpace_Type (monodomain -> localMeshPtr(), & (dFESpace->refFE() ), & (dFESpace->fe().geoMap() ), comm) );

         vectorPtr_Type electroFibers( new vector_Type( electroFiberFESpace -> map() ) );
//       HeartUtility::setupFibers(*electroFibers, fvec);
//       vectorPtr_Type fibersRotated( new vector_Type( monodomain -> feSpacePtr() -> map() ) );

         electroFiberFESpace -> interpolate ( static_cast< FESpace< RegionMesh<LinearTetra>, MapEpetra >::function_Type > ( fibersDirection ), *electroFibers , 0);
         ElectrophysiologyUtility::normalize(*electroFibers);
//     HeartUtility::importFibers(electroFibers, parameterList.get ("fiber_file", ""), monodomain -> localMeshPtr() );
         monodomain -> setFiberPtr( electroFibers );
    	 emDisp.reset(  new vector_Type( electroFibers -> map() ) );
    	 solidGammaf.reset( new vector_Type( solidaFESpace -> map() ) );

     }
     else
     {
    	solidGammaf = gammaf;
    	 monodomain -> setFiberPtr( solidFibers );
    	 emDisp = solid.displacementPtr();
    	 electroFiberFESpace = dFESpace;
    	 electrodETFESpace = dETFESpace;
     }


     monodomain -> exportFiberDirection(problemFolder);
     boost::shared_ptr< Exporter<RegionMesh<LinearTetra> > > exporterSheets;
     exporterSheets.reset ( new ExporterHDF5<RegionMesh<LinearTetra> > ( dataFile, "SheetsDirection" ) );

       //      exporter->setPostDir ( "./" );
             exporterSheets -> setPostDir ( problemFolder );
       exporterSheets->setMeshProcId ( localSolidMesh, comm->MyPID() );
       exporterSheets->addVariable ( ExporterData<RegionMesh<LinearTetra> >::VectorField, "sheets", dFESpace, solidSheets, UInt (0) );
       exporterSheets->postProcess(0);
       exporterSheets->closeFile();
       //********************************************//
     // Create the global matrix: mass + stiffness in ELECTROPHYSIOLOGY //
     //********************************************//
     if ( comm->MyPID() == 0 )
     {
         cout << "\nSetup operators:  dt = " << monodomain -> timeStep() << "\n" ;
     }

     monodomain -> setDisplacementPtr( emDisp );
     monodomain -> setupLumpedMassMatrix();
     monodomain -> setupStiffnessMatrix();
     monodomain -> setupGlobalMatrix();

     if ( comm->MyPID() == 0 )
     {
         cout << "Done! \n" ;
     }

     //==================================================================//
     //==================================================================//
     //					SETUP Activation								//
     //==================================================================//
     //==================================================================//

     //   vectorPtr_Type gammaf( new vector_Type( monodomain -> globalSolution().at(3) -> map() ) );

     if(load4restart)
     {
     	std::string Gfilename = parameterList.get("Gfilename", "G");
     	std::string Gfieldname = parameterList.get("Gfieldname", "G");
     	ElectrophysiologyUtility::importScalarField(gammaf,Gfilename,Gfieldname,monodomain -> localMeshPtr() );
     }
     else *gammaf *= 0;
        *solidGammaf = parameterList.get("initial_gamma_f",0.0);


     if ( comm->MyPID() == 0 )
     {
         std::cout << "\nset gammaf and fibers" << std::endl;
     }
     solid.activeMaterial() -> setGammaf( *solidGammaf );

     vectorPtr_Type solidGammas( new vector_Type( solidGammaf -> map() ) );
     vectorPtr_Type solidGamman( new vector_Type( solidGammaf -> map() ) );
     Int gcase = parameterList.get ("case", 0);
     if ( comm->MyPID() == 0 )
     {
         std::cout << "\ncase: " << gcase << std::endl;
     }
     if(gcase == 1)
     {
		 Real gfactor = parameterList.get ("gfactor", 3.0);
		 *solidGamman = gfactor * *solidGammaf;
		 solid.activeMaterial() -> setGamman(*solidGamman);
		 *solidGammas = 1.0;
		 *solidGammas /= (1.0 + *solidGammaf);
		 *solidGammas /= (1.0 + *solidGamman);
		 *solidGammas -= 1.0;
		 solid.activeMaterial() -> setGammas(*solidGammas);

		 if(usingDifferentMeshes)
		 {
		 *gamman = gfactor * *gammaf;
		 *gammas = 1.0;
		 *gammas /= (1.0 + *gammaf);
		 *gammas /= (1.0 + *gamman);
		 *gammas -= 1.0;
		 }
		 else
		 {
			 gamman = solidGamman;
			 gammas = solidGammas;
		 }
     }
     else
     {
		 *solidGammas = 1.0;
		 *solidGammas /= (1.0 + *solidGammaf);
		 EpetraSqrt(*solidGammas);
		 *solidGammas -= 1.0;
		 solid.activeMaterial() -> setGamman(*solidGammas);
		 *solidGamman = *solidGammas;
		 solid.activeMaterial() -> setGammas(*solidGamman);

		 if(usingDifferentMeshes)
		 {
		 *gammas = 1.0;
		 *gammas /= (1.0 + *gammaf);
		 EpetraSqrt(*gammas);
		 *gammas -= 1.0;
 		 *gamman = *gammas;
		 }
		 else
		 {
			 gamman = solidGamman;
			 gammas = solidGammas;
		 }
     }

     //==================================================================//
     //==================================================================//
     //					SETUP INTERPOLATION								//
     //==================================================================//
     //==================================================================//

	 typedef RBFInterpolation<mesh_Type>           interpolation_Type;
	 typedef boost::shared_ptr<interpolation_Type> interpolationPtr_Type;

	 //Coarse To Fine ( C2F )
	 interpolationPtr_Type C2F;
	 //Fine To Coarse ( F2C )
	 interpolationPtr_Type F2C;
     if(usingDifferentMeshes)
     {
		    Teuchos::RCP< Teuchos::ParameterList > belosList = Teuchos::rcp ( new Teuchos::ParameterList );
		    belosList = Teuchos::getParametersFromXmlFile ( "ParamList.xml" );

		     if ( comm->MyPID() == 0 )
		     {
		         std::cout << "\nresetting" << std::endl;
		     }
		     std::string c2f = parameterList.get ("c2f", "RBFrescaledVectorial");
		     std::string f2c = parameterList.get ("f2c", "RBFrescaledScalar");

	     C2F.reset ( interpolation_Type::InterpolationFactory::instance().createObject ( c2f ) );
		 F2C.reset ( interpolation_Type::InterpolationFactory::instance().createObject ( f2c ) );
//		 C2F.reset ( interpolation_Type::InterpolationFactory::instance().createObject ( "RBFlocallyRescaledVectorial" ) );
//		 F2C.reset ( interpolation_Type::InterpolationFactory::instance().createObject ( "RBFlocallyRescaledScalar" ) );

			int nFlags = 1;
			std::vector<int> flags (nFlags);
			flags[0] = -1;

		     if ( comm->MyPID() == 0 )
		     {
		         std::cout << "\nintepolation setup" << std::endl;
		     }

			C2F->setup( fullSolidMesh , localSolidMesh, monodomain -> fullMeshPtr(), monodomain -> localMeshPtr(), flags);
			F2C->setup( monodomain -> fullMeshPtr(), monodomain -> localMeshPtr(), fullSolidMesh , localSolidMesh, flags);


		     if ( comm->MyPID() == 0 )
		     {
For faster browsing, not all history is shown. View entire blame