20 #include <gsl/gsl_linalg.h>
23 #include "../basecode/SparseMatrix.h"
25 #include "../shell/Shell.h"
30 const double* m,
unsigned int numCompts,
31 const double* ans,
const double* rhs )
33 vector< double > check( numCompts, 0.0 );
34 for (
unsigned int i = 0; i < numCompts; ++i ) {
35 for (
unsigned int j = 0; j < numCompts; ++j )
36 check[i] += m[i*numCompts + j] * ans[j];
39 for (
unsigned int i = 0; i < numCompts; ++i )
40 ret += (check[i] - rhs[i]) * (check[i] - rhs[i] );
131 static double test[] = {
132 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
133 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0,
134 0, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0,
135 0, 0, 9, 10, 11, 0, 0, 0, 0, 0, 0, 0,
136 0, 0, 0, 12, 13, 14, 0, 0, 0, 0, 0, 0,
137 0, 0, 0, 0, 15, 16, 17, 0, 0, 0, 0, 0,
138 0, 0, 0, 0, 0, 18, 19, 20, 0, 0, 0, 0,
139 0, 0, 0, 0, 0, 0, 21, 22, 23, 0, 0, 0,
140 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 0, 0,
141 0, 0, 0, 0, 0, 0, 0, 0, 27, 28, 29, 0,
142 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 31, 32,
143 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 34,
145 const unsigned int numCompts = 12;
146 static unsigned int parents[] = { 1,2,3,4,5,6,7,8,9,10,11, unsigned(-1)};
182 vector< Triplet< double > > fops;
185 vector< unsigned int > parentVoxel;
186 vector< unsigned int > lookupOldRowsFromNew;
187 parentVoxel.insert( parentVoxel.begin(), &parents[0], &parents[numCompts] );
202 vector< unsigned int > diag;
203 vector< double > diagVal;
207 vector< double > y( numCompts, 1.0 );
208 vector< double > ones( numCompts, 1.0 );
217 vector< double > alle;
218 for(
unsigned int i = 0; i < numCompts; ++i ) {
219 for(
unsigned int j = 0; j < numCompts; ++j ) {
220 alle.push_back( foo.
get( i, j ) );
225 assert(
checkAns( &alle[0], numCompts, &y[0], &ones[0] ) < 1e-25 );
230 vector< double > temp( &test[0], &test[numCompts*numCompts] );
231 gsl_matrix_view m = gsl_matrix_view_array( &temp[0], numCompts, numCompts );
233 vector< double > z( numCompts, 1.0 );
234 gsl_vector_view b = gsl_vector_view_array( &z[0], numCompts );
235 gsl_vector* x = gsl_vector_alloc( numCompts );
237 gsl_permutation* p = gsl_permutation_alloc( numCompts );
238 gsl_linalg_LU_decomp( &m.matrix, p, &s );
239 gsl_linalg_LU_solve( &m.matrix, p, &b.vector, x);
240 vector< double > gslAns( numCompts );
241 for (
unsigned int i = 0; i < numCompts; ++i ) {
242 gslAns[i] = gsl_vector_get( x, i );
246 assert(
checkAns( test, numCompts, &gslAns[0], &ones[0] ) < 1e-25 );
247 gsl_permutation_free( p );
248 gsl_vector_free( x );
249 cout <<
"." << flush;
255 static unsigned int k[] = {20,40,60,80,100,10,30,50,70,90};
256 static double d[] = {1,2,3,4,5,6,7,8,9,10};
257 vector< unsigned int > col;
258 col.insert( col.begin(), k, k+10);
259 vector< double > entry;
260 entry.insert( entry.begin(), d, d+10);
263 for (
unsigned int i = 0; i < col.size(); ++i )
264 assert( col[i] == (i + 1) * 10 );
266 assert( entry[0] == 6 );
267 assert( entry[1] == 1 );
268 assert( entry[2] == 7 );
269 assert( entry[3] == 2 );
270 assert( entry[4] == 8 );
271 assert( entry[5] == 3 );
272 assert( entry[6] == 9 );
273 assert( entry[7] == 4 );
274 assert( entry[8] == 10 );
275 assert( entry[9] == 5 );
285 cout <<
"." << flush;
290 static double test[] = {
298 const unsigned int numCompts = 6;
301 vector< unsigned int > parentVoxel( numCompts );
319 for(
unsigned int i =0; i < numCompts; ++i ) {
320 unsigned int start = 0;
323 for(
unsigned int j = start ; j < i+1 && j < numCompts ; ++j ) {
326 else if ( i + 1 == j ) {
328 }
else if ( i == j ) {
331 else if ( i == numCompts - 1 )
338 cout <<
"." << flush;
347 double diffLength = 1e-6;
348 double runtime = 10.0;
350 double diffConst = 1.0e-12;
352 Id cyl = s->
doCreate(
"CylMesh", model,
"cyl", 1 );
359 assert( ndc == static_cast< unsigned int >( round( len / diffLength )));
360 Id pool = s->
doCreate(
"Pool", cyl,
"pool", 1 );
363 Id dsolve = s->
doCreate(
"Dsolve", model,
"dsolve", 1 );
366 s->
doUseClock(
"/model/dsolve",
"process", 1 );
370 vector< double > poolVec;
373 assert( poolVec.size() == ndc );
374 assert(
doubleEq( poolVec[0], 1.0 ) );
375 assert(
doubleEq( poolVec[1], 0.0 ) );
377 vector< double > nvec =
380 assert( nvec.size() == ndc );
388 assert( nvec.size() == poolVec.size() );
389 for (
unsigned int i = 0; i < nvec.size(); ++i )
390 assert(
doubleEq( nvec[i], poolVec[i] ) );
398 double dx = diffLength;
400 double analyticTot = 0.0;
402 for (
unsigned int i = 0; i < nvec.size(); ++i ) {
403 double x = i * dx + dx * 0.5;
406 ( 1.0 / sqrt(
PI * diffConst * runtime ) ) *
407 exp( -x * x / ( 4 * diffConst * runtime ) );
408 err += ( y - nvec[i] ) * ( y - nvec[i] );
415 assert( err < 1.0e-5 );
419 cout <<
"." << flush;
428 double diffLength = 1e-6;
429 double runtime = 10.0;
431 double diffConst = 1.0e-12;
435 Id cyl = s->
doCreate(
"CylMesh", model,
"cyl", 1 );
442 assert( ndc == static_cast< unsigned int >( round( len / diffLength )));
443 Id pool = s->
doCreate(
"Pool", cyl,
"pool", 1 );
446 Id dsolve = s->
doCreate(
"Dsolve", model,
"dsolve", 1 );
448 s->
doUseClock(
"/model/dsolve",
"process", 1 );
460 vector< double > poolVec;
462 for (
unsigned int i = 0; i < poolVec.size(); ++i ) {
468 cout <<
"." << flush;
501 string name,
double len,
double dia,
double theta );
505 double diffLength = 10e-6;
507 double runtime = 100.0;
508 double diffConst = 1.0e-12;
511 Id dend =
makeCompt( soma, model,
"dend", len, 3e-6, 0 );
512 Id branch1 =
makeCompt( dend, model,
"branch1", len, 2e-6, 45.0 );
513 Id branch2 =
makeCompt( dend, model,
"branch2", len, 2e-6, -45.0 );
514 Id twig1 =
makeCompt( branch1, model,
"twig1", len, 1.5e-6, 90.0 );
515 Id twig2 =
makeCompt( branch1, model,
"twig2", len, 1.5e-6, 0.0 );
517 Id nm = s->
doCreate(
"NeuroMesh", model,
"neuromesh", 1 );
526 Id pool1 = s->
doCreate(
"Pool", nm,
"pool1", 1 );
528 Id pool2 = s->
doCreate(
"Pool", nm,
"pool2", 1 );
530 Id pool3 = s->
doCreate(
"Pool", nm,
"pool3", 1 );
533 Id dsolve = s->
doCreate(
"Dsolve", model,
"dsolve", 1 );
535 s->
doUseClock(
"/model/dsolve",
"process", 1 );
540 vector< double > nvec =
543 assert( nvec.size() == ndc );
558 vector< double > pool1Vec;
560 assert( pool1Vec.size() == ndc );
562 vector< double > pool2Vec;
564 assert( pool2Vec.size() == ndc );
566 vector< double > pool3Vec;
568 assert( pool3Vec.size() == ndc );
572 for (
unsigned int i = 0; i < nvec.size(); ++i ) {
573 assert(
doubleEq( pool1Vec[i], nvec[i] ) );
575 myTot2 += pool2Vec[i];
576 myTot3 += pool3Vec[i];
581 assert(
doubleEq( pool3Vec[0], 3.0 ) );
592 cout <<
"." << flush;
598 string name,
double len,
double dia,
double theta );
602 double diffLength = 1e-6;
604 double runtime = 100.0;
605 double diffConst = 1.0e-12;
608 Id dend =
makeCompt( soma, model,
"dend", len, 3e-6, 0 );
609 Id branch1 =
makeCompt( dend, model,
"branch1", len, 2e-6, 45.0 );
610 Id branch2 =
makeCompt( dend, model,
"branch2", len, 2e-6, -45.0 );
611 Id twig1 =
makeCompt( branch1, model,
"twig1", len, 1.5e-6, 90.0 );
612 Id twig2 =
makeCompt( branch1, model,
"twig2", len, 1.5e-6, 0.0 );
614 Id nm = s->
doCreate(
"NeuroMesh", model,
"neuromesh", 1 );
622 assert( ndc == 210 );
623 Id pool1 = s->
doCreate(
"Pool", nm,
"pool1", 1 );
625 Id pool2 = s->
doCreate(
"Pool", nm,
"pool2", 1 );
628 Id dsolve = s->
doCreate(
"Dsolve", model,
"dsolve", 1 );
630 s->
doUseClock(
"/model/dsolve",
"process", 1 );
635 vector< double > nvec =
638 assert( nvec.size() == ndc );
648 vector< double > pool1Vec;
650 assert( pool1Vec.size() == ndc );
652 vector< double > pool2Vec;
654 assert( pool2Vec.size() == ndc );
657 for (
unsigned int i = 0; i < nvec.size(); ++i ) {
658 assert(
doubleEq( pool1Vec[i], nvec[i] ) );
660 myTot2 += pool2Vec[i];
675 cout <<
"." << flush;
684 double diffLength = 1e-6;
685 double runtime = 10.0;
688 double diffConst = 1.0e-12;
690 Id cyl = s->
doCreate(
"CylMesh", model,
"cyl", 1 );
697 assert( ndc == static_cast< unsigned int >( round( len / diffLength )));
698 Id pool1 = s->
doCreate(
"Pool", cyl,
"pool1", 1 );
699 Id pool2 = s->
doCreate(
"Pool", cyl,
"pool2", 1 );
703 Id stoich = s->
doCreate(
"Stoich", model,
"stoich", 1 );
704 Id ksolve = s->
doCreate(
"Ksolve", model,
"ksolve", 1 );
705 Id dsolve = s->
doCreate(
"Dsolve", model,
"dsolve", 1 );
713 vector< double > poolVec;
717 assert( poolVec.size() == ndc );
718 assert(
doubleEq( poolVec[0], 1.0 ) );
719 assert(
doubleEq( poolVec[1], 0.0 ) );
721 vector< double > nvec =
724 assert( nvec.size() == ndc );
727 s->
doUseClock(
"/model/dsolve",
"process", 0 );
728 s->
doUseClock(
"/model/ksolve",
"process", 1 );
737 assert( nvec.size() == poolVec.size() );
738 for (
unsigned int i = 0; i < nvec.size(); ++i )
739 assert(
doubleEq( nvec[i], poolVec[i] ) );
747 double dx = diffLength;
749 double analyticTot = 0.0;
751 for (
unsigned int i = 0; i < nvec.size(); ++i ) {
752 double x = i * dx + dx * 0.5;
755 ( 1.0 / sqrt(
PI * diffConst * runtime ) ) *
756 exp( -x * x / ( 4 * diffConst * runtime ) );
757 err += ( y - nvec[i] ) * ( y - nvec[i] );
764 assert( err < 1.0e-5 );
768 cout <<
"." << flush;
773 static double test[] = {
774 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0,
775 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
776 7, 0, 8, 9, 0, 0, 0, 0, 10, 0, 0,
777 11, 0, 12, 13, 0, 0, 0, 0, 0, 0, 14,
778 0, 0, 0, 0, 15, 16, 0, 0, 17, 18, 0,
779 0, 0, 0, 0, 19, 20, 21, 0, 22, 0, 0,
780 0, 0, 0, 0, 0, 23, 24, 25, 0, 0, 0,
781 0, 0, 0, 0, 0, 0, 26, 27, 0, 0, 0,
782 0, 0, 28, 0, 29, 30, 0, 0, 31, 0, 0,
783 0, 0, 0, 0, 32, 0, 0, 0, 0, 33, 0,
784 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 35,
786 const unsigned int numCompts = 11;
790 vector< unsigned int > parentVoxel;
791 bool ret = fe.buildTree( 0, parentVoxel );
793 assert( parentVoxel[0] == static_cast< unsigned int >( -1 ) );
794 assert( parentVoxel[1] == 0 );
795 assert( parentVoxel[2] == 0 );
796 assert( parentVoxel[3] == 0 );
797 assert( parentVoxel[8] == 2 );
798 assert( parentVoxel[10] == 3 );
799 assert( parentVoxel[5] == 4 );
800 assert( parentVoxel[9] == 4 );
801 assert( parentVoxel[6] == 5 );
802 assert( parentVoxel[7] == 6 );
804 assert( parentVoxel[10] == 2 );
834 Id dend = s->
doCreate(
"Compartment", model,
"dend", 1 );
835 Id neck = s->
doCreate(
"Compartment", model,
"spine_neck", 1 );
836 Id head = s->
doCreate(
"Compartment", model,
"spine_head", 1 );
854 Id nm = s->
doCreate(
"NeuroMesh", model,
"nm", 1 );
857 Id sm = s->
doCreate(
"SpineMesh", model,
"sm", 1 );
858 Id pm = s->
doCreate(
"PsdMesh", model,
"pm", 1 );
860 assert( !mid.
bad() );
865 vector< Id > pools( 9 );
866 static string names[] = {
"a",
"b",
"c",
"b",
"c",
"d",
"c",
"d",
"e" };
867 static Id parents[] = {nm, nm, nm, sm, sm, sm, pm, pm, pm};
868 for (
unsigned int i = 0; i < 9; ++i ) {
869 pools[i] = s->
doCreate(
"Pool", parents[i], names[i], 1 );
870 assert( pools[i] !=
Id() );
875 assert(
doubleEq( vol, 10e-6 * 1e-12 *
PI ) );
878 Id dendsolve = s->
doCreate(
"Dsolve", model,
"dendsolve", 1 );
879 Id spinesolve = s->
doCreate(
"Dsolve", model,
"spinesolve", 1 );
880 Id psdsolve = s->
doCreate(
"Dsolve", model,
"psdsolve", 1 );
894 spinesolve, psdsolve );
896 s->
doUseClock(
"/model/#solve",
"process", 0 );
911 cout <<
"." << flush;
void doStart(double runtime, bool notify=false)
T get(unsigned int row, unsigned int column) const
Id makeCompt(Id parentCompt, Id parentObj, string name, double len, double dia, double theta)
Element * element() const
Synonym for Id::operator()()
void buildBackwardSub(vector< unsigned int > &diag, vector< Triplet< double > > &bops, vector< double > &diagVal)
static void advance(vector< double > &y, const vector< Triplet< double > > &ops, const vector< double > &diagVal)
void doSetClock(unsigned int tickNum, double dt)
void testCylDiffnWithStoich()
bool hinesReorder(const vector< unsigned int > &parentVoxel, vector< unsigned int > &lookupOldRowsFromNew)
static bool set(const ObjId &dest, const string &field, A arg)
Id doCreate(string type, ObjId parent, string name, unsigned int numData, NodePolicy nodePolicy=MooseBlockBalance, unsigned int preferredNode=1)
void testTaperingCylDiffn()
void testSmallCellDiffn()
void testFastMatrixElim()
bool doubleEq(double x, double y)
virtual unsigned int numData() const =0
Returns number of data entries across all nodes.
void testSetDiffusionAndTransport()
void makeTestMatrix(const double *test, unsigned int numCompts)
ObjId doAddMsg(const string &msgType, ObjId src, const string &srcField, ObjId dest, const string &destField)
void setDiffusionAndTransport(const vector< unsigned int > &parentVoxel, double diffConst, double motorConst, double dt)
double checkAns(const double *m, unsigned int numCompts, const double *ans, const double *rhs)
void doUseClock(string path, string field, unsigned int tick)
static A get(const ObjId &dest, const string &field)
static void getVec(ObjId dest, const string &field, vector< A > &vec)
void buildForwardElim(vector< unsigned int > &diag, vector< Triplet< double > > &fops)
void sortByColumn(vector< unsigned int > &col, vector< double > &entry)
static bool set(const ObjId &dest, const string &field, A1 arg1, A2 arg2)