-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmklink.c
More file actions
1002 lines (835 loc) · 33.9 KB
/
Copy pathmklink.c
File metadata and controls
1002 lines (835 loc) · 33.9 KB
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
/* Program do generacji polaczen synaptycznych miedzy subnets
Na wyjsciu ma produkowac plik z polaczeniami z zapisana para neuronow waga i
delay. Nastepny program (lub druga czesc tego samego) moze wykorzystac to
do generacji struktury neuronow i synaps we wlasciwym formacie.
01/29/03 P. Kudela, losuj2 prawdopodobienstwo polaczenia zanika exp.
a w zapisz2 delay wzrasta liniowo
05/29/03 P. Kudela, now multi-class supported
*/
#define RISC
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <math.h>
#include "lnet.h"
#define randomm(num) (int)((double)num*(double)random()/((double)(RAND_MAX+1.0)))
#define randomize() srandom((unsigned)time(NULL))
int lrodz; /* number of neuron classes */
int *NN; /* NN[lrodz] total number of neurons within each class */
int *NOF; /* NOF[lrodz] table of index offsets */
int **N; /* N[lrodz][3] size of subnetwork for each class */
int **k; /* k[lrodz][3] scaling factor to align class networks */
int ***o; /* o[lrodz][lrodz][3] neighborhood array */
int **s; /* s[lrodz][lrodz] synapsy na we/wy z otoczenie */
int **w,**sdw; /* w[lrodz][lrodz] sdw[][] synaptic weights and dispersions */
int **d,**sdd; /* d[lrodz][lrodz] sdd[][] synaptic delays and dispersions */
double **Ap,**lambda;/* Ap[lrodz][lrodz] lambda[][] amplitude and spatial decay rate of connection probability */
int *tab; /* temporary neighborhood table tab[maxot]*/
double *dist, *costheta, *sintheta; /* table of distances between candidate neurons and costheta, sintheta, updated 08/23/05 */
int *postx, *posty; /* Array of postsynaptic neuron coordinates for accepted target */
double *zclass; /* Array of class z-values within cortical model */
int *horaxonflagpos,*veraxonflagpos, *axonflag; /* Array of flags for stimulated axon portions */
int *horaxonflagneg,*veraxonflagneg;
/* Neuron Class Type Firing Pattern
1 Layer II/III Pyramid RS
2 Layer IV Spiny RS
3 Layer V Pyramid IB
4 Layer VI Pyramid IB
5 Basket Layers II-VI FS
6 Double Bouquet II-VI FS
7 Chandelier FS */
#define P23 1
#define P4 2
#define P5 3
#define P6 4
#define BASKET 5
#define BOUQUET 6
#define CHANDELIER 7
#define BASKETANG 0.0 //1.5708 /* for offset costheta distr for basket cell axons */
#define PIIIANG 0.0 /* for offset costheta distr for PIII cell axons */
#define PVANG 0.0 /*for offset costheta distr for PV cell axons */
#define PVIANG 0.0 /* for offset costheta distr for PVI cell axons */
#define SPATIALSCALE 25 /* center to center column spacing in microns */
#define BASKETLAMBDA 50 /* 1/e decay for basket cell axon connections */
#define BASKETNEAR 50 /* distance in microns within which basket conn'ions are isotropic */
#define PIIINEARRAD 300 /* Near connection radius for PIII neurons */
#define STRIPWIDTH 500 /* 500 micron strip width for domain simulations */
#define PIIIFARAVE 2000 /* Ave dist to long range connection spot */
#define PIIISPOTRAD 200 /* Long range spot radius */
#define BOUNDARYLAYER 300 /* Boundary layer distance, to reduce connections */
#define ISLENGTH 30 /* One half initial segment length (microns) */
/* Definitions for E-field effects */
#define SEG 500 /* spacing btwn nodes of Ranvier for 5.7 micron myelinated axon */
//#define VOLT .2 /* stimulation voltage applied to metal electrode in voltage controlled mode */
#define I0 .010 /* Stimulation current in current controlled mode in Amps */
#define SIGMA 0.3 /* Tissue conductivity in A/Vm */
#define THRESHD2V 2 /* Threshold for activation fct (mV), second difference of voltage along fiber element */
#define A 1000 /* Electrode disk radius (microns) */
#define STIMPOS 2340 /* Electrode Z-value (microns), taken with z=0 at the top of the white matter */
#define DISKX 32 /* Center of electrode disk x-value (lattice constants) */
#define DISKY 32 /* Center of electrode disk y-value (lattice constants) */
#define PI 3.14159
/* Cortical layer z-values */
#define ZVIB 162 /* VIbeta */
#define ZVIA 488 /* VIalpha */
#define ZV 1130 /* V */
#define ZIVCBETA 1230 /* IVCbeta */
#define ZIVCALPHA 1230 /* IVCalpha */
#define ZIVB 1230 /* IVB */
#define ZIVA 1230 /* IVA */
#define ZIII 1540 /* III */
#define ZII 1940 /* II */
#define ZI 2185 /* I */
#define Gi 7.6e-07 /* Intracompartment conductivity in inverse Ohms */
struct SYN_TMP
{
NEURON_INDEX neuron_pre;
NEURON_INDEX neuron_post;
NEURON_INDEX preclass_num;
WEIGHT weight;
DELAY delay;
VAR axdelay;
};
int sort_delay(const void *, const void *);
int sort_neu_num(const void *, const void *);
void set_neu(struct NEURON *);
unsigned seed_save();
int main(int argc, char *argv[])
{
int i,j,l,jp;
int nr,ir,ix,iy,iz; /* index for neurons*/
int maxot, tmpot;
int xyz[3]={0,0,0};
int no,nl;
ulong ns,is;
int *neu_pre;
int Nneu;
int torus_flag;
int syn_n;
char label[]="xyz",name[10];
FILE *inputfile,*outputfile,*graphfile,*axflagfile;
struct SYN_TMP *syn_tmp;
struct SYNAPSE syn;
struct NEURON *neu;
int otoczenie(int,int,int*,int);
int otoczenie2(int,int,int*,int);
int losuj(int*,int);
int losuj2(int*,double*,double*,double*,int,double,double,int,int,int*);
extern int zapisz(int,int,int,int,FILE *);
extern int zapisz2(int,int,int,int,double,FILE *,FILE *,FILE *,int);
extern int sort_neu_num(const void *, const void *);
int nsub;
int pre[3];
double horzmid,horxmid,horymid,horrhomid,horVmid,horrhopre,horVpre,horrhopost,horVpost;
double horseglength,hordelsquaredV;
double verzmid,verxmid,verymid,verrhomid,verVmid,verrhopre,verVpre,verrhopost,verVpost;
double verseglength,verdelsquaredV,aradius,coeff;
int synapsesum;
double currentinjsum, avecurrentinj;
int nps,s_j,s_i,critdex;
/*
1.Reading and checking input file information
*/
/* printf("Made it to file checking\n"); */
if(argc<3)
{
fprintf(stderr,"USAGE:\n\tmk_link <file_name> <sqrt(no of subnets in all)> [<seed>]\n");
return 1;
}
if(argc>3)
srandom((unsigned)atoi(argv[3]));
else
printf("seed %u\n",seed_save()); /* seed for srand */
srand48(127L);
//inputfile = fopen(strcpy(name,argv[1]),"r");
inputfile = fopen(strcat(strcpy(name,argv[1]),".net"),"r");
if(inputfile==NULL){fprintf(stderr, "Network definition file not present %s\n",name);return -1;}
fscanf(inputfile,"%d",&torus_flag);
fscanf(inputfile,"%d",&lrodz);
nsub=atoi(argv[2]);
/* Memory allocations */
N=(int**)malloc(lrodz*sizeof(int*)); /* table allocation for size of networks for each class */
for(i=0;i<lrodz;i++)
N[i]=(int*)malloc(3*sizeof(int));
k=(int**)malloc(lrodz*3*sizeof(int)); /* table allocation for network scaling factor for each class */
for(i=0;i<lrodz;i++)
k[i]=(int*)malloc(3*sizeof(int));
o=(int ***)malloc(lrodz*sizeof(int **)); /* allocating neighborhood tables */
for(i=0;i<lrodz;i++)
o[i]=(int **)malloc(lrodz*sizeof(int *));
for(i=0;i<lrodz;i++)
for(j=0;j<lrodz;j++)
o[i][j]=(int *)malloc(3*sizeof(int));
s=(int **)malloc(lrodz*sizeof(int*)); /* synaptic table allocation */
for(i=0;i<lrodz;i++)
s[i]=(int*)malloc(lrodz*sizeof(int));
w=(int **)malloc(lrodz*sizeof(int*)); /* allocation for synaptic weights */
for(i=0;i<lrodz;i++)
w[i]=(int*)malloc(lrodz*sizeof(int));
sdw=(int **)malloc(lrodz*sizeof(int*)); /* allocation for synaptic weight dispersion */
for(i=0;i<lrodz;i++)
sdw[i]=(int*)malloc(lrodz*sizeof(int));
d=(int **)malloc(lrodz*sizeof(int*)); /* allocation for synaptic delay */
for(i=0;i<lrodz;i++)
d[i]=(int*)malloc(lrodz*sizeof(int));
sdd=(int **)malloc(lrodz*sizeof(int*)); /* allocation for synaptic delay dispersion */
for(i=0;i<lrodz;i++)
sdd[i]=(int*)malloc(lrodz*sizeof(int));
Ap=(double **)malloc(lrodz*sizeof(double)); /* used to compute probability of connection */
for(i=0;i<lrodz;i++)
Ap[i]=(double*)malloc(lrodz*sizeof(double));
lambda=(double **)malloc(lrodz*sizeof(double)); /* used to commpute probability of connection */
for(i=0;i<lrodz;i++)
lambda[i]=(double*)malloc(lrodz*sizeof(double));
zclass=(double*)malloc(lrodz*sizeof(double));
/* Load cortical layer z-information for E-field calc, array index by class number */
zclass[0]= ZII;
zclass[1]= ZIVB;
zclass[2]= ZV;
zclass[3]= ZVIA;
zclass[4]= ZII;
zclass[5]= ZII;
zclass[6]= ZIVB;
/* initialize mapping and scaling arrays */
for(i=0;i<lrodz;i++)
{
N[i][0]=N[i][1]=nsub; //square mapping
N[i][2]=1;
k[i][0]=k[i][1]=k[i][2]=1;
}
/* input parameters from file to fill arrays */
for(i=0;i<lrodz;i++)
for(j=0;j<lrodz;j++)
{
fscanf(inputfile,"%d,%d,%d",&o[j][i][0],&o[j][i][1],&o[j][i][2]);
fscanf(inputfile,"%d",&s[j][i]);
fscanf(inputfile,"%d",&w[j][i]);
fscanf(inputfile,"%d",&sdw[j][i]);
fscanf(inputfile,"%d",&d[j][i]);
fscanf(inputfile,"%d",&sdd[j][i]);
fscanf(inputfile,"%lf",&Ap[j][i]);
fscanf(inputfile,"%lf",&lambda[j][i]);
}
fclose(inputfile);
/*
2. Offset calculations and table filling
*/
/* printf("Made it to offset calculations\n"); */
NN=(int*)malloc(lrodz*sizeof(long int)); /* table with number of subnetworks containing each class */
for(i=0;i<lrodz;i++)
NN[i]=(N[i][0]?N[i][0]:1)*(N[i][1]?N[i][1]:1)*(N[i][2]?N[i][2]:1);
NOF=(int*)malloc(lrodz*sizeof(int)); /* table with offsets */
for(i=1,NOF[0]=0;i<lrodz;i++)
NOF[i]=NOF[i-1]+NN[i-1];
/* maximal neigborhood calculation */
maxot=0;
if(torus_flag!=2) /* i.e. not choosing from whole array */
for(i=0;i<lrodz;i++)
for(j=0;j<lrodz;j++)
{
tmpot=1;
for(l=0;l<3;l++)
tmpot*=2*o[i][j][l]+1;
if(tmpot > maxot)
maxot=tmpot;
}
else
for(i=0;i<lrodz;i++)
{
if(maxot<NN[i])
maxot=NN[i];
}
tab=(int*)malloc(maxot*sizeof(int));
dist=(double*)malloc(maxot*sizeof(double));
costheta=(double*)malloc(maxot*sizeof(double));
sintheta=(double*)malloc(maxot*sizeof(double));
postx=(int*)malloc(maxot*sizeof(int));
posty=(int*)malloc(maxot*sizeof(int));
horaxonflagpos=(int*)malloc(maxot*sizeof(int));
veraxonflagpos=(int*)malloc(maxot*sizeof(int));
horaxonflagneg=(int*)malloc(maxot*sizeof(int));
veraxonflagneg=(int*)malloc(maxot*sizeof(int));
axonflag=(int*)malloc(maxot*sizeof(int));
Nneu=0;
for(i=0;i<lrodz;i++)
Nneu+=NN[i]; /* calculate subnetworks X # of classes */
for(i=0;i<lrodz;i++)
printf("\nNN[%d]=%d, NOF[%d]=%d",i,NN[i],i,NOF[i]); /* co by sprawdzic */
printf("\nmaxot=%d,Nneu=%d\n",maxot,Nneu);
/*
3.Setting up for output of links
*/
outputfile=fopen("syn.tmp","wb");
graphfile=fopen("syn.graph","wb");
axflagfile=fopen("syn.axflag","wb");
if(outputfile==NULL){fprintf(stderr,"\nError in opening synapse output file");exit(1);}
if(graphfile==NULL){fprintf(stderr,"\nError in opening synapse output file");exit(1);}
if(axflagfile==NULL){fprintf(stderr,"\nError in opening axflagfile file");exit(1);}
/*randomize();*/
nr=0; /* subnetwork "neuron" number */
ns=0; /* synapse index */
currentinjsum=0; /* in microamps */
synapsesum=0;
for(ir=0;ir<lrodz;ir++) /* loop on classes of pre-neurons */
{
//printf("Working on Preclass %d\n",ir+1);
for (ix=0,xyz[0]=0;ix<N[ir][0];ix++,xyz[0]+=k[ir][0]) /* x-loop */
{
//printf("ix= %d\n",ix+1);
for(iy=0,xyz[1]=0;iy<N[ir][1];iy++,xyz[1]+=k[ir][1]) /* y-loop */
for(iz=0,xyz[2]=0;iz<N[ir][2];iz++,xyz[2]+=k[ir][2],nr++) /* z-loop */
for(jp=0;jp<lrodz;jp++) /* loop on classes of post-neurons */
{
/* generate tables with numbers of neurons of each class jp,in the neighborhood of neuron nr (neuron from class ir
connects to jp (used to be the other way around) */
switch(torus_flag)
{
case 0: /* no torus boundary condition */
no=otoczenie(nr,ir,xyz,jp);
break;
case 1: /* torus boundary */
no=otoczenie2(nr,ir,xyz,jp);
break;
case 2: /* case 2 choosing from whole array */
for(i=NOF[jp],no=0;i<NN[jp]+NOF[jp];i++)
if(i!=nr)
tab[no++]=i;
break;
default:
fprintf(stderr,"wrong option for torus flag %d\n",torus_flag);
exit(0);
}
if(!no)continue;
l=(no<s[jp][ir])?no:s[jp][ir];
/* if no is greater than the number of allowed synapses, then set l=# of allowed synapses */
//printf("Allowed/required conn comp no=%d,preclass=%d,postclass=%d,s[jp][ir]=%d\n",no,ir+1,jp+1,s[jp][ir]);/* network linkage checks */
/* Boundary effect correction, reducing allowed connections for neurons in specific column number ranges, and row number ranges */
nps=nsub; //sqrt(subnets/node)*sqrt(nodes)
s_j=(nr)%nps; /* column index of presynaptic subnetwork */
s_i=(nr)/nps-ir*nps; /* row index of presynaptic subnetwork */
critdex=BOUNDARYLAYER/SPATIALSCALE;
//printf("Subnet indices: column=%d row=%d nps=%d critdex=%d nr=%d\n",s_j,s_i,nps,critdex,nr);
//printf("Pre-l: l=%d\n",l);
if ( (s_j<critdex) && (s_i>=critdex) && (s_i<=((nps-1)-critdex)) ){
l=(l*5)/8;
}
if ( (s_j>((nps-1)-critdex)) && (s_i>=critdex) && (s_i<=((nps-1)-critdex)) ){
l=(l*5)/8;
}
if ( (s_i<critdex) && (s_j>=critdex) && (s_j<=((nps-1)-critdex)) ){
l=(l*5)/8;
}
if ( (s_i>((nps-1)-critdex)) && (s_j>=critdex) && (s_j<=((nps-1)-critdex)) ){
l=(l*5)/8;
}
if ( (s_j<critdex) && (s_i<critdex) ){
l=(l*3)/8;
}
if ( (s_j<critdex) && (s_i>((nps-1)-critdex)) ){
l=(l*3)/8;
}
if ( (s_j>((nps-1)-critdex)) && (s_i<critdex) ){
l=(l*3)/8;
}
if ( (s_j>((nps-1)-critdex)) && (s_i>((nps-1)-critdex)) ){
l=(l*3)/8;
}
//printf("Post-l: l=%d\n",l);
/* Form connections and determine stimulation locations */
for(i=0;i<l;i++) /* losuje z calego otoczenia - nowa wersja */
{ /* z losuj2 01/29/03 */
for(j=0;j<3;j++)
pre[j]=xyz[j]/k[jp][j];
// printf("no=%d,preclass=%d,postclass=%d,l=%d,i=%d\n",no,ir+1,jp+1,l,i); /* network linkage checks */
nl=-1;
while (nl==-1){
nl=losuj2(tab,dist,costheta,sintheta,no,Ap[jp][ir],lambda[jp][ir],jp,ir,pre);
}
//printf("i= %d,l= %d,nl= %d\n",i,l,nl);
if(nl!=-1)
{
/* Set synapse flag if gradient requirement met */
/* Check E field gradient requirement */
/* Charged balance, bipolar pulse */
/* Horizontal axon segment */
aradius=A;
aradius=aradius/(1000*1000);
coeff=I0/(4*PI*SIGMA*aradius);
//printf("\n aradius=%f, coeff=%f",aradius,coeff);
axonflag[nl]=0;
horaxonflagpos[nl]=0;
horaxonflagneg[nl]=0;
horzmid=zclass[jp];
horxmid=(pre[0]+postx[nl])/2;
horymid=(pre[1]+posty[nl])/2;
horrhomid=sqrt(pow((DISKX-horxmid),2)+pow((DISKY-horymid),2))*25;
horVmid=(coeff)*asin(2*A/(sqrt(pow((STIMPOS-horzmid),2)+pow((horrhomid-A),2))+sqrt(pow((STIMPOS-horzmid),2)+pow((horrhomid+A),2))));
horrhopre=sqrt(pow((DISKX-pre[0]),2)+pow((DISKY-pre[1]),2))*25;
horVpre=(coeff)*asin(2*A/(sqrt(pow((STIMPOS-zclass[jp]),2)+pow((horrhopre-A),2))+sqrt(pow((STIMPOS-zclass[jp]),2)+pow((horrhopre+A),2))));
horrhopost=sqrt(pow((DISKX-postx[nl]),2)+pow((DISKY-posty[nl]),2))*25;
horVpost=(coeff)*asin(2*A/(sqrt(pow((STIMPOS-zclass[jp]),2)+pow((horrhopost-A),2))+sqrt(pow((STIMPOS-zclass[jp]),2)+pow((horrhopost+A),2))));
horseglength=sqrt(pow((postx[nl]-pre[0]),2)+pow((posty[nl]-pre[1]),2))*25;
/* hordelsquaredV in mV */
hordelsquaredV = (horVpre+horVpost-2*horVmid)*1000;
if (hordelsquaredV > THRESHD2V)
horaxonflagpos[nl]=1;
if (-hordelsquaredV > THRESHD2V)
horaxonflagneg[nl]=1;
// Vertical axon segment (Old Version)
//veraxonflagpos[nl]=0;
//veraxonflagneg[nl]=0;
//verzmid=(zclass[ir]+zclass[jp])/2;
//verxmid=pre[0];
//verymid=pre[1];
//verrhomid=sqrt(pow((DISKX-verxmid),2)+pow((DISKY-verymid),2))*25;
//verVmid=(coeff)*asin(2*A/(sqrt(pow((STIMPOS-verzmid),2)+pow((verrhomid-A),2))+sqrt(pow((STIMPOS-verzmid),2)+pow((verrhomid+A),2))));
//verrhopre=sqrt(pow((DISKX-pre[0]),2)+pow((DISKY-pre[1]),2))*25;
//verVpre=(coeff)*asin(2*A/(sqrt(pow((STIMPOS-zclass[ir]),2)+pow((verrhopre-A),2))+sqrt(pow((STIMPOS-zclass[ir]),2)+pow((verrhopre+A),2))));
//verrhopost=sqrt(pow((DISKX-pre[0]),2)+pow((DISKY-pre[1]),2))*25;
//verVpost=(coeff)*asin(2*A/(sqrt(pow((STIMPOS-zclass[jp]),2)+pow((verrhopost-A),2))+sqrt(pow((STIMPOS-zclass[jp]),2)+pow((verrhopost+A),2))));
//verseglength=sqrt(pow((zclass[jp]-zclass[ir]),2));
///* verdelsquaredV in mV */
//verdelsquaredV = (verVpre+verVpost-2*verVmid)*1000;
//if (verdelsquaredV > THRESHD2V)
// veraxonflagpos[nl]=1;
//if (-verdelsquaredV > THRESHD2V)
// veraxonflagneg[nl]=1;
// Vertical axon segment (IS Version)
veraxonflagpos[nl]=0;
veraxonflagneg[nl]=0;
verzmid=(2*zclass[ir]-2*ISLENGTH)/2;
verxmid=pre[0];
verymid=pre[1];
verrhomid=sqrt(pow((DISKX-verxmid),2)+pow((DISKY-verymid),2))*25;
verVmid=(coeff)*asin(2*A/(sqrt(pow((STIMPOS-verzmid),2)+pow((verrhomid-A),2))+sqrt(pow((STIMPOS-verzmid),2)+pow((verrhomid+A),2))));
verrhopre=sqrt(pow((DISKX-pre[0]),2)+pow((DISKY-pre[1]),2))*25;
verVpre=(coeff)*asin(2*A/(sqrt(pow((STIMPOS-zclass[ir]),2)+pow((verrhopre-A),2))+sqrt(pow((STIMPOS-zclass[ir]),2)+pow((verrhopre+A),2))));
verrhopost=sqrt(pow((DISKX-pre[0]),2)+pow((DISKY-pre[1]),2))*25;
verVpost=(coeff)*asin(2*A/(sqrt(pow((STIMPOS-zclass[ir]+2*ISLENGTH),2)+pow((verrhopost-A),2))+sqrt(pow((STIMPOS-zclass[ir]+2*ISLENGTH),2)+pow((verrhopost+A),2))));
verseglength=2*ISLENGTH; //sqrt(pow((zclass[jp]-zclass[ir]),2));
/* verdelsquaredV in mV */
verdelsquaredV = (verVpre+verVpost-2*verVmid)*1000;
if (verdelsquaredV > THRESHD2V)
veraxonflagpos[nl]=1;
if (-verdelsquaredV > THRESHD2V)
veraxonflagneg[nl]=1;
/* if (-verdelsquaredV > THRESHD2V){
currentinjsum+=(-verdelsquaredV)*Gi;
synapsesum=synapsesum+1;
} */
//if ( (horaxonflagpos[nl]==1) || (veraxonflagpos[nl]==1) )
if ( veraxonflagpos[nl]==1 )
axonflag[nl]=1;
//if ( (horaxonflagneg[nl]==1) || (veraxonflagneg[nl]==1) )
if ( veraxonflagneg[nl]==1 )
axonflag[nl]=2;
ns+=zapisz2(nr,tab[nl],jp,ir,dist[nl],outputfile,graphfile,axflagfile,axonflag[nl]);
/* printf("\npostindex=%d, postclass=%d, preindex=%d, preclass=%d",nr,ir+1,tab[nl],jp+1);*/ /* network linkage checks */
// tab[nl]=-1;
}
} /* koniec petli losowania */
} /* jp */
}
}
fclose(outputfile);
fclose(graphfile);
fclose(axflagfile);
// avecurrentinj=currentinjsum/synapsesum;
//printf("currentinjsum=%f,avecurrentinj=%f,synapsesum=%i\n",currentinjsum,avecurrentinj,synapsesum); /* network linkage checks */
printf("\n");
return 0;
} // koniec main()
//sort_delay - uzywana w qsort() przy porzadkowaniu delay'ow
int sort_delay(const void * a, const void * b )
{
char delay1[1],delay2[1];
struct SYN_TMP *syn1=NULL, *syn2=NULL;
syn1=(struct SYN_TMP *)a;
syn2=(struct SYN_TMP *)b;
*delay1=syn1->delay;
*delay2=syn2->delay;
return( strcmp(delay1,delay2) );
}
/*
sort_neu_num - uzywana w qsort()
*/
int sort_neu_num(const void * a, const void * b)
{
NEURON_INDEX neu1,neu2;
struct SYN_TMP *syn_tmp1=NULL,*syn_tmp2=NULL;
syn_tmp1=(struct SYN_TMP *)a;
syn_tmp2=(struct SYN_TMP *)b;
neu1=syn_tmp1->neuron_pre;
neu2=syn_tmp2->neuron_pre;
return(neu1-neu2);
}
/*
4. Szczegoly generacji tablicy otoczenia:
*/
int rozrzut(int sd)
{
int los;
los=randomm(100);
if(los < 50 )
return( -1*randomm(sd) );
else
return( randomm(sd) );
}
int losuj(int *tablica,int index)
{
int i,j;
do
{
i=randomm(index);
}while(tablica[i]<0);
j=tablica[i];
tablica[i]=-1;
return j;
}
int losuj2(int *tablica, double *distance, double *costht,double *sintht,int index, double Ap, double lambda, int classpost, int classpre,int *precoord)
/* zwraca index w tab, a nie numer neuronu jak losuj() */
{
int i,j,flagspatial;
//double Ap=1.0;
//double lambda=6;
double los,losang,los1,los2,cutoff;
double p3nearrad,p3farave,p3spotrad;
double stripsize,preyy,postycoord,prefloor,postfloor;
double precheck,postcheck;
// i=randomm(index);
do
{
i=randomm(index);
// printf("i=%d,tablica[i]=%d,index=%d\n",i,tablica[i],index); /* network linkage checks */
}while(tablica[i]<0);
switch(classpre)
{
case 0: /* Pre - Layer II/III Pyramid */
/* Patch Handling */
p3nearrad=PIIINEARRAD/SPATIALSCALE;
stripsize=STRIPWIDTH/SPATIALSCALE;
//p3farave=PIIIFARAVE/SPATIALSCALE;
//p3spotrad=PIIISPOTRAD/SPATIALSCALE;
los=drand48();
// los2= Ap*exp(-1.0*distance[i]/lambda);
//losang=drand48();
// los2=1;
flagspatial=0;
if (distance[i]<=p3nearrad)
flagspatial=1;
else {
/* Stripflag A=even, B=odd */
preyy=precoord[1];
postycoord=posty[i];
prefloor=floor(preyy/stripsize);
postfloor=floor(postycoord/stripsize);
precheck=prefloor-2*floor(prefloor/2.0);
postcheck=postfloor-2*floor(postfloor/2.0);
//printf("precheck=%f,postcheck=%f,preyy=%f\n",precheck,postcheck,preyy);
if ( precheck == postcheck)
flagspatial=0;
else
flagspatial=0;
//printf("precheck=%f,postcheck=%f,flagspatial=%d\n",precheck,postcheck,flagspatial);
}
/* Angular isotropy routine */
//los1=(costht[i]*cos(PIIIANG)+sintht[i]*sin(PIIIANG));
//los1*=los1;
//los1*=los1;
//los1*=los1;
/*printf("p3nearrad=%lf,p3farave=%lf,p3spotrad=%lf,flagspatial=%i,distance=%lf\n",p3nearrad,p3farave,p3spotrad,flagspatial,distance[i]);*/
if( ( flagspatial ) /*&& (losang < los1)*/ )
{
return i;
}
else
{
// tablica[i]=-1;
return -1;
}
break;
//los=drand48();
//// los2= Ap*exp(-1.0*distance[i]/lambda);
//los2=1;
////printf("index=%i, neuron=%i, distance=%lf, drand48=%3.2lf, prob= %3.2lf\n",i,j,distance[i],los,los2);
//if( los < los2 )
//{
// return i;
//}
//else
//{
// // tablica[i]=-1;
// return -1;
//}
//break;
case 1: /* Pre - Layer IV Spiny */
los=drand48();
// los2= Ap*exp(-1.0*distance[i]/lambda);
los2=1;
//printf("index=%i, neuron=%i, distance=%lf, drand48=%3.2lf, prob= %3.2lf\n",i,j,distance[i],los,los2);
if( los < los2 )
{
return i;
}
else
{
// tablica[i]=-1;
return -1;
}
break;
case 2: /* Pre - Layer V Pyramid */
los=drand48();
// los2= Ap*exp(-1.0*distance[i]/lambda);
//losang=drand48();
los2=1;
//los1=(costht[i]*cos(PVANG)+sintht[i]*sin(PVANG));
//los1*=los1;
//los1*=los1;
//los1*=los1;
//printf("index=%i, neuron=%i, distance=%lf, drand48=%3.2lf, prob= %3.2lf\n",i,j,distance[i],los,los2);
if( ( los < los2 ) /* && (losang < los1) */ )
{
return i;
}
else
{
// tablica[i]=-1;
return -1;
}
break;
case 3: /* Pre - Layer VI Pyramid */
los=drand48();
// los2= Ap*exp(-1.0*distance[i]/lambda);
//losang=drand48();
los2=1;
//los1=(costht[i]*cos(PVIANG)+sintht[i]*sin(PVIANG));
//los1*=los1;
//los1*=los1;
//los1*=los1;
//printf("index=%i, neuron=%i, distance=%lf, drand48=%3.2lf, prob= %3.2lf\n",i,j,distance[i],los,los2);
if( ( los < los2 ) /* && (losang < los1) */ )
{
return i;
}
else
{
// tablica[i]=-1;
return -1;
}
break;
case 4: /* Pre - Basket Cell */
cutoff = BASKETNEAR/SPATIALSCALE;
if ( distance[i] <= cutoff ){ /* Isotropic connections to 50 microns */
return i;
}
else {
los=drand48();
//losang=drand48();
lambda = BASKETLAMBDA/SPATIALSCALE;
los2= Ap*exp(-1.0*distance[i]/lambda);
//los1=(costht[i]*cos(BASKETANG)+sintht[i]*sin(BASKETANG));
//los1*=los1;
//los1*=los1;
//los1*=los1;
// los2=1;
/* printf("index=%i, neuron=%i, distance=%lf, drand48=%3.2lf, prob= %3.2lf\n",i,j,distance[i],los,los2); */
if( (los < los2) /* && (losang < los1) */ )
{
/* printf("index=%i, neuron=%i, costht=%lf, drand48=%3.2lf, prob= %3.2lf\n",i,j,costht[i],losang,los1);*/
return i;
}
else
{
// tablica[i]=-1;
return -1;
}
}
break;
case 5: /* Pre - Double Bouquet */
los=drand48();
// los2= Ap*exp(-1.0*distance[i]/lambda);
los2=1;
//printf("index=%i, neuron=%i, distance=%lf, drand48=%3.2lf, prob= %3.2lf\n",i,j,distance[i],los,los2);
if( los < los2 )
{
return i;
}
else
{
// tablica[i]=-1;
return -1;
}
break;
case 6: /* Pre - Chandelier Cell */
los=drand48();
// los2= Ap*exp(-1.0*distance[i]/lambda);
los2=1;
//printf("index=%i, neuron=%i, distance=%lf, drand48=%3.2lf, prob= %3.2lf\n",i,j,distance[i],los,los2);
if( los < los2 )
{
return i;
}
else
{
// tablica[i]=-1;
return -1;
}
break;
default:
fprintf(stderr,"Problem with neuron class in losuj2 %d\n",classpre);
exit(0);
}
}
int zapisz(int inr, int inl, int i, int j, FILE * outputfile)
{
struct SYN_TMP syn_tmp;
extern int rozrzut(int);
syn_tmp.neuron_post=inl;
syn_tmp.neuron_pre=inr;
syn_tmp.weight=w[i][j]+rozrzut(sdw[i][j]);
syn_tmp.delay =d[i][j]+rozrzut(sdd[i][j]);
//printf("%i %i %i %i\n",syn_tmp.neuron_pre, syn_tmp.neuron_post , syn_tmp.weight,syn_tmp.delay );
return(fwrite(&syn_tmp,sizeof(struct SYN_TMP),1,outputfile));
}
int zapisz2(int inr, int inl, int i, int j, double distance, FILE * outputfile, FILE * graphfile, FILE * axflagfile, int axflag)
{
struct SYN_TMP syn_tmp;
extern int rozrzut(int);
int axonal_del=5; /* was 5 */
syn_tmp.neuron_post=inl;
syn_tmp.neuron_pre=inr;
syn_tmp.preclass_num=j;
syn_tmp.weight=w[i][j]+rozrzut(sdw[i][j]);
syn_tmp.delay =d[i][j]+rozrzut(sdd[i][j]);
//printf("distance=%3.2lf, axonal del=%3.2lf, delay=%i, delay + axonal del=%i\n", distance, distance*axonal_del, syn_tmp.delay, syn_tmp.delay+(int)rint(distance*axonal_del));
syn_tmp.axdelay=(int)rint(distance*axonal_del)+3*rozrzut(sdd[i][j]);
// printf("%i %i %i %i %i\n",syn_tmp.neuron_pre, syn_tmp.neuron_post , syn_tmp.weight, syn_tmp.delay , syn_tmp.axdelay);
if ((j)==4){
fwrite(&inr,sizeof(ushort),1,graphfile);
fwrite(&j,sizeof(ushort),1,graphfile);
fwrite(&inl,sizeof(ushort),1,graphfile);
fwrite(&i,sizeof(ushort),1,graphfile);
}
/* printf("axflag= %i\n",axflag); */
fwrite(&axflag,sizeof(ushort),1,axflagfile);
return(fwrite(&syn_tmp,sizeof(struct SYN_TMP),1,outputfile));
}
int otoczenie(int nr ,int ir,int xyz[3],int jp)
{
int nx,ny,no,i[3],ix,iy,iz,mini[3],maxi[3], *ot;
int j;
/* tab must be declared earlier with sufficient size */
ot = o[jp][ir];
/* obliczamy najpierw wspolrzedne i[3] neuronu na siatce jp, ktory jest najblizej x,y,z */ /*???*/
no=0;
for(j=0;j<3;j++)
i[j]=xyz[j]/k[jp][j];
/* teraz otoczenie
wersja bez zawijania (brzegowe maja mniejsze otoczenie) */ /*???*/
for(j=0;j<3;j++)
{
mini[j]=i[j]-ot[j];
maxi[j]=i[j]+ot[j]+1;
if(mini[j]<0)mini[j]=0;
if(maxi[j]>N[jp][j])maxi[j]=N[jp][j];
}
for(ix=mini[0];ix<maxi[0];ix++)
{ nx=ix*N[jp][1]*N[jp][2]+NOF[jp];
for(iy=mini[1];iy<maxi[1];iy++)
{ ny=nx+iy*N[jp][2];
for(iz=mini[2];iz<maxi[2];iz++)
{
if((j=ny+iz)!=nr){
dist[no]=sqrt((ix-i[0])*(ix-i[0])+(iy-i[1])*(iy-i[1])+(iz-i[2])*(iz-i[2]));
costheta[no]=(ix-i[0])/dist[no];
sintheta[no]=(iy-i[1])/dist[no];
/* printf("ix=%i, iy=%i, i[0]=%i,i[1]=%i,costheta=%lf,sintheta=%lf,dist=%lf\n",ix,iy,i[0],i[1],costheta[no],sintheta[no],dist[no]);*/
// printf("%6.1lf,",dist[no]); /* tablica z odleglosciami - nowa wersja 01/29/03 */
postx[no]=ix;
posty[no]=iy;
tab[no++]=j;}
}
}
}
//printf("\n\n");
return no;
}
int otoczenie2(int nr, int ir,int xyz[3],int jp)
{
int nx,ny,no,i[3],ix,iy,iz,mini[3],maxi[3], *ot;
int j,ii,NX,NY,NZ;
/* tab must be declared earlier with sufficient size */
ot = o[jp][ir];
/* obliczamy najpierw wspolrzedne i[3] neuronu na siatce jp, ktory jest najblizej x,y,z */ /* ??? */
no=0;
for(j=0;j<3;j++)
i[j]=xyz[j]/k[jp][j];
/* Torus case */
for(j=0;j<3;j++)
{
mini[j]=i[j]-ot[j];
maxi[j]=i[j]+ot[j]+1;
}
NX=N[jp][0];
NY=N[jp][1];
NZ=N[jp][2];
for(ix=mini[0];ix<maxi[0];ix++)
{ ii=(ix+NX)%NX;
nx=ii*NZ*NY+NOF[jp];
for(iy=mini[1];iy<maxi[1];iy++)
{ ii=(iy+NY)%NY;
ny=nx+ii*NZ;
for(iz=mini[2];iz<maxi[2];iz++)
{
if((j=ny+(iz+NZ)%NZ)!=nr)
tab[no++]=j;
}
}
}
return no;
}
void set_neu(struct NEURON * neu)
{
int j,kk;
switch(neu->param)
{
default:
neu->flags|=HISTO;
neu->polarity = 0x0;
for (kk=0;kk<NEXCCLASS;kk++){
neu->Ve_o[kk]=0;
neu->Ve_d[kk]=0;
}
for (kk=0;kk<NINHCLASS;kk++){
neu->Vi_o[kk]=0;
neu->Vi_d[kk]=0;
}
neu->interval=0;
neu->sum_interval=0;
for(j=0;j<SPIKE_LIST_LENGTH;j++)
neu->spikes[j]=0;
neu->first=EMPTY_LIST;
neu->last=0x1;
neu->V=V_0;
neu->W=W_0;
#ifdef CALCIUM
for(j=1;j<7;j++)
{
neu->C[j]=0.;
neu->U[j]=0.;
}
// neu->C=C_0;
neu->X=X_0;
neu->B=B_0;
#endif
}
}
unsigned seed_save()
{
unsigned seed;
seed = (unsigned)time(NULL);
srandom(seed);
return seed;
}