HeartSolver.hpp 9.61 KB
Newer Older
thomaskummer's avatar
thomaskummer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
//
//  HeartSolver.cpp
//  LifeV
//
//  Created by Thomas Kummer on 03.05.16.
//  Copyright © 2016 Thomas Kummer. All rights reserved.
//

#ifndef _HEARTDATA_H_
#define _HEARTDATA_H_


#include <stdio.h>
thomaskummer's avatar
thomaskummer committed
14
#include <lifev/em/solver/EMSolver.hpp>
thomaskummer's avatar
thomaskummer committed
15
#include <lifev/em/solver/circulation/Circulation.hpp>
thomaskummer's avatar
thomaskummer committed
16

thomaskummer's avatar
thomaskummer committed
17
#include <lifev/em/solver/HeartData.hpp>
thomaskummer's avatar
thomaskummer committed
18

thomaskummer's avatar
thomaskummer committed
19
20
21
22
23

namespace LifeV
{

    
thomaskummer's avatar
thomaskummer committed
24
25
26
27
template <class EmSolver>
class HeartSolver {
   
public:
thomaskummer's avatar
thomaskummer committed
28
    
thomaskummer's avatar
thomaskummer committed
29
30
    HeartSolver(EmSolver& emSolver,  Circulation& circulationSolver) :
        M_emSolver          (emSolver),
thomaskummer's avatar
thomaskummer committed
31
32
        M_circulationSolver (circulationSolver),
        M_heartData         (HeartData())
thomaskummer's avatar
thomaskummer committed
33
    {}
thomaskummer's avatar
thomaskummer committed
34
    
thomaskummer's avatar
thomaskummer committed
35
    virtual ~HeartSolver() {}
thomaskummer's avatar
thomaskummer committed
36
37
38
39
40
41
42
43
44
45

    EmSolver& emSolver()
    {
        return M_emSolver;
    }
    
