#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct SparseMatrix_t
{
double *a;
int *col;
int *StartRow;
int n;
struct SparseMatrix_t *l;
struct SparseMatrix_t *u;
} SparseMatrix;
typedef struct contact_t
{
int id;
double xp;
double amp;
int xl;
int xr;
double fl;
double fr;
struct contact_t *next;
} contact;
typedef struct branch_t
{
struct branch_t *parent;
struct branch_t *child;
struct branch_t *peer;
int id;
int nc;
double xl;
double yl;
double zl;
double xr;
double yr;
double zr;
double diam;
double plen;
double elen;
double hseg;
int nodes;
int junct;
int first;
contact *conlist;
} branch;
typedef struct dendrite_t
{
branch *root;
double plen;
} dendrite;
typedef struct neuron_t
{
int ndend;
dendrite *dendlist;
} neuron;
int Count_Branches( branch *, branch *),
Count_Contacts( branch *, branch *);
double branch_length( branch *, branch *),
ran(unsigned int *, unsigned int *, unsigned int *);
void Build_Test_Dendrite( branch **, branch *),
Remove_Branch( branch **, branch *),
Output_Info( branch *, FILE *),
Destroy_Test_Neuron( neuron *),
Destroy_Test_Dendrite( branch *),
Find_Contacts( branch *, double *, double *, int *),
Assign_Branch_Nodes( branch *, double *),
Enumerate_Nodes( branch *, int *),
Generate_Dendrite(branch *, int *),
Input_Current( branch *),
Assign_Current( branch *, double *, double ),
Matrix_Vector_Multiply( SparseMatrix *, double *, double *),
Matrix_Malloc( SparseMatrix *, int, int),
Matrix_Free( SparseMatrix *),
LU_Factor(SparseMatrix *, int *),
LU_Solve( SparseMatrix *, double *, double *);
#define CS 1.0
#define GS 0.091
#define GA 14.286
#define CM 1.0
#define GM 0.091
#define OUTPUT "NewRes40.dat"
#define INFO "Info20.dat"
#define TEND 10.0
#define NSIM 2000
#define NT 1000
#define DT 1.0
#define NODES 40
#define NSEED 7
#define FSEED "GenRan40.ran"
#define NCON 75
#define AMP 2.0e-5
#define SIN 0.0e-3
#define T 10.0
#define M 1000
#define RS 0.002
#define RD 0.000648741708
#define L 0.2256605
#define X 0.0135280571
SparseMatrix lhs, rhs;
unsigned int ix, iy, iz;
int main( int argc, char **argv )
{
extern unsigned int ix, iy, iz;
int k, j, id, start, begin, nodes, n, nc, i, in, nstep,
maxstep, ncon, FirstNode, NumberOfInput;
int counter, nb, nsim, connected;
double *v, *x, max, *eta, *eval, *cval, AreaOfSoma, gama, *chi,
xold, xnew, fac, arg, sum, tmp, vs, pi, dt, tnow, len,
h, CableDiameter, ElectrotonicLength, *amp, *loc, input,
CableLength, dx, CellLength, LocusContact;
void srand( unsigned int);
neuron *cell;
contact *newcon, *oldcon, *con;
extern SparseMatrix lhs, rhs;
branch *bnow, *bold, *bnew, *FirstBranch, *CellFirstBranch;
char word[20];
FILE *fp;
nsim = 1;
maxstep = ((int) 1000.0*T);
if ( (fp=fopen(FSEED,"r"))!=NULL ) {
while ( fscanf(fp,"%lu %lu %lu", &ix, &iy, &iz )!=EOF ) nsim++;
fclose(fp);
} else {
srand( ((unsigned int) NSEED) );
ix = rand( );
iy = rand( );
iz = rand( );
}
pi = 4.0*atan(1.0);
if ( argc != 2 ) {
printf("\n Invoke program with load <input>\n");
return 1;
} else {
printf("\nOpening file %s\n",argv[1]);
if ( (fp=fopen(argv[1],"r")) == NULL ) {
printf("\n Test Neuron file not found");
return 1;
}
bold = NULL;
while ( fscanf(fp,"%s",word) != EOF ) {
if ( strcmp(word,"Branch") == 0 || strcmp(word,"branch") == 0 ) {
bnew = (branch *) malloc( sizeof(branch) );
fscanf(fp,"%d", &(bnew->id) );
bnew->peer = NULL;
bnew->child = NULL;
bnew->conlist = NULL;
if ( bold != NULL) {
bold->child = bnew;
} else {
CellFirstBranch = bnew;
}
bnew->parent = bold;
fscanf(fp,"%lf %lf %lf", &(bnew->xl), &(bnew->yl), &(bnew->zl) );
fscanf(fp,"%lf %lf %lf", &(bnew->xr), &(bnew->yr), &(bnew->zr) );
fscanf(fp,"%lf %lf", &(bnew->plen), &(bnew->diam) );
bnew->elen = (bnew->plen)/sqrt(bnew->diam);
bold = bnew;
} else if ( strcmp(word,"Marker") == 0 || strcmp(word,"marker") == 0 ) {
if ( bold == NULL ) {
printf("\nMarker is not assigned to a branch\n");
return 0;
}
printf("Found and initialised a branch contact\n");
newcon = (contact *) malloc( sizeof(contact) );
newcon->next = NULL;
fscanf(fp,"%lf %lf", &newcon->xp, &newcon->amp );
if ( bnew->conlist == NULL ) {
bnew->conlist = newcon;
} else {
con = bnew->conlist;
while ( con->next ) con = con->next;
con->next = newcon;
}
} else {
printf("Unrecognised dendritic feature\n");
return 0;
}
}
fclose(fp);
}
CellLength = 0.0;
bnew = CellFirstBranch;
while ( bnew ) {
CellLength += bnew->plen;
bnew = bnew->child;
}
start = 1;
while ( nsim <= NSIM ) {
printf("\r Simulation %d", nsim);
bnow = CellFirstBranch;
bold = NULL;
while ( bnow ) {
bnew = (branch *) malloc( sizeof(branch) );
bnew->id = bnow->id;
bnew->xl = bnow->xl;
bnew->yl = bnow->yl;
bnew->zl = bnow->zl;
bnew->xr = bnow->xr;
bnew->yr = bnow->yr;
bnew->zr = bnow->zr;
bnew->diam = bnow->diam;
bnew->plen = bnow->plen;
bnew->elen = bnow->elen;
bnew->peer = NULL;
bnew->child = NULL;
if ( bold ) {
bold->child = bnew;
} else {
FirstBranch = bnew;
}
bnew->parent = bold;
bold = bnew;
if ( bnow->conlist ) {
oldcon = NULL;
con = bnow->conlist;
while ( con ) {
newcon = (contact *) malloc( sizeof(contact) );
newcon->next = NULL;
newcon->fl = NULL;
newcon->fr = NULL;
newcon->amp = con->amp;
newcon->id = con->id;
newcon->xp = con->xp;
newcon->xl = NULL;
newcon->xr = NULL;
if ( oldcon != NULL ) {
oldcon->next = newcon;
} else {
bnew->conlist = newcon;
}
oldcon = newcon;
con = con->next;
}
} else {
bnew->conlist = NULL;
}
bnow = bnow->child;
}
for ( k=0 ; k<NCON ; k++ ) {
LocusContact = CellLength*ran( &ix, &iy, &iz);
bnew = FirstBranch;
len = bnew->plen;
while ( LocusContact > len ) {
bnew = bnew->child;
len += bnew->plen;
}
newcon = (contact *) malloc( sizeof(contact) );
newcon->next = NULL;
newcon->fl = NULL;
newcon->fr = NULL;
newcon->amp = AMP;
newcon->xl = NULL;
newcon->xr = NULL;
newcon->xp = LocusContact-(len-bnew->plen);
if ( bnew->conlist ) {
oldcon = bnew->conlist;
while ( oldcon->next ) oldcon = oldcon->next;
oldcon->next = newcon;
} else {
bnew->conlist = newcon;
}
}
bold = FirstBranch;
n = 0;
while ( bold ) {
bnew = FirstBranch;
do {
tmp = pow(bold->xl-bnew->xr,2)+
pow(bold->yl-bnew->yr,2)+
pow(bold->zl-bnew->zr,2);
connected = ( tmp < 0.01 );
bnew = bnew->child;
} while ( bnew && !connected );
if ( !connected ) n++;
bold = bold->child;
}
cell = (neuron *) malloc( sizeof(neuron) );
cell->ndend = n;
cell->dendlist = (dendrite *) malloc( n*sizeof(dendrite) );
bold = FirstBranch;
n = 0;
while ( n < cell->ndend ) {
bnew = FirstBranch;
do {
tmp = pow(bold->xl-bnew->xr,2)+
pow(bold->yl-bnew->yr,2)+
pow(bold->zl-bnew->zr,2);
connected = ( tmp < 0.01 );
bnew = bnew->child;
} while ( bnew );
if ( !connected ) cell->dendlist[n++].root = bold;
bold = bold->child;
}
for ( k=0 ; k<cell->ndend ; k++ ) {
bold = cell->dendlist[k].root;
Remove_Branch( &FirstBranch, bold);
}
for ( k=0 ; k<cell->ndend ; k++ ) {
Build_Test_Dendrite( &FirstBranch, cell->dendlist[k].root );
}
if ( FirstBranch != NULL ) printf("\nWarning: Unconnected branch segments still exist\n");
ElectrotonicLength = 0.0;
bnow = cell->dendlist[0].root;
while ( bnow != NULL ) {
ElectrotonicLength += bnow->elen;
bnow = bnow->child;
}
CableDiameter = 0.0;
for ( k=0 ; k<cell->ndend ; k++ ) {
CableDiameter += pow(cell->dendlist[k].root->diam,1.5);
}
CableDiameter = pow(CableDiameter,2.0/3.0);
CableLength = ElectrotonicLength*sqrt(CableDiameter);
NumberOfInput = 0;
for ( k=0 ; k<cell->ndend ; k++ ) {
bnow = cell->dendlist[k].root;
NumberOfInput += Count_Contacts( cell->dendlist[k].root, bnow);
}
amp = (double *) malloc( NumberOfInput*sizeof(double) );
loc = (double *) malloc( NumberOfInput*sizeof(double) );
for ( ncon=k=0 ; k<cell->ndend ; k++ ) {
bnow = cell->dendlist[k].root;
Find_Contacts( bnow, loc, amp, &ncon);
}
tmp = sqrt(CableDiameter)/CableLength;
for ( k=0 ; k<ncon ; k++ ) loc[k] *= tmp;
if ( ncon == 0 ) {
printf("\n No contact found - Fatal error!");
return 1;
}
if ( start ) {
AreaOfSoma = 4.0*pi*RS*RS;
gama = AreaOfSoma/(pi*CableDiameter*CableLength);
eval = (double *) malloc( (M+1)*sizeof(double) );
cval = (double *) malloc( (M+1)*sizeof(double) );
eval[0] = 0.0;
cval[0] = 1.0;
for ( k=1 ; k<=M ; k++ ) {
xnew = arg = pi*((double) k );
do {
xold = xnew;
xnew = arg-atan(gama*xold);
} while ( fabs(xold-xnew) > 5.e-7 );
eval[k] = xnew;
cval[k] = cos(xnew);
}
eta = (double *) malloc( (M+1)*sizeof(double) );
eta[0] = GM/CM;
for ( k=1 ; k<=M ; k++ ) {
eta[k] = (GM+0.25*CableDiameter*GA*pow(eval[k]/CableLength,2))/CM;
}
}
chi = (double *) malloc( (M+1)*sizeof(double) );
fac = pi*CM*CableDiameter*CableLength;
for ( k=0 ; k<=M ; k++ ) {
chi[k] = SIN*cval[k];
for ( j=0 ; j<ncon ; j++ ) {
chi[k] += amp[j]*cos(eval[k]*(1.0-loc[j]));
}
chi[k] *= cval[k];
chi[k] /= fac*eta[k]*(1.0+gama*cval[k]*cval[k]);
}
for ( k=1 ; k<=M ; k++ ) chi[k] *= 2.0;
for ( nb=k=0 ; k<cell->ndend ; k++ ) {
bnow = cell->dendlist[k].root;
nb += Count_Branches( bnow, bnow);
}
h = CellLength/((double) NODES-nb);
for ( k=0 ; k<cell->ndend ; k++ ) Assign_Branch_Nodes( cell->dendlist[k].root, &h);
FirstNode = 0;
for ( k=0 ; k<cell->ndend ; k++ ) Enumerate_Nodes( cell->dendlist[k].root, &FirstNode );
for ( k=0 ; k<cell->ndend ; k++ ) cell->dendlist[k].root->junct = FirstNode;
nodes = FirstNode+1;
if ( start ) {
Matrix_Malloc( &lhs, nodes, 3*nodes-2 );
Matrix_Malloc( &rhs, nodes, 3*nodes-2 );
}
lhs.StartRow[0] = rhs.StartRow[0] = 0;
for ( counter=k=0 ; k<cell->ndend ; k++ ) {
bnow = cell->dendlist[k].root;
Generate_Dendrite( bnow, &counter);
}
lhs.a[3*nodes-3] = rhs.a[3*nodes-3] = 0.0;
for ( k=0 ; k<cell->ndend ; k++ ) {
bnow = cell->dendlist[k].root;
lhs.a[counter] = (bnow->diam)*(bnow->hseg);
rhs.a[counter] = -pow(bnow->diam,2)/(bnow->hseg);
lhs.col[counter] = rhs.col[counter] = bnow->first;
lhs.a[3*nodes-3] += 2.0*(bnow->diam)*(bnow->hseg);
rhs.a[3*nodes-3] += pow(bnow->diam,2)/(bnow->hseg);
counter++;
}
if ( counter != 3*(nodes-1) ) {
printf("\nEnumeration error!\n");
getchar( );
} else {
}
lhs.col[counter] = rhs.col[counter] = nodes-1;
lhs.StartRow[nodes] = rhs.StartRow[nodes] = counter+1;
for( k=0 ; k<cell->ndend ; k++ ) Input_Current(cell->dendlist[k].root);
if ( nsim == 1 ) {
fp = fopen(OUTPUT, "w");
fclose(fp);
}
dt = 1.0/((double) NT);
for ( k=0 ; k<3*nodes-2 ; k++ ) {
rhs.a[k] = pi*dt*(0.125*GA*rhs.a[k]+GM*lhs.a[k]/12.0);
lhs.a[k] *= pi*CM/6.0;
lhs.a[k] += rhs.a[k];
rhs.a[k] = lhs.a[k]-2.0*rhs.a[k];
}
lhs.a[3*nodes-3] += AreaOfSoma*(CS+0.5*GS*dt);
rhs.a[3*nodes-3] += AreaOfSoma*(CS-0.5*GS*dt);
if ( start ) {
v = (double *) malloc( (nodes)*sizeof(double) );
x = (double *) malloc( (nodes)*sizeof(double) );
}
for ( k=0 ; k<nodes ; k++ ) v[k] = x[k] = 0.0;
begin = 1;
LU_Factor(&lhs, &begin);
nstep = 0;
while ( nstep < maxstep ) {
Matrix_Vector_Multiply( &rhs, v, x);
x[nodes-1] -= dt*SIN;
for ( k=0 ; k<nodes ; k++ ) v[k] = x[k];
if ( nstep < maxstep ) {
for ( k=0 ; k<cell->ndend ; k++ ) Assign_Current(cell->dendlist[k].root, x, dt);
}
LU_Solve( &lhs, v, x );
nstep++;
if ( nstep%1000 == 0 ) {
tnow = dt*((double) nstep);
for ( vs=0.0,k=M ; k>=0 ; k-- ) {
arg = tnow*eta[k];
if ( arg > 20.0 ) {
tmp = 1.0;
} else {
tmp = (1.0-exp(-arg));
}
vs -= tmp*chi[k];
}
fp = fopen(OUTPUT,"a");
fprintf(fp,"%20.15lf",v[nodes-1]);
fclose(fp);
}
}
fp = fopen(OUTPUT, "a");
fprintf(fp,"\n");
fclose(fp);
free(amp);
free(loc);
free(chi);
Destroy_Test_Neuron( cell );
if ( nsim == 1 ) {
fp = fopen(FSEED,"w");
} else {
fp = fopen(FSEED,"a");
}
fprintf(fp,"%u \t %u \t %u\n", ix, iy, iz);
fclose(fp);
if ( start ) start = 0;
nsim++;
}
return 0;
}
void Output_Info( branch *b, FILE *fp)
{
if ( b->child ) Output_Info( b->child, fp);
if ( b->peer ) Output_Info( b->peer, fp);
fprintf(fp,"%3d \t $3d\n", b->id, b->nc);
return;
}
void Build_Test_Dendrite( branch **head, branch *root)
{
double tmp;
branch *bnow, *bnext, *btmp;
bnow = *head;
while ( bnow != NULL ) {
bnext = bnow->child;
tmp = pow(bnow->xl-root->xr,2)+
pow(bnow->yl-root->yr,2)+
pow(bnow->zl-root->zr,2);
if ( tmp <= 0.01 ) {
Remove_Branch( head, bnow);
bnow->child = NULL;
bnow->peer = NULL;
bnow->parent = root;
if ( root->child != NULL ) {
btmp = root->child;
while ( btmp->peer != NULL ) btmp = btmp->peer;
btmp->peer = bnow;
} else {
root->child = bnow;
}
}
bnow = bnext;
}
if ( root->child ) Build_Test_Dendrite( head, root->child);
if ( root->peer ) Build_Test_Dendrite( head, root->peer);
return;
}
void Remove_Branch(branch **head, branch *b)
{
if ( *head == NULL || b == NULL ) return;
if ( *head == b ) {
*head = b->child;
if ( *head != NULL ) (*head)->parent = NULL;
} else {
b->parent->child = b->child;
if ( b->child != NULL ) b->child->parent = b->parent;
}
b->parent = NULL;
b->child = NULL;
return;
}
void Destroy_Test_Neuron(neuron *cell)
{
int k;
for ( k=0 ; k<cell->ndend ; k++ ) {
Destroy_Test_Dendrite( cell->dendlist[k].root );
}
free(cell);
return;
}
void Destroy_Test_Dendrite( branch *b )
{
int i;
contact *prevcon, *nextcon;
if ( b->child ) Destroy_Test_Dendrite(b->child);
if ( b->peer ) Destroy_Test_Dendrite(b->peer);
if ( b->conlist ) {
prevcon = b->conlist;
do {
nextcon = prevcon->next;
free(prevcon);
prevcon = nextcon;
} while ( prevcon );
}
free(b);
return;
}
int Count_Contacts( branch *bstart, branch *bnow)
{
static int n;
contact *con;
if ( bstart == bnow ) n = 0;
if ( bnow != NULL ) {
if ( bnow->child ) Count_Contacts(bstart, bnow->child);
if ( bnow->peer ) Count_Contacts(bstart, bnow->peer);
con = bnow->conlist;
while ( con ) {
n++;
con = con->next;
}
}
return n;
}
int Count_Branches( branch *bstart, branch *bnow)
{
static int n;
if ( bstart == bnow ) n = 0;
if ( bnow != NULL ) {
if ( bnow->child ) Count_Branches(bstart, bnow->child);
if ( bnow->peer ) Count_Branches(bstart, bnow->peer);
n++;
}
return n;
}
void Find_Contacts( branch *b, double *loc, double *amp, int *ncon)
{
contact *con;
branch *btmp;
if ( b->child ) Find_Contacts( b->child, loc, amp, ncon);
if ( b->peer ) Find_Contacts( b->peer, loc, amp, ncon);
con = b->conlist;
while ( con ) {
amp[(*ncon)] = con->amp;
loc[(*ncon)] = (con->xp)/sqrt(b->diam);
btmp = b->parent;
while ( btmp ) {
loc[(*ncon)] += btmp->elen;
btmp = btmp->parent;
}
(*ncon)++;
con = con->next;
}
return;
}
void Enumerate_Nodes(branch *bnow, int *FirstNode )
{
branch *btmp;
if ( bnow->child ) Enumerate_Nodes( bnow->child, FirstNode );
if ( bnow->peer ) Enumerate_Nodes( bnow->peer, FirstNode );
if ( bnow->child ) {
btmp = bnow->child;
while ( btmp ) {
btmp->junct = *FirstNode;
btmp = btmp->peer;
}
}
*FirstNode += bnow->nc;
bnow->first = *FirstNode-1;
return;
}
void Generate_Dendrite( branch *b, int *counter)
{
int k, CurrentNode, nc;
extern SparseMatrix lhs, rhs;
branch *btmp;
double SumL, SumR;
if ( b->child ) Generate_Dendrite( b->child, counter);
if ( b->peer ) Generate_Dendrite( b->peer, counter);
nc = b->nc;
CurrentNode = (b->first)-(nc-1);
if ( b->child ) {
btmp = b->child;
SumR = SumL = 0.0;
while ( btmp ) {
lhs.a[*counter] = (btmp->diam)*(btmp->hseg);
rhs.a[*counter] = -pow(btmp->diam,2)/(btmp->hseg);
lhs.col[*counter] = rhs.col[*counter] = btmp->first;
SumL += 2.0*(btmp->diam)*(btmp->hseg);
SumR += pow(btmp->diam,2)/(btmp->hseg);
(*counter)++;
btmp = btmp->peer;
}
lhs.a[*counter] = SumL+2.0*(b->diam)*(b->hseg);
rhs.a[*counter] = SumR+pow(b->diam,2)/(b->hseg);
lhs.col[*counter] = rhs.col[*counter] = CurrentNode;
(*counter)++;
lhs.a[*counter] = (b->diam)*(b->hseg);
rhs.a[*counter] = -pow(b->diam,2)/(b->hseg);
if ( CurrentNode == b->first ) {
lhs.col[*counter] = rhs.col[*counter] = b->junct;
} else {
lhs.col[*counter] = rhs.col[*counter] = CurrentNode+1;
}
(*counter)++;
lhs.StartRow[CurrentNode+1] = rhs.StartRow[CurrentNode+1] = *counter;
} else {
lhs.a[*counter] = 2.0*(b->diam)*(b->hseg);
rhs.a[*counter] = pow(b->diam,2)/(b->hseg);
lhs.col[*counter] = rhs.col[*counter] = CurrentNode;
(*counter)++;
lhs.a[*counter] = (b->diam)*(b->hseg);
rhs.a[*counter] = -pow(b->diam,2)/(b->hseg);
if ( CurrentNode == b->first ) {
lhs.col[*counter] = rhs.col[*counter] = b->junct;
} else {
lhs.col[*counter] = rhs.col[*counter] = CurrentNode+1;
}
(*counter)++;
lhs.StartRow[CurrentNode+1] = rhs.StartRow[CurrentNode+1] = *counter;
}
for ( k=nc-1 ; k>0 ; k-- ) {
CurrentNode++;
lhs.a[*counter] = (b->diam)*(b->hseg);
rhs.a[*counter] = -pow(b->diam,2)/(b->hseg);
lhs.col[*counter] = rhs.col[*counter] = CurrentNode-1;
(*counter)++;
lhs.a[*counter] = 4.0*(b->diam)*(b->hseg);
rhs.a[*counter] = 2.0*pow(b->diam,2)/(b->hseg);
lhs.col[*counter] = rhs.col[*counter] = CurrentNode;
(*counter)++;
lhs.a[*counter] = (b->diam)*(b->hseg);
rhs.a[*counter] = -pow(b->diam,2)/(b->hseg);
if ( CurrentNode == b->first ) {
lhs.col[*counter] = rhs.col[*counter] = b->junct;
} else {
lhs.col[*counter] = rhs.col[*counter] = CurrentNode+1;
}
(*counter)++;
lhs.StartRow[CurrentNode+1] = rhs.StartRow[CurrentNode+1] = *counter;
}
return;
}
void Input_Current( branch *b )
{
int k;
double len;
contact *con;
if ( b->child ) Input_Current( b->child );
if ( b->peer ) Input_Current( b->peer );
con = b->conlist;
while ( con ) {
k = 0;
len = b->hseg;
while ( con->xp > len ) {
k++;
len += b->hseg;
}
if ( k == 0 ) {
con->xl = b->junct;
con->xr = b->first;
con->fl = 1.0-(con->xp)/(b->hseg);
con->fr = (con->xp)/(b->hseg);
} else {
con->xl = b->first-k+1;
con->xr = b->first-k;
con->fl = (len-con->xp)/(b->hseg);
con->fr = 1.0-(con->fl);
}
con = con->next;
}
return;
}
void Assign_Current(branch *bnow, double *x, double fac )
{
contact *con;
if ( bnow->child ) Assign_Current(bnow->child, x, fac );
if ( bnow->peer ) Assign_Current(bnow->peer, x, fac );
con = bnow->conlist;
while ( con ) {
x[con->xl] -= fac*(con->fl)*(con->amp);
x[con->xr] -= fac*(con->fr)*(con->amp);
con = con->next;
}
return;
}
void Matrix_Vector_Multiply( SparseMatrix *a, double *v , double *b)
{
int i, j, k, n;
n = a->n;
for ( j=0 ; j<n ; j++) {
k = a->StartRow[j+1];
for( b[j]=0.0,i=(a->StartRow[j]) ; i<k ; i++ ) {
b[j] += (a->a[i])*v[a->col[i]];
}
}
return;
}
void Matrix_Malloc( SparseMatrix *a, int n, int w)
{
a->a = (double *) malloc( w*sizeof(double) );
a->col = (int *) malloc( w*sizeof(int) );
a->StartRow = (int *) malloc( (n+1)*sizeof(int) );
a->n = n;
a->l = malloc(sizeof(SparseMatrix));
a->u = malloc(sizeof(SparseMatrix));
a->l->a = (double *) malloc( (2*n-1)*sizeof(double) );
a->l->col = (int *) malloc( (2*n-1)*sizeof(int) );
a->l->StartRow = (int *) malloc( (n+1)*sizeof(int) );
a->l->n = n;
a->u->a = (double *) malloc( (2*n-1)*sizeof(double) );
a->u->col = (int *) malloc( (2*n-1)*sizeof(int) );
a->u->StartRow = (int *) malloc( (n+1)*sizeof(int) );
a->u->n = n;
return;
}
void Matrix_Free( SparseMatrix *a)
{
free(a->a);
free(a->col);
free(a->StartRow);
free(a);
}
void LU_Factor(SparseMatrix *m, int *start)
{
double tmp, sum;
int i, j, k, r, n, cl, cu, col, row;
n = m->n;
if ( *start ) {
cl = cu = 0;
for ( i=k=0 ; i<n ; i++ ) {
m->l->StartRow[i] = cl;
m->u->StartRow[i] = cu;
while ( m->col[k] < i ) m->l->col[cl++] = m->col[k++];
m->l->col[cl++] = m->col[k];
m->u->col[cu++] = m->col[k++];
while ( k < m->StartRow[i+1] ) m->u->col[cu++] = m->col[k++];
}
m->l->StartRow[n] = cl;
m->u->StartRow[n] = cu;
*start = 0;
}
cl = cu = 0;
for ( i=0 ; i<n ; i++ ) {
for ( k=m->StartRow[i] ; k < m->StartRow[i+1] ; k++ ) {
if ( m->col[k] < i ) {
sum = m->a[k];
for ( j=m->l->StartRow[i] ; j<cl ; j++ ) {
col = m->l->col[j];
row = m->u->StartRow[col];
while ( m->u->col[row] < m->col[k] && row < m->u->StartRow[col+1] ) row++;
if ( m->u->col[row] == m->col[k] ) sum -= (m->l->a[j])*(m->u->a[row]);
}
row = m->u->StartRow[m->l->col[cl]];
while ( m->u->col[row] < m->col[k] ) row++;
m->l->a[cl++] = sum/(m->u->a[row]);
} else if ( m->col[k] == i ) {
m->l->a[cl++] = 1.0;
sum = m->a[k];
for ( j=m->l->StartRow[i] ; j<m->l->StartRow[i+1]-1; j++ ) {
col = m->l->col[j];
row = m->u->StartRow[col];
while ( m->u->col[row] < i && row < m->u->StartRow[col+1] ) row++;
if ( m->u->col[row] == i ) sum -= (m->l->a[j])*(m->u->a[row]);
}
m->u->a[cu++] = sum;
} else {
sum = m->a[k];
for ( j=m->l->StartRow[i] ; j<m->l->StartRow[i+1]-1; j++ ) {
col = m->l->col[j];
row = m->u->StartRow[col];
while ( m->u->col[row] < m->col[k] && row < m->u->StartRow[col+1] ) row++;
if ( m->u->col[row] == m->col[k] ) sum -= (m->l->a[j])*(m->u->a[row]);
}
m->u->a[cu++] = sum;
}
}
}
return;
}
void LU_Solve(SparseMatrix *m, double *x, double *b )
{
int i,j;
double *z;
z = (double *) malloc( (m->n)*sizeof(double) );
for ( i=0 ; i<m->n ; i++ ) {
z[i] = b[i];
for (j=m->l->StartRow[i];j<m->l->StartRow[i+1]-1;j++)
{ z[i] -= (m->l->a[j])*(z[(m->l->col[j])]); }
z[i] /= m->l->a[m->l->StartRow[i+1]-1];
}
for ( i = (m->n) - 1 ; i>=0 ; i-- ) {
x[i] = z[i];
for (j=m->u->StartRow[i]+1;j<m->u->StartRow[i+1];j++)
{ x[i] -= (m->u->a[j])*(x[m->u->col[j]]); }
x[i] /= m->u->a[m->u->StartRow[i]];
}
free(z);
return;
}
void Assign_Branch_Nodes( branch *b, double *h )
{
int nc, k;
double hseg;
nc = ((int) ceil((b->plen)/(*h)));
b->hseg = (b->plen)/((double) nc);
b->nc = nc;
if ( b->child != NULL) Assign_Branch_Nodes( b->child, h);
if ( b->peer != NULL) Assign_Branch_Nodes( b->peer, h);
return;
}
double ran(unsigned int *ix, unsigned int *iy, unsigned int *iz)
{
double tmp;
*ix = (171*(*ix))%30269;
*iy = (172*(*iy))%30307;
*iz = (170*(*iz))%30323;
tmp = ((double) (*ix))/30269.0+((double) (*iy))/30307.0
+((double) (*iz))/30323.0;
return fmod(tmp,1.0);
}