/
TestBidomainProblem.hpp
1328 lines (1071 loc) · 57.8 KB
/
TestBidomainProblem.hpp
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
398
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
/*
Copyright (c) 2005-2024, University of Oxford.
All rights reserved.
University of Oxford means the Chancellor, Masters and Scholars of the
University of Oxford, having an administrative office at Wellington
Square, Oxford OX1 2JD, UK.
This file is part of Chaste.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Oxford nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TESTBIDOMAINPROBLEM_HPP_
#define TESTBIDOMAINPROBLEM_HPP_
#include <cxxtest/TestSuite.h>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <vector>
#include "LuoRudy1991.hpp"
#include "FaberRudy2000.hpp"
#include "BidomainProblem.hpp"
#include "MonodomainProblem.hpp"
#include "Hdf5DataReader.hpp"
#include "PlaneStimulusCellFactory.hpp"
#include "HeartEventHandler.hpp"
#include "PetscTools.hpp"
#include "PetscSetupAndFinalize.hpp"
#include "ArchiveOpener.hpp"
#include "CmguiMeshWriter.hpp"
#include "FileFinder.hpp"
#include "OutputFileHandler.hpp"
#include "MemfemMeshReader.hpp"
#include "BidomainSolver.hpp"
#include "CompareHdf5ResultsFiles.hpp"
#include "NumericFileComparison.hpp"
#include "Electrodes.hpp"
#include "SimpleBathProblemSetup.hpp"
#include "FileComparison.hpp"
#include "SingleTraceOutputModifier.hpp"
#ifdef CHASTE_VTK
#define _BACKWARD_BACKWARD_WARNING_H 1 //Cut out the strstream deprecated warning for now (gcc4.3)
#include <vtkVersion.h>
#endif
/**
* This stimulus causes an exception to be thrown when normally a simple stimulus would activate.
*
* Used for testing exception handling in TestBidomain3dErrorHandling
*/
class ExceptionStimulus : public AbstractStimulusFunction
{
private:
/** Duration of initial stimulus, typically in milliseconds */
double mDuration;
/** The time at which the stimulus starts, typically in milliseconds */
double mTimeOfStimulus;
public:
/**
* Constructor.
*
* @param duration Duration of initial stimulus milliseconds
* @param timeOfStimulus The time at which the stimulus starts (defaults to 0.0) milliseconds
*/
ExceptionStimulus(double duration, double timeOfStimulus)
{
mDuration = duration;
mTimeOfStimulus = timeOfStimulus;
mDuration += (mDuration+mTimeOfStimulus)*DBL_EPSILON;
}
/**
* @param time time at which to return the stimulus
* @return zero or throws an Exception at the time specified by constructor.
*/
double GetStimulus(double time)
{
if (mTimeOfStimulus < time && time <= mDuration+mTimeOfStimulus)
{
EXCEPTION("Stimulus has caused a bad thing to happen in a cell model.");
}
return 0.0;
}
};
/**
* ExceptionStimulusCellFactory provides all cells with an ExceptionStimulus.
*
* Used for testing exception handling in TestBidomain3dErrorHandling
*/
template<class CELL, unsigned ELEMENT_DIM, unsigned SPACE_DIM = ELEMENT_DIM>
class ExceptionStimulusCellFactory : public AbstractCardiacCellFactory<ELEMENT_DIM,SPACE_DIM>
{
protected:
/** The stimulus to apply at stimulated nodes */
boost::shared_ptr<ExceptionStimulus> mpStimulus;
public:
/**
* Constructor
* @param stimulusDuration The duration of time the exception should be applied (defaults to 1ms).
* @param startTime When the exception should be introduced (defaults to 0.0).
*/
ExceptionStimulusCellFactory(double stimulusDuration=1, double startTime = 0.0)
: AbstractCardiacCellFactory<ELEMENT_DIM,SPACE_DIM>()
{
mpStimulus.reset(new ExceptionStimulus(stimulusDuration, startTime));
}
/**
* @param node The global index of a node
* @return A cardiac cell which corresponds to this node.
*/
AbstractCardiacCellInterface* CreateCardiacCellForTissueNode(Node<SPACE_DIM>* pNode)
{
return new CELL(this->mpSolver, mpStimulus);
}
};
class DelayedTotalStimCellFactory : public AbstractCardiacCellFactory<1>
{
private:
// define a new stimulus
boost::shared_ptr<SimpleStimulus> mpIntraStimulus;
public:
DelayedTotalStimCellFactory(double mag)
: AbstractCardiacCellFactory<1>(),
mpIntraStimulus(new SimpleStimulus( mag, 0.1, 0.1))
{
}
AbstractCardiacCell* CreateCardiacCellForTissueNode(Node<1>* pNode)
{
return new CellLuoRudy1991FromCellML(mpSolver, mpIntraStimulus);
}
};
class TestBidomainProblem : public CxxTest::TestSuite
{
private:
std::vector<double> mSolutionReplicated1d2ms;///<Used to test differences between tests
public:
void tearDown()
{
HeartConfig::Reset();
}
void TestBidomainErrorHandling()
{
if (PetscTools::GetNumProcs() > 2u)
{
// There are only 2 nodes in this simulation
TS_TRACE("This test is not suitable for more than 2 processes.");
return;
}
HeartConfig::Instance()->SetIntracellularConductivities(Create_c_vector(1.75, 1.75, 1.75));
HeartConfig::Instance()->SetExtracellularConductivities(Create_c_vector(7.0, 7.0, 7.0));
HeartConfig::Instance()->SetSimulationDuration(1.0); //ms
HeartConfig::Instance()->SetMeshFileName("mesh/test/data/1D_0_to_1_1_element");
HeartConfig::Instance()->SetOutputDirectory("BidomainWithErrors");
HeartConfig::Instance()->SetOutputFilenamePrefix("bidomain");
// Check the linear system can be solved to a low tolerance (in particular, checks the null space
// stuff was implemented correctly
HeartConfig::Instance()->SetUseAbsoluteTolerance(1e-14);
// We put in a massive stimulus, and alter the printing timestep from default, to cover
// some memory handling in the case of exceptions being thrown (#1222).
HeartConfig::Instance()->SetOdePdeAndPrintingTimeSteps(0.01, 0.01, 0.1);
// 100x bigger stimulus than normal.
ExceptionStimulusCellFactory<CellLuoRudy1991FromCellML, 1> bidomain_cell_factory(1, 0.02);
BidomainProblem<1> bidomain_problem( &bidomain_cell_factory );
bidomain_problem.Initialise();
TS_ASSERT_THROWS_THIS(bidomain_problem.Solve(),
"Stimulus has caused a bad thing to happen in a cell model.");
}
//This test reproduces the problem size of heart/test/data/xml/bidomain1d_small.xml
void TestBidomainSmallestMesh()
{
if (PetscTools::GetNumProcs() > 3u)
{
TS_TRACE("This test is not suitable for more than 3 processes.");
return;
}
HeartConfig::Instance()->SetSimulationDuration(1.0); //ms
HeartConfig::Instance()->SetFibreLength(0.1, 0.05);
HeartConfig::Instance()->SetSpaceDimension(1u);
HeartConfig::Instance()->SetOutputDirectory("Bidomain1dSmall");
HeartConfig::Instance()->SetOutputFilenamePrefix("small");
HeartConfig::Instance()->SetOdePdeAndPrintingTimeSteps(0.01, 0.1, 0.1);
PlaneStimulusCellFactory<CellLuoRudy1991FromCellML, 1> bidomain_cell_factory;
BidomainProblem<1> bidomain_problem( &bidomain_cell_factory );
bidomain_problem.Initialise();
bidomain_problem.Solve();
}
// NOTE: This test uses NON-PHYSIOLOGICAL parameters values (conductivities,
// surface-area-to-volume ratio, capacitance, stimulus amplitude). Essentially,
// the equations have been divided through by the surface-area-to-volume ratio.
// (Historical reasons...)
void TestBidomainDg01DPinned()
{
HeartConfig::Instance()->SetIntracellularConductivities(Create_c_vector(0.0005));
HeartConfig::Instance()->SetExtracellularConductivities(Create_c_vector(0.0005));
HeartConfig::Instance()->SetSimulationDuration(1.0); //ms
HeartConfig::Instance()->SetMeshFileName("mesh/test/data/1D_0_to_1_100_elements");
HeartConfig::Instance()->SetOutputDirectory("bidomainDg01d");
HeartConfig::Instance()->SetOutputFilenamePrefix("BidomainLR91_1d");
PlaneStimulusCellFactory<CellLuoRudy1991FromCellML, 1> bidomain_cell_factory;
BidomainProblem<1> bidomain_problem( &bidomain_cell_factory );
bidomain_problem.Initialise();
HeartConfig::Instance()->SetSurfaceAreaToVolumeRatio(1.0);
HeartConfig::Instance()->SetCapacitance(1.0);
std::vector<unsigned> pinned_nodes;
// check throws if the fixed node num isn't valid
pinned_nodes.push_back(1000);
bidomain_problem.SetFixedExtracellularPotentialNodes(pinned_nodes);
TS_ASSERT_THROWS_THIS( bidomain_problem.Solve(), "Fixed node number must be less than total number nodes" );
/* HOW_TO_TAG Cardiac/Problem definition
* Fix phi_e at particular nodes (note: this is not required)
*/
// Pin extracellular potential of node 100 to 0
pinned_nodes.clear();
pinned_nodes.push_back(100);
bidomain_problem.SetFixedExtracellularPotentialNodes(pinned_nodes);
try
{
bidomain_problem.Solve();
}
catch (const Exception& e)
{
TS_FAIL(e.GetMessage());
}
DistributedVector striped_voltage = bidomain_problem.GetSolutionDistributedVector();
DistributedVector::Stripe voltage(striped_voltage,0);
for (DistributedVector::Iterator index = striped_voltage.Begin();
index != striped_voltage.End();
++index)
{
// assuming LR model has Ena = 54.4 and Ek = -77
double Ena = 54.4; // mV
double Ek = -77.0; // mV
TS_ASSERT_LESS_THAN_EQUALS( voltage[index], Ena + 30);
TS_ASSERT_LESS_THAN_EQUALS(-voltage[index] + (Ek-30), 0);
std::vector<double> r_ode_vars = bidomain_problem.GetBidomainTissue()->GetCardiacCell(index.Global)->GetStdVecStateVariables();
for (int j=0; j<8; j++)
{
// if not voltage or calcium ion conc, test whether between 0 and 1
if ((j!=0) && (j!=7))
{
TS_ASSERT_LESS_THAN_EQUALS( r_ode_vars[j], 1.0);
TS_ASSERT_LESS_THAN_EQUALS(-r_ode_vars[j], 0.0);
}
}
// wave shouldn't have reached the second half of the mesh so
// these should all be near the resting potential
if (index.Global>50)
{
TS_ASSERT_DELTA(voltage[index], -83.85, 0.1);
}
// final voltages for nodes 0 to 5 produced with ksp_rtol=1e-9
double test_values[6]={31.0335, 28.9214, 20.0279, -3.92649, -57.9395, -79.7754};
for (unsigned node=0; node<=5; node++)
{
if (index.Global == node)
{
// test against hardcoded value to check nothing has changed
TS_ASSERT_DELTA(voltage[index], test_values[node], 7e-3);
//With ksp_rtol set to 1e-6 the starting value may lead to changes of more that 1e-3 in final answer
}
}
}
DistributedVector::Stripe extracellular_potential(striped_voltage,1);
if (striped_voltage.IsGlobalIndexLocal(100))
{
TS_ASSERT_DELTA(extracellular_potential[100], 0.0, 1e-6);
}
}
// NOTE: This test uses NON-PHYSIOLOGICAL parameters values (conductivities,
// surface-area-to-volume ratio, capacitance, stimulus amplitude). Essentially,
// the equations have been divided through by the surface-area-to-volume ratio.
// (Historical reasons...)
void TestBidomainDg01DAveragePhiEOverDifferentRows()
{
HeartEventHandler::Disable();
HeartConfig::Instance()->SetIntracellularConductivities(Create_c_vector(0.0005));
HeartConfig::Instance()->SetExtracellularConductivities(Create_c_vector(0.0005));
HeartConfig::Instance()->SetSimulationDuration(1.0); //ms
HeartConfig::Instance()->SetMeshFileName("mesh/test/data/1D_0_to_1_100_elements");
HeartConfig::Instance()->SetOutputDirectory("bidomainDg01d");
HeartConfig::Instance()->SetOutputFilenamePrefix("BidomainLR91_1d");
// Final values to test against have been produced with ksp_rtol=1e-9
HeartConfig::Instance()->SetUseRelativeTolerance(1e-8);
PlaneStimulusCellFactory<CellLuoRudy1991FromCellML, 1> bidomain_cell_factory;
BidomainProblem<1> bidomain_problem( &bidomain_cell_factory );
bidomain_problem.Initialise();
HeartConfig::Instance()->SetSurfaceAreaToVolumeRatio(1.0);
HeartConfig::Instance()->SetCapacitance(1.0);
// Apply the constraint 'Average phi = 0' to nodes 0, 50, 100.
for (unsigned node=0; node<bidomain_problem.rGetMesh().GetNumNodes(); node+=50)
{
bidomain_problem.SetNodeForAverageOfPhiZeroed(node);
try
{
bidomain_problem.Solve();
}
catch (const Exception& e)
{
TS_FAIL(e.GetMessage());
}
DistributedVector striped_voltage = bidomain_problem.GetSolutionDistributedVector();
DistributedVector::Stripe voltage(striped_voltage,0);
DistributedVector::Stripe phi_e(striped_voltage,1);
for (DistributedVector::Iterator index = striped_voltage.Begin();
index != striped_voltage.End();
++index)
{
// assuming LR model has Ena = 54.4 and Ek = -77
double Ena = 54.4; // mV
double Ek = -77.0; // mV
TS_ASSERT_LESS_THAN_EQUALS( voltage[index], Ena + 30);
TS_ASSERT_LESS_THAN_EQUALS(-voltage[index] + (Ek-30), 0);
std::vector<double> r_ode_vars = bidomain_problem.GetBidomainTissue()->GetCardiacCell(index.Global)->GetStdVecStateVariables();
for (int j=0; j<8; j++)
{
// if not voltage or calcium ion conc, test whether between 0 and 1
if ((j!=0) && (j!=7))
{
TS_ASSERT_LESS_THAN_EQUALS( r_ode_vars[j], 1.0);
TS_ASSERT_LESS_THAN_EQUALS(-r_ode_vars[j], 0.0);
}
}
// wave shouldn't have reached the second half of the mesh so
// these should all be near the resting potential
if (index.Global>50)
{
TS_ASSERT_DELTA(voltage[index], -83.85, 0.1);
}
// final voltages for nodes 0 to 5 produced with ksp_rtol=1e-9
double voltage_test_values[6]={31.0335, 28.9214, 20.0279, -3.92649, -57.9395, -79.7754};
if (index.Global<6)
{
// test against hardcoded value to check nothing has changed
TS_ASSERT_DELTA(voltage[index], voltage_test_values[index.Global], 7e-3);
}
// final extracellular potencials for nodes 0 to 5 produced with ksp_rtol=1e-9
double phi_e_test_values[6]={-55.2567, -54.2006, -49.7538, -37.7767, -10.7701, 0.148278};
if (index.Global<6)
{
// test against hardcoded value to check nothing has changed
TS_ASSERT_DELTA(phi_e[index], phi_e_test_values[index.Global], 7e-3);
}
}
// check mean of extracellular potential is 0
double local_phi_e=0.0;
double total_phi_e=0.0;
for (DistributedVector::Iterator index = striped_voltage.Begin();
index != striped_voltage.End();
++index)
{
local_phi_e += phi_e[index];
}
int ierr = MPI_Allreduce(&local_phi_e, &total_phi_e, 1, MPI_DOUBLE, MPI_SUM, PETSC_COMM_WORLD);
TS_ASSERT_EQUALS(ierr, (int)MPI_SUCCESS);
TS_ASSERT_DELTA(total_phi_e, 0, 1e-4);
bidomain_problem.Initialise();
}
// Coverage of the exception in the solver itself
BoundaryConditionsContainer<1,1,2> container;
BidomainSolver<1,1> bidomain_solver(false,
&bidomain_problem.rGetMesh(),
bidomain_problem.GetBidomainTissue(),
&container);
TS_ASSERT_THROWS_THIS(bidomain_solver.SetRowForAverageOfPhiZeroed(0),
"Row for applying the constraint \'Average of phi_e = zero\' should be odd in C++ like indexing");
HeartEventHandler::Enable();
}
/*
* The monodomain equations are obtained by taking the limit of the bidomain
* equations as sigma_e tends to infinity (corresponding to the extracellular
* space being grounded). Therefore, if we set sigma_e very large (relative to
* sigma_i) in a bidomain simulation it should agree with a monodomain
* simulation with the same parameters.
*
* NOTE: This test uses NON-PHYSIOLOGICAL parameters values (conductivities,
* surface-area-to-volume ratio, capacitance, stimulus amplitude). Essentially,
* the equations have been divided through by the surface-area-to-volume ratio.
* (Historical reasons...)
*
*/
void TestCompareBidomainProblemWithMonodomain()
{
HeartConfig::Instance()->SetIntracellularConductivities(Create_c_vector(0.0005));
HeartConfig::Instance()->SetSimulationDuration(1.0); //ms
HeartConfig::Instance()->SetMeshFileName("mesh/test/data/1D_0_to_1_100_elements");
HeartConfig::Instance()->SetOutputDirectory("Monodomain1dVersusBidomain");
HeartConfig::Instance()->SetOutputFilenamePrefix("monodomain1d");
Vec monodomain_results;
PlaneStimulusCellFactory<CellLuoRudy1991FromCellML, 1> cell_factory;
// To avoid an issue with the Event handler only one simulation should be
// in existance at a time: therefore monodomain simulation is defined in a block
{
///////////////////////////////////////////////////////////////////
// monodomain
///////////////////////////////////////////////////////////////////
MonodomainProblem<1> monodomain_problem( &cell_factory );
monodomain_problem.Initialise();
HeartConfig::Instance()->SetSurfaceAreaToVolumeRatio(1.0);
HeartConfig::Instance()->SetCapacitance(1.0);
// now solve
monodomain_problem.Solve();
VecDuplicate(monodomain_problem.GetSolution(), &monodomain_results);
VecCopy(monodomain_problem.GetSolution(), monodomain_results);
}
///////////////////////////////////////////////////////////////////
// bidomain
///////////////////////////////////////////////////////////////////
// keep the intra conductivity to be the same as monodomain
// and the extra conductivity to be very large in comparison
HeartConfig::Instance()->SetExtracellularConductivities(Create_c_vector(1));
HeartConfig::Instance()->SetOutputDirectory("Bidomain1d");
HeartConfig::Instance()->SetOutputFilenamePrefix("bidomain1d");
BidomainProblem<1> bidomain_problem( &cell_factory );
// Not really needed for coverage, but it's worth checking that logic works properly in both scenarios.
TS_ASSERT(!bidomain_problem.GetHasBath());
bidomain_problem.Initialise();
HeartConfig::Instance()->SetSurfaceAreaToVolumeRatio(1.0);
HeartConfig::Instance()->SetCapacitance(1.0);
// now solve
bidomain_problem.Solve();
///////////////////////////////////////////////////////////////////
// compare
///////////////////////////////////////////////////////////////////
DistributedVector dist_bidomain_voltage = bidomain_problem.GetSolutionDistributedVector();
DistributedVector monodomain_voltage = dist_bidomain_voltage.GetFactory()->CreateDistributedVector(monodomain_results);
DistributedVector::Stripe bidomain_voltage(dist_bidomain_voltage, 0);
DistributedVector::Stripe extracellular_potential(dist_bidomain_voltage, 1);
for (DistributedVector::Iterator index = monodomain_voltage.Begin();
index != monodomain_voltage.End();
++index)
{
if (index.Global==0)
{
TS_ASSERT_LESS_THAN(0, monodomain_voltage[index]);
}
// the mono and bidomains should agree closely
TS_ASSERT_DELTA(monodomain_voltage[index], bidomain_voltage[index], 0.4);
// the extracellular potential should be uniform
TS_ASSERT_DELTA(extracellular_potential[index], 0, 0.06);
}
PetscTools::Destroy(monodomain_results);
}
///////////////////////////////////////////////////////////////////
// Solve a simple simulation and check the output was only
// printed out at the correct times
///////////////////////////////////////////////////////////////////
void TestBidomainProblemPrintsOnlyAtRequestedTimesAndOnlyRequestedNodes()
{
HeartEventHandler::Disable();
HeartConfig::Instance()->SetPrintingTimeStep(0.1);
HeartConfig::Instance()->SetPdeTimeStep(0.01);
HeartConfig::Instance()->SetSimulationDuration(0.3); //ms
HeartConfig::Instance()->SetMeshFileName("mesh/test/data/1D_0_to_1mm_10_elements");
HeartConfig::Instance()->SetOutputDirectory("Bidomain1d");
HeartConfig::Instance()->SetOutputFilenamePrefix("bidomain_testPrintTimes");
// run testing PrintingTimeSteps
PlaneStimulusCellFactory<CellLuoRudy1991FromCellML, 1> cell_factory;
BidomainProblem<1>* p_bidomain_problem = new BidomainProblem<1>( &cell_factory );
/* HOW_TO_TAG Cardiac/Output
* Only output data for particular nodes
*/
//Restrict the number of nodes
std::vector<unsigned> nodes_to_be_output;
nodes_to_be_output.push_back(0);
nodes_to_be_output.push_back(5);
nodes_to_be_output.push_back(10);
p_bidomain_problem->SetOutputNodes(nodes_to_be_output);
/* HOW_TO_TAG Cardiac/Output
* Output data using a light-weight output modifier. This can be used in addition to regular HDF5
* output or can replace it.
*/
// Duplicate this with a single trace at node 5
boost::shared_ptr<SingleTraceOutputModifier> trace_5(new SingleTraceOutputModifier("trace_5.txt", 5, 0.1));
p_bidomain_problem->AddOutputModifier(trace_5);
// for coverage:
p_bidomain_problem->SetWriteInfo();
p_bidomain_problem->Initialise();
p_bidomain_problem->Solve();
// Extra trace data
OutputFileHandler handler("Bidomain1d", false);
NumericFileComparison comp(handler.GetOutputDirectoryFullPath()+ "trace_5.txt", "heart/test/data/Bidomain1d/trace_5.txt");
TS_ASSERT(comp.CompareFiles(5e-4));
// read data entries for the time file and check correct
Hdf5DataReader data_reader1=p_bidomain_problem->GetDataReader();
std::vector<double> times = data_reader1.GetUnlimitedDimensionValues();
TS_ASSERT_EQUALS( times.size(), (unsigned) 4);
TS_ASSERT_DELTA( times[0], 0.00, 1e-12);
TS_ASSERT_DELTA( times[1], 0.10, 1e-12);
TS_ASSERT_DELTA( times[2], 0.20, 1e-12);
TS_ASSERT_DELTA( times[3], 0.30, 1e-12);
//Get back node over all times
std::vector<double> node_0 = data_reader1.GetVariableOverTime("V", 0);
TS_ASSERT_EQUALS( node_0.size(), 4U);
TS_ASSERT_DELTA( node_0[0], -83.853, 1e-10);
TS_ASSERT_DELTA( node_0[1], -83.8354, 5e-4);
TS_ASSERT_DELTA( node_0[2], -83.8266, 3e-4);
TS_ASSERT_DELTA( node_0[3], -83.8200, 3e-4);
std::vector<double> node_5 = data_reader1.GetVariableOverTime("V", 5);
TS_ASSERT_EQUALS( node_5.size(), 4U);
std::vector<double> node_10 = data_reader1.GetVariableOverTime("V", 10);
TS_ASSERT_EQUALS( node_10.size(), 4U);
//Can't read back this node as it wasn't written
TS_ASSERT_THROWS_THIS( data_reader1.GetVariableOverTime("V", 1),
"The incomplete dataset 'Data' does not contain info of node 1");
delete p_bidomain_problem;
p_bidomain_problem = new BidomainProblem<1>( &cell_factory );
// Now check that we can turn off output printing
// Output should be the same as above: printing every 10th time step
// because even though we set to print every time step...
HeartConfig::Instance()->SetPrintingTimeStep(1);
// ...we have output turned off
p_bidomain_problem->PrintOutput(false);
p_bidomain_problem->Initialise();
p_bidomain_problem->Solve();
Hdf5DataReader data_reader3=p_bidomain_problem->GetDataReader();
times = data_reader3.GetUnlimitedDimensionValues();
TS_ASSERT_EQUALS( times.size(), (unsigned) 4);
TS_ASSERT_DELTA( times[0], 0.00, 1e-12);
TS_ASSERT_DELTA( times[1], 0.10, 1e-12);
TS_ASSERT_DELTA( times[2], 0.20, 1e-12);
TS_ASSERT_DELTA( times[3], 0.30, 1e-12);
delete p_bidomain_problem;
HeartEventHandler::Enable();
}
void TestBidomainFallsOverProducesOutput()
{
#ifndef NDEBUG //Note that this test relies on the debug VerifyStateVariables() method throwing
HeartConfig::Instance()->SetSimulationDuration(0.3); //ms
HeartConfig::Instance()->SetMeshFileName("mesh/test/data/1D_0_to_1_100_elements");
HeartConfig::Instance()->SetOutputDirectory("BidomainFallsOver");
HeartConfig::Instance()->SetOutputFilenamePrefix("res");
HeartConfig::Instance()->SetVisualizeWithMeshalyzer();
// Something happens at 0.1ms
// DelayedTotalStimCellFactory bidomain_cell_factory(-6e5); //Normal stimulus
DelayedTotalStimCellFactory bidomain_cell_factory(-6e6); //Takes sodium out of range
BidomainProblem<1> bidomain_problem( &bidomain_cell_factory );
bidomain_problem.Initialise();
TS_ASSERT_THROWS_CONTAINS(bidomain_problem.Solve(),
"State variable fast_sodium_current_m_gate__m has gone out of range. Check numerical parameters, for example time and space stepsizes");
// Make sure that there's time for the files to be written
// (most files are only written by the master)
PetscTools::Barrier();
//Test for regular output
Hdf5DataReader data_reader=bidomain_problem.GetDataReader();
std::vector<double> times = data_reader.GetUnlimitedDimensionValues();
//TS_ASSERT_EQUALS( times.size(), 31U);// For normal stimulation
TS_ASSERT_EQUALS( times.size(), 31U); // For fully allocated hdf5
//TS_ASSERT_EQUALS( times.size(), 21U);// For over stimulation
TS_ASSERT_DELTA( times[1], 0.01, 1e-12);
//TS_ASSERT_DELTA( times.back(), 0.30, 1e-12);//For normal stimulation
//TS_ASSERT_DELTA( times.back(), 0.20, 1e-12);//For over stimulation
TS_ASSERT_DELTA( times.back(), 0, 1e-12);//For hdf5 writer allocated up to 30 slots
TS_ASSERT_DELTA( times[20], 0.20, 1e-12);//For hdf5 writer allocated up to 30 slots
TS_ASSERT_DELTA( times[21], 0, 1e-12);//For hdf5 writer allocated up to 30 slots
// Test for post-processed output (and don't wipe the directory!)
OutputFileHandler handler("BidomainFallsOver/output", false);
std::string files[6] = {"res_mesh.pts","res_mesh.cnnx","ChasteParameters.xml",
"res_Phi_e.dat","res_V.dat","res_times.info"};
for (unsigned i=0; i<6; i++)
{
TS_ASSERT(handler.FindFile(files[i]).Exists());
}
#endif //NDEBUG Note that this test relies on the debug VerifyStateVariables() method throwing
}
void TestBidomainProblemExceptions()
{
PlaneStimulusCellFactory<CellLuoRudy1991FromCellML, 1> cell_factory;
BidomainProblem<1> bidomain_problem( &cell_factory );
// Throws because we've not called initialise
TS_ASSERT_THROWS_THIS(bidomain_problem.Solve(), "Cardiac tissue is null, Initialise() probably hasn\'t been called");
// Throws because mesh filename is unset
TS_ASSERT_THROWS_CONTAINS(bidomain_problem.Initialise(),
"No mesh given: define it in XML parameters file or call SetMesh()\n"
"No XML element Simulation/Mesh found in parameters when calling");
HeartConfig::Instance()->SetMeshFileName("mesh/test/data/1D_0_to_1mm_10_elements");
TS_ASSERT_THROWS_NOTHING(bidomain_problem.Initialise());
// Negative simulation duration
HeartConfig::Instance()->SetSimulationDuration(-1.0); //ms
TS_ASSERT_THROWS_THIS(bidomain_problem.Solve(), "End time should be in the future");
HeartConfig::Instance()->SetSimulationDuration(1.0); //ms
// set output data to avoid their exceptions (which is covered in TestMonoDg0Assembler
HeartConfig::Instance()->SetOutputDirectory("temp");
HeartConfig::Instance()->SetOutputFilenamePrefix("temp");
// Exception caused by relative tolerance and no clamping
HeartConfig::Instance()->SetUseRelativeTolerance(2e-3);
TS_ASSERT_THROWS_THIS(bidomain_problem.Solve(), "Bidomain external voltage is not bounded in this simulation - use KSP *absolute* tolerance");
HeartConfig::Instance()->SetUseAbsoluteTolerance(2e-3);
// Throws (in AbstractCardiacProblem) as dt does not divide end time
HeartConfig::Instance()->SetPrintingTimeStep(0.15);
HeartConfig::Instance()->SetPdeTimeStep(0.15);
TS_ASSERT_THROWS_THIS( bidomain_problem.Solve(),"PDE timestep does not seem to divide end time - check parameters" );
HeartConfig::Instance()->SetPdeTimeStep(0.01);
HeartConfig::Instance()->SetPrintingTimeStep(0.01);
//Throws because the node number is slightly bigger than the number of nodes in the mesh
std::vector<unsigned> too_large;
too_large.push_back(4358743);
bidomain_problem.SetFixedExtracellularPotentialNodes(too_large);
TS_ASSERT_THROWS_THIS(bidomain_problem.Solve(), "Fixed node number must be less than total number nodes");
//Explicitly reset the counters so the next test in the test suite doesn't find on
HeartEventHandler::Reset();
}
void TestCompareOrthotropicWithAxisymmetricBidomain()
{
HeartConfig::Instance()->SetSimulationDuration(1.0); //ms
HeartConfig::Instance()->SetMeshFileName("heart/test/data/box_shaped_heart/box_heart", cp::media_type::Orthotropic);
HeartConfig::Instance()->SetOutputDirectory("OrthotropicBidomain");
HeartConfig::Instance()->SetOutputFilenamePrefix("ortho3d");
PlaneStimulusCellFactory<CellLuoRudy1991FromCellML, 3> cell_factory;
///////////////////////////////////////////////////////////////////
// orthotropic
///////////////////////////////////////////////////////////////////
BidomainProblem<3> orthotropic_bido( &cell_factory );
orthotropic_bido.Initialise();
orthotropic_bido.Solve();
///////////////////////////////////////////////////////////////////
// axisymmetric
///////////////////////////////////////////////////////////////////
HeartConfig::Instance()->SetMeshFileName("heart/test/data/box_shaped_heart/box_heart", cp::media_type::Axisymmetric);
HeartConfig::Instance()->SetOutputDirectory("AxisymmetricBidomain");
HeartConfig::Instance()->SetOutputFilenamePrefix("axi3d");
BidomainProblem<3> axisymmetric_bido( &cell_factory);
HeartConfig::Instance()->SetVisualizeWithCmgui(true);
HeartConfig::Instance()->SetVisualizeWithVtk(true);
axisymmetric_bido.Initialise();
axisymmetric_bido.Solve();
///////////////////////////////////////////////////////////////////
// compare
///////////////////////////////////////////////////////////////////
DistributedVector orthotropic_solution = orthotropic_bido.GetSolutionDistributedVector();
DistributedVector axisymmetric_solution = axisymmetric_bido.GetSolutionDistributedVector();
DistributedVector::Stripe ortho_voltage(orthotropic_solution, 0);
DistributedVector::Stripe axi_voltage(axisymmetric_solution, 0);
DistributedVector::Stripe ortho_ex_pot(orthotropic_solution, 1);
DistributedVector::Stripe axi_ex_pot(axisymmetric_solution, 1);
for (DistributedVector::Iterator index = orthotropic_solution.Begin();
index != orthotropic_solution.End();
++index)
{
TS_ASSERT_DELTA(ortho_voltage[index], axi_voltage[index], 1e-7);
TS_ASSERT_DELTA(ortho_ex_pot[index], axi_ex_pot[index], 1e-7);
}
// We check that the cmgui files generated by the convert command in the problem class are OK
// We compare against mesh files and one data file that are known to be visualized correctly in Cmgui.
// The files written in parallel are different from the ones written in sequential because of the different node
// renumbering, therefore we test only the sequential case.
// Note that the outputs of sequential and parallel simulations look the same when loaded with cmgui.
// There are also minor rounding differences at the last decimal figure between sequential and parallel.
EXIT_IF_PARALLEL
std::string results_dir = OutputFileHandler::GetChasteTestOutputDirectory() + "AxisymmetricBidomain/cmgui_output/";
//the mesh files...
std::string node_file1 = results_dir + "/axi3d.exnode";
std::string node_file2 = "heart/test/data/CmguiData/bidomain/bidomain3dValid.exnode";
std::string elem_file1 = results_dir + "/axi3d.exelem";
std::string elem_file2 = "heart/test/data/CmguiData/bidomain/bidomain3dValid.exelem";
FileComparison comparer(node_file1,node_file2);
TS_ASSERT(comparer.CompareFiles());
FileComparison comparer2(elem_file1,elem_file2);
TS_ASSERT(comparer2.CompareFiles());
//...and a couple of data files as examples
FileComparison comparer3(results_dir + "/axi3d_61.exnode",
"heart/test/data/CmguiData/bidomain/bidomain3dValidData61.exnode");
TS_ASSERT(comparer3.CompareFiles());
FileComparison comparer4(results_dir + "/axi3d_100.exnode",
"heart/test/data/CmguiData/bidomain/bidomain3dValidData100.exnode");
TS_ASSERT(comparer4.CompareFiles());
//info file
FileComparison comparer5(results_dir + "/axi3d_times.info",
"heart/test/data/CmguiData/bidomain/axi3d_times.info");
TS_ASSERT(comparer5.CompareFiles());
//HeartConfig XML
TS_ASSERT(FileFinder(results_dir + "ChasteParameters.xml").Exists());
#ifdef CHASTE_VTK
// Requires "sudo aptitude install libvtk5-dev" or similar
results_dir = OutputFileHandler::GetChasteTestOutputDirectory() + "AxisymmetricBidomain/vtk_output/";
VtkMeshReader<3,3> mesh_reader(results_dir + "axi3d.vtu");
TS_ASSERT_EQUALS( mesh_reader.GetNumNodes(), 50U);
TS_ASSERT_EQUALS( mesh_reader.GetNumElements(), 139U);
std::vector<double> first_node = mesh_reader.GetNextNode();
TS_ASSERT_DELTA( first_node[0] , 0.0 , 1e-6 );
TS_ASSERT_DELTA( first_node[1] , 0.0, 1e-6 );
TS_ASSERT_DELTA( first_node[2] , 0.0 , 1e-6 );
std::vector<double> next_node = mesh_reader.GetNextNode();
TS_ASSERT_DELTA( next_node[0] , 0.0 , 1e-6 );
TS_ASSERT_DELTA( next_node[1] , 5.0 , 1e-6 );
TS_ASSERT_DELTA( next_node[2] , 0.0 , 1e-6 );
//V_m and phi_e samples
std::vector<double> v_at_last, v_at_50, phi_at_50;
mesh_reader.GetPointData( "V_000050", v_at_50);
TS_ASSERT_DELTA( v_at_50[0], -83.6547, 1e-3 );
TS_ASSERT_DELTA( v_at_50[25], -83.8549, 1e-3 );
TS_ASSERT_DELTA( v_at_50[49], -83.8557, 1e-3 );
mesh_reader.GetPointData( "Phi_e_000050", phi_at_50);
TS_ASSERT_DELTA( phi_at_50[0], -0.0332, 1e-3 );
TS_ASSERT_DELTA( phi_at_50[25], 0.0064, 1e-3 );
TS_ASSERT_DELTA( phi_at_50[49], 0.0062, 1e-3 );
mesh_reader.GetPointData( "V_000100", v_at_last);
TS_ASSERT_DELTA( v_at_last[0], -83.6863, 1e-3 );
//HeartConfig XML
TS_ASSERT(FileFinder(results_dir + "ChasteParameters.xml").Exists());
#else
std::cout << "This test ran, but did not test VTK-dependent functions as VTK visualization is not enabled." << std::endl;
std::cout << "If required please install and alter your hostconfig settings to switch on chaste support." << std::endl;
#endif //CHASTE_VTK
}
// Test the functionality for outputting the values of requested cell state variables
void TestBidomainProblemPrintsMultipleVariables()
{
// Get the singleton in a clean state
HeartConfig::Instance()->Reset();
// Set configuration file
HeartConfig::Instance()->SetParametersFile("heart/test/data/xml/MultipleVariablesBidomain.xml");
// Override the variables we are interested in writing.
std::vector<std::string> output_variables;
output_variables.push_back("calcium_dynamics__Ca_NSR");
output_variables.push_back("ionic_concentrations__Nai");
output_variables.push_back("fast_sodium_current_j_gate__j");
output_variables.push_back("ionic_concentrations__Ki");
HeartConfig::Instance()->SetOutputVariables( output_variables );
// Set up problem
PlaneStimulusCellFactory<CellFaberRudy2000FromCellML, 1> cell_factory;
BidomainProblem<1> bidomain_problem( &cell_factory );
// Solve
bidomain_problem.Initialise();
bidomain_problem.Solve();
// Get a reference to a reader object for the simulation results
Hdf5DataReader data_reader1 = bidomain_problem.GetDataReader();
std::vector<double> times = data_reader1.GetUnlimitedDimensionValues();
// Check there is information about 11 timesteps (0, 0.01, 0.02, ...)
unsigned num_steps = 11u;
TS_ASSERT_EQUALS( times.size(), num_steps);
TS_ASSERT_DELTA( times[0], 0.0, 1e-12);
TS_ASSERT_DELTA( times[1], 0.01, 1e-12);
TS_ASSERT_DELTA( times[2], 0.02, 1e-12);
TS_ASSERT_DELTA( times[3], 0.03, 1e-12);
// There should be 11 values per variable and node.
std::vector<double> node_5_v = data_reader1.GetVariableOverTime("V", 5);
TS_ASSERT_EQUALS( node_5_v.size(), num_steps);
std::vector<double> node_5_phi = data_reader1.GetVariableOverTime("Phi_e", 5);
TS_ASSERT_EQUALS( node_5_phi.size(), num_steps);
for (unsigned i=0; i<output_variables.size(); i++)
{
unsigned global_index = 2+i*2;
std::vector<double> values = data_reader1.GetVariableOverTime(output_variables[i], global_index);
TS_ASSERT_EQUALS( values.size(), num_steps);
// Check the last values match the cells' state
if (bidomain_problem.rGetMesh().GetDistributedVectorFactory()->IsGlobalIndexLocal(global_index))
{
AbstractCardiacCellInterface* p_cell = bidomain_problem.GetTissue()->GetCardiacCell(global_index);
TS_ASSERT_DELTA(values.back(), p_cell->GetAnyVariable(output_variables[i],0), 1e-12);
}
}
}
/* HOW_TO_TAG Cardiac/Output
* Output all cell model state variables for the cell model used in a particular simulation
*/
void TestBidomainProblemPrintsAllStateVariables()
{
// Get the singleton in a clean state
HeartConfig::Instance()->Reset();
// Set configuration file
HeartConfig::Instance()->SetParametersFile("heart/test/data/xml/MultipleVariablesBidomain.xml");
//Override the output directory so that this test goes into a fresh place
HeartConfig::Instance()->SetOutputDirectory("AllStateVariablesBidomain");
{
//MultipleVariablesBidomain.xml uses FaberRudy. Interrogate a model to get all the state variables
boost::shared_ptr<ZeroStimulus> p_stimulus(new ZeroStimulus());
boost::shared_ptr<EulerIvpOdeSolver> p_solver(new EulerIvpOdeSolver);
CellFaberRudy2000FromCellML temporary_fr2000_ode_system(p_solver, p_stimulus);
TS_ASSERT_EQUALS(temporary_fr2000_ode_system.rGetStateVariableNames().size(), 25u);
// Copy all the state variable names from the temporary cell into HeartConfig so that they are all printed
HeartConfig::Instance()->SetOutputVariables( temporary_fr2000_ode_system.rGetStateVariableNames() );
}
// Set up problem
PlaneStimulusCellFactory<CellFaberRudy2000FromCellML, 1> cell_factory;
BidomainProblem<1> bidomain_problem( &cell_factory );
// Solve
bidomain_problem.Initialise();
bidomain_problem.Solve();
// Get a reference to a reader object for the simulation results
Hdf5DataReader data_reader1 = bidomain_problem.GetDataReader();
// Get an obscure state variable
std::vector<double> obscure_at_5 = data_reader1.GetVariableOverTime("slow_delayed_rectifier_potassium_current_xs2_gate__xs2", 5);
TS_ASSERT_EQUALS( obscure_at_5.size(), 11u);
for (unsigned time_step=0; time_step<11; time_step++)
{
TS_ASSERT_DELTA( obscure_at_5[time_step], 0.0044, 1e-3); //Does not change over time at this node
}
}
/*
* Simple bidomain simulation to test against in the archiving tests below
*