    Circulation& circulation()
    {
        return M_heartData;
    }
thomaskummer's avatar
thomaskummer committed
46
    
thomaskummer's avatar
thomaskummer committed
47
    const HeartData& data() const
thomaskummer's avatar
thomaskummer committed
48
49
50
51
    {
        return M_heartData;
    }
    
thomaskummer's avatar
thomaskummer committed
52
    void setup(const GetPot& datafile)
thomaskummer's avatar
thomaskummer committed
53
    {
thomaskummer's avatar
thomaskummer committed
54
        M_heartData.setup(datafile);
thomaskummer's avatar
thomaskummer committed
55
    }
thomaskummer's avatar
thomaskummer committed
56
    
thomaskummer's avatar
thomaskummer committed
57
    template <class lambda>
thomaskummer's avatar
thomaskummer committed
58
    void preload(const lambda& modifyFeBC, const std::vector<Real>& bcValues)
thomaskummer's avatar
thomaskummer committed
59
    {
thomaskummer's avatar
thomaskummer committed
60
        M_emSolver.structuralOperatorPtr() -> data() -> dataTime() -> setTime(0.0);
thomaskummer's avatar
thomaskummer committed
61
62
63
64
65
66
67
68
69
70
        
        auto preloadPressure = [] (std::vector<double> p, const int& step, const int& steps)
        {
            for (auto& i : p) {i *= double(step) / double(steps);}
            return p;
        };
        
        LifeChrono chronoSave;
        chronoSave.start();
        
thomaskummer's avatar
thomaskummer committed
71
        M_emSolver.saveSolution (-1.0);
thomaskummer's avatar
thomaskummer committed
72
        
thomaskummer's avatar
thomaskummer committed
73
        if ( 0 == M_emSolver.comm()->MyPID() )
thomaskummer's avatar
thomaskummer committed
74
75
76
77
78
79
80
81
82
83
84
        {
            std::cout << "\n*****************************************************************";
            std::cout << "\nData stored in " << chronoSave.diff() << " s";
            std::cout << "\n*****************************************************************\n";
        }
        
        LifeChrono chronoPreload;
        chronoPreload.start();
        
        for (int i (1); i <= data().preloadSteps(); i++)
        {
thomaskummer's avatar
thomaskummer committed
85
            if ( 0 == M_emSolver.comm()->MyPID() )
thomaskummer's avatar
thomaskummer committed
86
87
88
89
90
91
92
            {
                std::cout << "\n*****************************************************************";
                std::cout << "\nPreload step: " << i << " / " << data().preloadSteps();
                std::cout << "\n*****************************************************************\n";
            }
            
            // Update pressure b.c.
thomaskummer's avatar
thomaskummer committed
93
            modifyFeBC(preloadPressure(bcValues, i, data().preloadSteps() ));
thomaskummer's avatar
thomaskummer committed
94
95
            
            // Solve mechanics
thomaskummer's avatar
thomaskummer committed
96
97
            M_emSolver.bcInterfacePtr() -> updatePhysicalSolverVariables();
            M_emSolver.solveMechanics();
thomaskummer's avatar
thomaskummer committed
98
99
            
            // Safe preload steps
thomaskummer's avatar
thomaskummer committed
100
            if ( data().safePreload() ) M_emSolver.saveSolution (i-1);
thomaskummer's avatar
thomaskummer committed
101
102
        }
        
thomaskummer's avatar
thomaskummer committed
103
        if ( 0 == M_emSolver.comm()->MyPID() )
thomaskummer's avatar
thomaskummer committed
104
105
106
107
108
109
110
        {
            std::cout << "\n*****************************************************************";
            std::cout << "\nPreload done in: " << chronoPreload.diff();
            std::cout << "\n*****************************************************************\n";
        }

    }
thomaskummer's avatar
thomaskummer committed
111
112
    
    
thomaskummer's avatar
thomaskummer committed
113
    void restart(std::string& restartInput, const GetPot& command_line, Real& t)
thomaskummer's avatar
thomaskummer committed
114
    {
thomaskummer's avatar
thomaskummer committed
115
        const std::string restartDir = ""; //command_line.follow (problemFolder.c_str(), 2, "-rd", "--restartDir");
thomaskummer's avatar
thomaskummer committed
116
117
118
119
        
        Real dtExport = 10.;
        
        // Get most recent restart index
thomaskummer's avatar
thomaskummer committed
120
121
122
123
        //if ( restartInput == "." )
        //{
        //    restartInput = pipeToString( ("tail -n 1 " + restartDir + "solution.dat | awk -F '[. ]' '{print $1 \".\" $2}' | awk '{printf \"%05g\", int($1*1000/" + std::to_string(dtExport) + ") + 1}'").c_str() );
        //}
thomaskummer's avatar
thomaskummer committed
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
        
        // Set time variable
        const unsigned int restartInputStr = std::stoi(restartInput);
        const unsigned int nIter = (restartInputStr - 1) * dtExport / data().dt_mechanics();
        t = nIter * data().dt_mechanics();
        
        if ( 0 == M_emSolver.comm()->MyPID() )
        {
            std::cout << "\nLoad from restart: " << restartInput << ",  nIterCirculation = " << nIter << ",  time = " << t << std::endl;
        }
        
        // Set time exporter time index
        M_emSolver.setTimeIndex(restartInputStr + 1);
        //solver.importHdf5();
        if ( 0 == M_emSolver.comm()->MyPID() )
        {
            std::cout << "\nLoad from restart: " << restartInput << ",  nIterCirculation = " << nIter << ",  time = " << t << std::endl;
        }
        // Load restart solutions from output files
        std::string polynomialDegree = data().datafile() ( "solid/space_discretization/order", "P1");
        if ( 0 == M_emSolver.comm()->MyPID() )
        {
            std::cout << "\nLoad from restart: " << restartInput << ",  nIterCirculation = " << nIter << ",  time = " << t << std::endl;
        }
        ElectrophysiologyUtility::importVectorField ( M_emSolver.structuralOperatorPtr() -> displacementPtr(), "MechanicalSolution" , "displacement", M_emSolver.localMeshPtr(), restartDir, polynomialDegree, restartInput );
        if ( 0 == M_emSolver.comm()->MyPID() )
        {
            std::cout << "\nLoad from restart: " << restartInput << ",  nIterCirculation = " << nIter << ",  time = " << t << std::endl;
        }
        for ( unsigned int i = 0; i < M_emSolver.electroSolverPtr()->globalSolution().size() ; ++i )
        {
            ElectrophysiologyUtility::importScalarField (M_emSolver.electroSolverPtr()->globalSolution().at(i), "ElectroSolution" , ("Variable" + std::to_string(i)), M_emSolver.localMeshPtr(), restartDir, polynomialDegree, restartInput );
        }
        if ( 0 == M_emSolver.comm()->MyPID() )
        {
            std::cout << "\nLoad from restart: " << restartInput << ",  nIterCirculation = " << nIter << ",  time = " << t << std::endl;
        }
        ElectrophysiologyUtility::importScalarField (M_emSolver.activationModelPtr() -> fiberActivationPtr(), "ActivationSolution" , "Activation", M_emSolver.localMeshPtr(), restartDir, polynomialDegree, restartInput );
        //ElectrophysiologyUtility::importScalarField (solver.activationTimePtr(), "ActivationTimeSolution" , "Activation Time", solver.localMeshPtr(), restartDir, polynomialDegree, restartInput );
        if ( 0 == M_emSolver.comm()->MyPID() )
        {
            std::cout << "\nLoad from restart: " << restartInput << ",  nIterCirculation = " << nIter << ",  time = " << t << std::endl;
        }
thomaskummer's avatar
thomaskummer committed
167
        M_circulationSolver.restartFromFile ( restartDir + "solution.dat" , nIter );
thomaskummer's avatar
thomaskummer committed
168
169
170
    }
    
    
thomaskummer's avatar
thomaskummer committed
171
    
thomaskummer's avatar
thomaskummer committed
172
protected:
thomaskummer's avatar
thomaskummer committed
173
    
thomaskummer's avatar
thomaskummer committed
174
    
thomaskummer's avatar
thomaskummer committed
175
176
    EmSolver& M_emSolver;
    Circulation& M_circulationSolver;
thomaskummer's avatar
thomaskummer committed
177
    
thomaskummer's avatar
thomaskummer committed
178
    HeartData M_heartData;
thomaskummer's avatar
thomaskummer committed
179
    
thomaskummer's avatar
thomaskummer committed
180
    
thomaskummer's avatar
thomaskummer committed
181
182
183
184
    VectorSmall<2> M_pressure;
    VectorSmall<2> M_volume;

    
thomaskummer's avatar
thomaskummer committed
185

thomaskummer's avatar
thomaskummer committed
186
    
thomaskummer's avatar
thomaskummer committed
187
188
189
190
191
192
193
194
195
196
197
    std::string pipeToString ( const char* command )
    {
        FILE* file = popen( command, "r" ) ;
        std::ostringstream stm ;
        char line[6] ;
        fgets( line, 6, file );
        stm << line;
        pclose(file) ;
        return stm.str() ;
    };
    
thomaskummer's avatar
thomaskummer committed
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
    Real patchForce (const Real& t, const Real& Tmax, const Real& tmax, const Real& tduration) const
    {
        bool time ( fmod(t-tmax+0.5*tduration, 700.) < tduration && fmod(t-tmax+0.5*tduration, 700.) > 0);
        Real force = std::pow( std::sin(fmod(t-tmax+0.5*tduration, 700.)*3.14159265359/tduration) , 2 ) * Tmax;
        return ( time ? force : 0 );
    }
    
    Real patchFunction (const Real& t, const Real&  X, const Real& Y, const Real& Z, const ID& /*i*/) const
    {
        Real disp = std::pow( std::sin(fmod(t, 700.) * 3.14159265359/300) , 2 )*15;
        return disp;
    }
    
    Real Iapp (const Real& t, const Real&  X, const Real& Y, const Real& Z, const ID& /*i*/) const
    {
        bool coords ( Y < -7. );
        //bool coords ( Y > 4. ); //( Y > 1.5 && Y < 3 );
        bool time ( fmod(t, 700.) < 4 && fmod(t, 700.) > 2);
        return ( coords && time ? 30 : 0 );
    }
    
    Real potentialMultiplyerFcn (const Real& t, const Real&  X, const Real& Y, const Real& Z, const ID& /*i*/) const
    {
        bool time ( fmod(t, 700.) < 4 && fmod(t, 700.) > 2);
        return 1.4 * time; // ( Y < 2.5 && Y > 0.5 ? 1.0 : 0.0 );
    }
    
thomaskummer's avatar
thomaskummer committed
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
    Real
    externalPower ( const VectorEpetra& dispCurrent,
                    const VectorEpetra& dispPrevious,
                    const boost::shared_ptr<ETFESpace< RegionMesh<LinearTetra>, MapEpetra, 3, 3 > > dispETFESpace,
                    Real pressure,
                    Real dt,
                    const unsigned int bdFlag) const
    {
        VectorEpetra traction ( dispCurrent.map() );
        VectorEpetra velocity ( (dispCurrent - dispPrevious) / dt );
        
        MatrixSmall<3,3> Id;
        Id(0,0) = 1.; Id(0,1) = 0., Id(0,2) = 0.;
        Id(1,0) = 0.; Id(1,1) = 1., Id(1,2) = 0.;
        Id(2,0) = 0.; Id(2,1) = 0., Id(2,2) = 1.;
        
        {
            using namespace ExpressionAssembly;
            
            auto I = value(Id);
            auto Grad_u = grad( dispETFESpace, dispCurrent, 0);
            auto F =  Grad_u + I;
            auto FmT = minusT(F);
            auto J = det(F);
            auto p = value(pressure);
            
            QuadratureBoundary myBDQR (buildTetraBDQR (quadRuleTria7pt) );
            
            integrate ( boundary ( dispETFESpace->mesh(), bdFlag),
                       myBDQR,
                       dispETFESpace,
                       p * J * dot( FmT * Nface,  phi_i)
                       //p * J * dot( FmT * Nface,  phi_i)
                       //value(-1.0) * J * dot (vE1, FmT * Nface) * phi_i) >> intergral
                       ) >> traction;
            
            traction.globalAssemble();
        }
        
        return traction.dot(velocity);
    }
    
    
thomaskummer's avatar
thomaskummer committed
268
269
270
271
};

}

thomaskummer's avatar
thomaskummer committed
272
#endif