source: experiments/frams/foraminifera/data/scripts/foraminifera.expdef @ 588

Last change on this file since 588 was 588, checked in by oriona, 8 years ago

Movement of forams in z axis restricted.

File size: 31.5 KB
Line 
1expdef:
2name:Reproduction of benthic foraminifera
3info:~
4Basic information about this simulation:
5www.framsticks.com/foraminifera
6
7Technical information:
8Genes and parameter values which control reproduction are stored in data->genes and data->lifeparams fields.
9
10genes:
11genes which are not encoded in Ff genotype:
12min_repro_energy - Minimum energy necessary for reproduction
13hibernation - Defines foram behavior in the case of no nutrients
14
15lifeparams:
16Physiological parameters of foraminifera:
17max_energy_level - maximum energy level reached so far
18gen - generation: 0 haploid, 1 diploid
19species - species: 0 not hibernating 1 hibernating
20hibernated - 0/1 foram is/isn't hibernated
21reproduce - 0/1 foram isn't/is ready for reproduction
22~
23code:~
24
25global colors;
26global curColor;
27global max_chamber_volume;
28global movePerStep;
29global nutrientenergywaiting;
30global reprocounter;
31global changePeriod;
32global phase;
33global nutrientSqrCm;
34global species_genes;
35global max_chambers_def;
36global foram_uid; //introduced because each replacement of a creature (while growing) would generate a new Creature.uid
37global chamber_vis_denominator;
38global curRadius;
39
40@include "foraminifera.inc"
41
42// -------------------------------- experiment begin --------------------------------
43
44function onExpDefLoad()
45{
46        // define genotype and creature groups
47        GenePools.clear();
48        Populations.clear();
49        GenePools[0].name = "Unused";
50
51        var pop = Populations[0];
52        pop.name = "Forams";
53        pop.en_assim = 0;
54        pop.nnsim = 0;
55        pop.enableperf = 1;
56        pop.death = 1;
57        pop.energy = 1;
58        pop.selfmask = 0;
59        pop.othermask = 0;
60        //pop.selfmask = 0x20002; pop.othermask = 0x10002;
61        pop.perfperiod = 25;
62        pop.bodysim = 0;
63
64        pop = Populations.addGroup("Nutrients");
65        pop.nnsim = 0;
66        pop.enableperf = 0;
67        pop.death = 1;
68        pop.energy = 1;
69        pop.selfmask = 0;
70        pop.othermask = 0;
71        //pop.othermask = 0x10002;
72        pop.bodysim = 0;
73
74        pop = Populations.addGroup("ReticulopodiaNutrients");
75        pop.nnsim = 0;
76        pop.enableperf = 0;
77        pop.death = 0;
78        pop.energy = 0;
79        pop.selfmask = 0;
80        pop.othermask = 0;
81        pop.bodysim = 0;
82
83        //world
84        SignalView.mode = 1;
85        World.wrldwat = 200;
86        World.wrldsiz = micronsToFrams(100000);
87        World.wrldbnd = 1;
88
89        //ExpProperties.visualize = 1; //uncomment to visualize reticulopodia and indicate nutrients positions
90
91        //ExpProperties.logging = 1; //uncomment to enable logging simulation parameters to log files   
92        ExpProperties.logPref = "";
93
94        //morphology
95        ExpProperties.zone1_range = micronsToFrams(1000);
96        ExpProperties.zone2_range = micronsToFrams(3000);
97        ExpProperties.chamber_proculus_haplo = micronsToFrams(20);
98        ExpProperties.chamber_proculus_diplo = micronsToFrams(10);
99        colors = ["1.0,1.0,0.3","1.0,0.7,0.0"]; //yellow and orange
100        curColor = colors[0];
101        curRadius = ExpProperties.zone1_range;
102
103        //nutrients
104        ExpProperties.nutrientradius = micronsToFrams(10);
105        ExpProperties.energy_nut = 200 * energyFromVolume(ExpProperties.nutrientradius,1);
106        nutrientSqrCm = 10;
107        ExpProperties.nutrient_pop = Math.pow(framsToMicrons(World.wrldsiz)*0.0001,2)/nutrientSqrCm;
108
109        ExpState.totaltestedcr = 0;
110        ExpState.nutrient = "";
111
112        max_chambers_def = 35;
113        chamber_vis_denominator = 12;
114        //addSpecies({"min_repro_energies" : [4,6]});
115        //addSpecies({"min_repro_energies" : [4,8]});
116       
117        //Simulator.print(create_genotype(0.2, 30, "1.0,1.0,0.0", 0.6)); //sample call
118        //Simulator.print(create_genotype(0.1, 40, "1.0,0.5,0.0", 0.1)); //sample call
119}
120
121@include "standard_placement.inc"
122
123function onExpInit()
124{
125        species_genes = [];
126        foram_uid = 0;
127        movePerStep = getMovePerStep();
128
129        Populations[0].clear();
130        Populations[1].clear();
131        Populations[2].clear(); //reticulopodia and nutrients
132
133        if (ExpProperties.max_chamber_num_haplo == max_chambers_def && ExpProperties.max_chamber_num_diplo == max_chambers_def)
134        {
135                max_chamber_volume = [[30403.5869594578,52812.2546633948,79578.5148482541,113588.815134453,154620.677376218,205094.322220826,262572.712174265,326078.453295303,402342.518962956,498133.985678766,615066.864740109,759500.497626816,937064.025544282,1155915.25563075,1429139.14079748,1762487.92940157,2176286.62046069,2685795.63187845,3316190.12127043,4096436.04462706,5051343.25226193,6231980.1061213,7687880.79524734,9485307.02904958,11716968.9852569,14459866.4934433,17836388.9853663,22004935.7247348,27138607.2546045,33482425.1582986,41336775.1280297,50997910.7842793,62888631.7871037,77563060.9243464,95659468.591964]
136, [3430.07716920763,6159.93090353532,9322.94192815286,13462.9896597283,18399.8550832969,24558.9218126892,31468.8148639192,39189.4977865513,48404.4292075836,60185.8639086061,74490.6048472854,92117.8178412275,113852.779747083,140714.366929552,174450.937643841,215250.242147183,266323.295274072,328858.042352538,406552.379957238,503526.321155323,621060.781025019,767240.824049468,947210.683224091,1169506.19906112,1447211.61255879,1787155.29073739,2205627.64766244,2723413.2837305,3360233.53738709,4147771.02835393,5126445.06973928,6328060.3331703,7805693.278958,9631924.72156452,11884287.1596814]];
137        }
138
139        else
140        {
141                max_chamber_volume = [Vector.new(), Vector.new()];
142                var density = 100;
143                for (var ploid = 0; ploid < 2; ploid++)
144                {
145                        var rad = getPloidRadius(ploid);
146                        for (var cham_num = 0; cham_num < getProperty(ploid,"max_chamber_num"); cham_num++)
147                        {
148                                max_chamber_volume[ploid].add(volumeFromGeno(ploid, rad, cham_num+1, density));
149                        }                                 
150                }
151        }
152
153        if (species_genes.size == 0)
154        {
155                addSpecies({}); //default
156        }
157
158        for (var spec = 0; spec < species_genes.size; spec++)
159        {
160                for (var i = 0; i < ExpProperties.foramPop; i++)
161                {
162                        addInitialForam(spec, i);       
163                }
164        }
165        ExpState.totaltestedcr = 0;
166
167        reprocounter = 0;
168        nutrientenergywaiting = 0;
169        changePeriod = 0;
170        phase = "low";
171}
172
173function onExpLoad()
174{
175        for (var pop in Populations)
176                pop.clear();
177
178        Loader.addClass(sim_params.*);
179        Loader.setBreakLabel(Loader.BeforeUnknown, "onExpLoad_Unknown");
180        Loader.run();
181
182        Simulator.print("Loaded " + Populations[0].size + " Forams and " + Populations[1].size + " nutrient objects");
183}
184
185function onExpLoad_Unknown()
186{
187        if (Loader.objectName == "org") // saved by the old expdef
188        {
189                var g = Genotype.newFromString("");
190                Loader.currentObject = g;
191                Interface.makeFrom(g).setAllDefault();
192                Loader.loadObject();
193                var cr = Populations[0].add(g);
194                if (cr != null)
195                {
196                        //cr.rotate(0,0,Math.rnd01*Math.twopi);
197                        if ((typeof(g.data->genes) == "Vector") && (g.data->genes.size >= 3))
198                        {
199                                // [x,y,energy]
200                                cr.move(g.data->genes[0] - cr.center_x, g.data->genes[1] - cr.center_y, 0);
201                                cr.energy = g.data->genes[2];
202                        }
203                        else
204                        {
205                                cr.move(Math.rnd01 * World.wrldsiz - cr.center_x, Math.rnd01 * World.wrldsiz - cr.center_y, 0);
206                        }
207                }
208        }
209        else if (Loader.objectName == "Creature")
210        {
211                Loader.currentObject = CreatureSnapshot.new();
212                Loader.loadObject();
213                Populations[0].add(Loader.currentObject);
214        }
215}
216
217function onExpSave()
218{
219        File.writeComment("saved by '%s.expdef'" % Simulator.expdef);
220
221        var tmpvec = [], i;
222
223        for(var cr in Populations[1])
224                tmpvec.add([cr.center_x, cr.center_y, cr.energy]);
225
226        ExpState.nutrient = tmpvec;
227        File.writeObject(sim_params.*);
228        ExpState.nutrient = null; //vectors are only created for saving and then discarded
229
230        for (var cr in Populations[0])
231                File.writeObject(cr);
232}
233
234// -------------------------------- experiment end --------------------------------
235
236function volumeFromGeno(morphotype, rad, chamber_num, density)
237{
238        var geno = create_genotype(rad, chamber_num, colors[morphotype], 1);
239        var m=Model.newFromString(geno);
240        var mg=ModelGeometry.forModel(m);
241        mg.geom_density=density;
242        var volumeInFrams = mg.volume();
243
244        return volumeInFrams/Math.pow(ExpProperties.scalingFactor,3);
245}
246
247function secToSimSteps(value_in_sec){
248        return value_in_sec/ExpProperties.secPerStep;
249}
250
251function volumeInMicrons(radiusInFrams)
252{
253        return 4.0/3.0*Math.pi*Math.pow(framsToMicrons(radiusInFrams),3);
254}
255
256function energyFromVolume(base, isRadiusInFrams)
257{
258        if (isRadiusInFrams == 1) //radius in frams
259        {
260                return ExpProperties.picoCarbonPerMikro*volumeInMicrons(base);
261        }
262        else //volume in microns
263        {
264                return ExpProperties.picoCarbonPerMikro * base;
265        }
266}
267
268function getMovePerStep()
269{
270        return micronsToFrams((ExpProperties.foramSpeedMmPerMin/60)*1000)*ExpProperties.secPerStep;
271}
272
273function micronsToFrams(micrometers)
274{
275        return micrometers*ExpProperties.scalingFactor;
276}
277
278function framsToMicrons(framsworldunits)
279{
280        return framsworldunits/ExpProperties.scalingFactor;
281}
282
283function getProperty(gen, prop_id)
284{
285        var ploid = "haplo";
286        if (gen == 1) ploid = "diplo";
287        return ExpProperties.[prop_id + "_" + ploid];
288}
289
290function getGene(cr, gen_id, gen_set)
291{
292        if (cr.data->lifeparams->gen == 0)
293                return cr.data->genes[gen_id];
294        else
295                return cr.data->genes[gen_set][gen_id];
296}
297
298function getPloidRadius(ploid)
299{
300        var radius = ExpProperties.chamber_proculus_haplo;
301                if (ploid == 1)
302                {
303                        radius = ExpProperties.chamber_proculus_diplo;
304                }
305        return radius;
306}
307
308function chamberNumFromEnergy(energy, ploid)
309{
310        var chamber_num = max_chamber_volume[ploid].size;
311        for (var i = 0; i < chamber_num; i++)
312        {
313                if (energy < energyFromVolume(max_chamber_volume[ploid][i],0))
314                {
315                        chamber_num = i+1;
316                        break;
317                }       
318        }                                 
319       
320        return chamber_num;
321}
322
323function addForam(species, iter, start_energy, ploid)
324{
325        var chambernum =  chamberNumFromEnergy(start_energy, ploid);
326        var radius = getPloidRadius(ploid);
327        var geno = create_genotype(radius, chambernum, colors[ploid], 1);
328        curColor = colors[ploid];
329        var cr = Populations[0].add(geno);
330        cr.name = "Initial creature" + species + "_" + iter;
331        placeRandomlyNotColliding(cr);
332        cr.energy = start_energy;
333        setGenotype({"opt" : "birth", "cr" : cr, "gen" : ploid, "species" : species, "energy0" : cr.energy, "genes" : species_genes[species], "parentsuids" : ["c0"]});
334        if (ploid == 1)
335        {
336                cr.data->genes = [cr.data->genes, cr.data->genes]; //TODO two different genes sets
337        }
338        moveReticulopodia(cr);
339}
340
341function addInitialForam(species, iter)
342{
343        var ploid = 0;
344        if (Math.rnd01 > 0.5)
345        {
346                ploid = 1;
347        }       
348        //add new foram with random energy bewtween starting energy and reproduction threshold
349        var repro_thr = species_genes[species]->min_repro_energies[ploid];
350        var start_energy = Math.rndUni(energyFromVolume(getPloidRadius(ploid),1),repro_thr-0.25*repro_thr);
351        addForam(species, iter, start_energy,ploid);
352}
353
354//new species can be added as a dictionary with parameter values that are different than default values
355function addSpecies(new_genes)
356{
357        species_genes.add({"min_repro_energies" : [ExpProperties.min_repro_energ_haplo,ExpProperties.min_repro_energ_diplo], "energies0" : [ExpProperties.energies0_haplo, ExpProperties.energies0_diplo], "hibernation" : 0, "morphotype" : 0});
358        for (var i = 0; i < new_genes.size; i++)
359        {
360                var key = new_genes.getKey(i);
361                species_genes[species_genes.size-1][key] = new_genes[key];
362        }
363}
364
365// -------------------------------- foram begin -----------------------------------
366
367function setForamMeta(cr)
368{
369        //percent of current energy
370        cr.idleen = (ExpProperties.e_meta * cr.energy)*ExpProperties.secPerStep;
371}
372
373function lastChamberNum(cr)
374{
375        return cr.numparts;
376}
377
378function getZoneRange(cr, zone_num)
379{
380        return ExpProperties.["zone"+zone_num+"_range"];
381}
382
383function addReticulopodia(cr, radius)
384{
385                if (reticulopodiaExists(cr))
386                {
387                        Populations[2].delete(cr.data->reticulopodiacreature);
388                }
389                var ret = Populations[2].add("//0\nm:Vstyle=reticulopodia\np:sh=1,sx=0.001,sy=0.001,sz=0.001\np:sh=3,sx=0.01,sy="+radius+",sz="+radius+",ry=1.57079633,vr="+curColor+"\nj:0, 1, sh=1");
390                cr.data->reticulopodiacreature = ret;
391                ret.getMechPart(0).orient.set(cr.getMechPart(0).orient);
392                ret.moveAbs(cr.center_x-radius, cr.center_y-radius, cr.center_z-radius);
393}
394
395function onForamsBorn(cr)
396{
397        setForamMeta(cr);
398        if (ExpProperties.visualize == 1)
399        {
400                addReticulopodia(cr, curRadius);
401                moveReticulopodia(cr);
402        }
403}
404
405function placeRandomlyNotColliding(cr)
406{
407        var retry = 100; //try 100 times
408        while (retry--)
409        {
410                placeCreatureRandomly(cr, 0, 0);
411                if (!cr.boundingBoxCollisions(0))
412                {
413                        cr.moveAbs(cr.pos_x, cr.pos_y, -cr.getPart(cr.numparts-1).sx); //place slightly under the bottom surface ("z" value depends on the size of the last=largest chamber)
414                        return cr;
415                }
416        }
417
418        Populations[0].delete(cr);
419}
420
421function reticulopodiaExists(cr)
422{
423        var has_ret = 0;
424
425        if (cr.data->reticulopodiacreature != null)
426        {
427                if (Populations[2].findUID(cr.data->reticulopodiacreature.uid) != null)
428                {
429                        has_ret = 1;
430                }
431        }
432
433        return has_ret;
434}
435
436function visualization(cr)
437{
438        return reticulopodiaExists(cr);
439}
440
441function foramGrow(cr, chamber_num, lastchambergrowth)
442{
443        if ((chamber_num+1) <= max_chamber_volume[cr.data->lifeparams->gen].size)
444        {
445                curColor = colors[cr.data->lifeparams->gen];
446                var ploid = cr.data->lifeparams->gen;
447                var geno = create_genotype(getPloidRadius(ploid), chamber_num+1, colors[ploid], lastchambergrowth);
448                var cr2 = Populations[0].add(geno);
449
450                cr2.energy0 = cr.energy;
451                cr2.energy = cr2.energy0;
452
453                setGenotype({"cr" : cr2, "parent_genes" : cr.data->genes, "parent_lifeparams" : cr.data->lifeparams, "opt" : "growth", "energy0" : cr.energy0});
454                cr2.moveAbs(cr.pos_x, cr.pos_y,cr.pos_z);
455                setForamMeta(cr2);
456
457                if (reticulopodiaExists(cr))
458                {
459                        Populations[2].delete(cr.data->reticulopodiacreature);
460                }
461                Populations[0].delete(cr);
462                return cr2;
463        }
464        return cr;
465}
466
467function visualizeChamberGrowth(cr, chamber_time)
468{
469        var total_time = secToSimSteps(ExpProperties.chamberGrowthSec);
470        var ret_unit = total_time/chamber_vis_denominator;
471        var chamber_unit = total_time-ret_unit;
472
473        if (chamber_time < ret_unit || chamber_time >= chamber_unit)
474        {
475                var new_rad = Math.min(Math.max((chamber_time%ret_unit)/ret_unit*getZoneRange(cr,1),0.01),getZoneRange(cr,1));
476
477                if(chamber_time < ret_unit)
478                {
479                        new_rad = getZoneRange(cr,1)-new_rad;
480                }
481
482                curColor = colors[cr.data->lifeparams->gen];
483                addReticulopodia(cr,new_rad);
484
485                if (chamber_time == 0)//checking for end of chamber growth process
486                {
487                        cr.data->lifeparams->chamber_growth = -1;       
488                }
489        }
490        else
491        {
492                var new_rad = 1 - Math.min(Math.max((chamber_time-ret_unit)/chamber_unit,0.01),1);
493                curRadius = cr.data->reticulopodiacreature.getPart(1).sy;
494                               
495                if (chamber_time == ret_unit)
496                {
497                        new_rad = 1;
498                }
499
500                var new_cr = foramGrow(cr, chamberNumFromEnergy(cr.data->lifeparams->max_energy_level, cr.data->lifeparams->gen)-1, new_rad);
501                curRadius = getZoneRange(new_cr,1);
502        }
503}
504
505function stepToNearest(cr)
506{
507        var p = cr.getMechPart(0);
508        var n = cr.signals.receiveSet("nutrient", getZoneRange(cr,2));
509
510        //if signals are received find the source of the nearest
511        if (n.size > 0)
512        {
513                var i;
514                var mp;
515                var distvec = XYZ.new(0, 0, 0);
516                var dist;
517                var mindist = 100000000000.0;
518                var mindistvec = null;
519                var eating = 0;
520
521                for (i = 0; i < n.size; i++)
522                {
523                        mp = n[i].value.getMechPart(0);
524                        distvec.set(mp.pos);
525                        distvec.sub(p.pos);
526                        dist = distvec.length;
527                        if (dist < getZoneRange(cr,1))
528                        {
529                                if (n[i].value != null)
530                                {
531                                        energyTransfer(cr, n[i].value);
532                                        eating = 1;
533                                }
534                        }
535                        else if (eating == 0 && cr.data->lifeparams->hibernated == 0 && dist < mindist)
536                        {
537                                mindist = dist;
538                                mindistvec = distvec.clone();
539                        }
540                }
541
542                if (!eating && cr.data->lifeparams->hibernated == 0)
543                {
544                        mindistvec.z = 0;
545                        mindistvec.normalize();
546                        mindistvec.scale(-1*movePerStep);
547                        cr.localDrive = mindistvec;
548                        moveEnergyDec(cr);
549                }
550
551                return 1;
552        }
553       
554        else
555        {
556                return 0;
557        }
558}
559
560function moveEnergyDec(cr)
561{
562        if (cr.data->lifeparams->hibernated == 0)
563        {
564                //percent of maximal energy
565                cr.energy -= (ExpProperties.energy_move * cr.data->lifeparams->max_energy_level)*ExpProperties.secPerStep;
566        }
567}
568
569function fence(center, zone)
570{
571        return Math.min(Math.max(0+zone,center),World.wrldsiz-zone); //add and subtract zone from the world size to prevent reticulopodia from crossing the fence
572}
573
574function foramMove(cr)
575{
576        //are there any nutrients in zone 1 or 2?
577        {
578                var moved = stepToNearest(cr); //TODO weighted sum of distance and energy
579                if (moved==1)
580                {
581                        moveReticulopodia(cr);
582                        return;
583                }
584        }
585
586        //Prevents forams from crossing the world border. In the case of touching the border with the reticulopodia direction of the movement should be changed.
587        var change_direction = 0;
588        var new_x = fence(cr.center_x, getZoneRange(cr, 1));
589        var new_y = fence(cr.center_y, getZoneRange(cr, 1));
590
591        if ((new_x != cr.center_x) || (new_y != cr.center_y) || (cr.data->lifeparams->dir_counter >= int(secToSimSteps(ExpProperties.dir_change_sec))))
592        {
593                change_direction = 1;
594                cr.moveAbs(new_x-cr.size_x/2, new_y-cr.size_y/2, -cr.getPart(cr.numparts-1).sx);//place slightly under the bottom surface ("z" value depends on the size of the last=largest chamber)
595        }
596
597        //no nutrients in zone 2
598        if (getGene(cr, "hibernation",0) == 1)
599        {
600                reverseHib(cr);
601                cr.localDrive = XYZ.new(0,0,0);
602        }
603        //random move
604        else if (change_direction == 1)
605        {
606                cr.data->lifeparams->dir = randomDir();
607                cr.data->lifeparams->dir_counter = 0;
608                cr.localDrive = cr.data->lifeparams->dir;
609                moveEnergyDec(cr);
610        }
611        else
612        {
613                cr.localDrive = cr.data->lifeparams->dir;
614        }
615        moveReticulopodia(cr);
616}
617
618function moveReticulopodia(cr)
619{
620        if (visualization(cr))
621        {
622                cr.data->reticulopodiacreature.moveAbs(cr.center_x-getZoneRange(cr,1), cr.center_y-getZoneRange(cr,1), cr.center_z-getZoneRange(cr,1));
623                cr.data->reticulopodiacreature.localDrive = cr.localDrive;
624        }
625}
626
627function randomDir()
628{
629        var dir = (Math.rndUni(-ExpProperties.zone2_range, ExpProperties.zone2_range), Math.rndUni(-ExpProperties.zone2_range, ExpProperties.zone2_range), 0); 
630        dir.normalize();
631        dir.scale(-1*movePerStep);
632        return dir;
633}
634
635function energyTransfer(cr1, cr2)
636{
637        cr1.localDrive = XYZ.new(0,0,0);
638        var e =  ExpProperties.feedtrans*cr1.energy*ExpProperties.secPerStep; //TODO efficiency dependent on age
639        //Simulator.print("transferring "+e +"("+e*ExpProperties.ingestion+")"+" to "+cr1.name +" ("+ cr1.energy+") " +" from "+cr2.uid+" ("+cr2.energy+") "+ e/ExpProperties.secPerStep+ " per sec");
640        var transferred = cr2.transferEnergyTo(cr1, e);
641        cr1.energy -= transferred*(1-ExpProperties.ingestion);
642        if (cr1.data->lifeparams->hibernated == 1)
643        {
644                reverseHib(cr1);
645        }
646}
647
648function reverseHib(cr)
649{
650        if (cr.data->lifeparams->hibernated == 1)
651        {
652                setForamMeta(cr); //unhibernate
653        }
654        else
655        {
656                cr.idleen = (ExpProperties.energy_hib * cr.energy)*ExpProperties.secPerStep; //hibernate
657        }
658        cr.data->lifeparams->hibernated = 1 - cr.data->lifeparams->hibernated;
659}
660
661function onForamsStep(cr)
662{
663        //checking for gametogenesis process
664        if (cr.data->lifeparams->division_time > 0)
665        {
666                cr.data->lifeparams->division_time = Math.max(cr.data->lifeparams->division_time-1,0);
667        }
668        //checking for end of gametogenesis
669        else if (cr.data->lifeparams->division_time == 0)
670        {
671                //waiting for gamets fusion
672        }
673        //checking for chamber growth process
674        else if (cr.data->lifeparams->chamber_growth > 0)
675        {
676                var chamber_time = Math.max(cr.data->lifeparams->chamber_growth-1,0);
677                cr.data->lifeparams->chamber_growth = chamber_time;
678                cr.energy -= ExpProperties.chamberCostPerSec * cr.energy * ExpProperties.secPerStep;
679
680                if (visualization(cr))
681                {
682                        visualizeChamberGrowth(cr, chamber_time);
683                }
684        }
685        //checking for end of the chamber growth process
686        else if (cr.data->lifeparams->chamber_growth == 0 && visualization(cr)==0)
687        {       
688                foramGrow(cr, lastChamberNum(cr), 1);
689                cr.data->lifeparams->chamber_growth = -1;
690                //Simulator.print("chamber "+ (lastChamberNum(cr) + 1) +" complete");
691        }
692        else
693        {
694                //update of metabolism rate
695                if (cr.data->lifeparams->hibernated == 0)
696                {
697                        setForamMeta(cr);
698                }
699
700                if (deathConditions(cr) == 1)
701                {
702                        if (ExpProperties.logging == 1)
703                        {
704                                log(createLogVector(cr, cr.data->lifeparams->max_energy_level),ExpProperties.logPref+"fossil_log.txt");
705                                log(createLogVector(cr, cr.lifespan),ExpProperties.logPref+"lifespan_log.txt");
706                        }                       
707                        Populations[0].kill(cr);
708                        return;
709                }
710
711                //update direction change counter
712                cr.data->lifeparams->dir_counter += 1;
713
714                foramMove(cr);
715
716                var repro = foramReproduce(cr);
717                if (repro == 1)
718                {
719                        return;
720                }
721
722                cr.data->lifeparams->max_energy_level = Math.max(cr.energy, cr.data->lifeparams->max_energy_level);
723
724                //cheking conditions of chamber growth process start
725                if  (lastChamberNum(cr) < max_chamber_volume[cr.data->lifeparams->gen].size)
726                {
727                        if ((cr.data->lifeparams->max_energy_level >= energyFromVolume(max_chamber_volume[cr.data->lifeparams->gen][lastChamberNum(cr)-1],0))) 
728                        {
729                                cr.data->lifeparams->chamber_growth = int(secToSimSteps(ExpProperties.chamberGrowthSec));
730                        }       
731                }
732        }       
733}
734
735function deathConditions(cr)
736{
737        if ((cr.energy <= getProperty(cr.data->lifeparams->gen,"e_death_level")*cr.data->lifeparams->max_energy_level) || (Math.rnd01 < ExpProperties.hunted_prob))
738        {
739                return 1;
740        }
741        else
742                return 0;
743}
744
745function onForamsDied(cr)
746{
747        if (visualization(cr))
748        {
749                Populations[2].delete(cr.data->reticulopodiacreature);
750        }
751        //fossilization
752        var geno = GenePools[0].add(cr.genotype);
753        geno.data->genes = cr.data->genes;
754        geno.data->lifeparams = cr.data->lifeparams;
755        if (ExpProperties.logging == 1) Simulator.print("\"" + cr.name + "\" died...");
756        ExpState.totaltestedcr++;
757}
758
759// --------------------------------foram end -------------------------------------
760
761// -------------------------------- nutrient begin --------------------------------
762
763function createNutrientGenotype(nutrientradius)
764{
765        return "//0\nm:Vstyle=nutrient\np:sh=3,sx="+nutrientradius+",sy="+nutrientradius+",sz="+nutrientradius+",ry=1.57,vr=0.0,1.0,0.0";
766}
767
768function onNutrientsStep(cr)
769{
770        cr.moveAbs(cr.pos_x % World.wrldsiz, cr.pos_y % World.wrldsiz, 0.5);
771}
772
773function addNutrient()
774{
775        var cr = Populations[1].add(createNutrientGenotype(ExpProperties.nutrientradius));
776
777        cr.name = "Nutrients";
778        cr.idleen = 0;
779        cr.energy0 = ExpProperties.energy_nut;
780        cr.energy = cr.energy0;
781        cr.signals.add("nutrient");
782
783        cr.signals[0].value = cr;
784
785        placeCreatureRandomly(cr, 0, 0);
786        if (ExpProperties.visualize == 1)
787        {
788                var nutsize = ExpProperties.nutrientradius*10;
789                var nut = Populations[2].add("//0\nm:Vstyle=nutrient_visual\np:sh=2,sx="+nutsize+",sy="+nutsize+",sz="+nutsize+",ry=1.5,vr=0.0,1.0,0.0");
790                cr.data->reticulopodiacreature = nut;
791                nut.moveAbs( cr.pos_x+cr.size_x/2-nut.size_x/2,  cr.pos_y+cr.size_y/2-nut.size_y/2, -nutsize);
792        }
793}
794
795function onNutrientsDied(cr)
796{
797        if (visualization(cr))
798        {
799                Populations[2].delete(cr.data->reticulopodiacreature);
800        }
801}
802
803function nutrientGrowth()
804{
805        if (ExpProperties.foodPeriodChange > 0)
806        {
807                        changePeriod += 1;
808                        if (phase=="low" && changePeriod >= secToSimSteps(23328000)) //9 months
809                        {
810                                ExpProperties.foodperiod = ExpProperties.foodperiod/ExpProperties.foodPeriodChange;
811                                phase = "high";
812                                changePeriod = 0;
813                        }
814               
815                        else if (phase == "high" && changePeriod >= secToSimSteps(7776000)) //3 months
816                        {
817                                ExpProperties.foodperiod = ExpProperties.foodperiod*ExpProperties.foodPeriodChange;
818                                phase = "low";
819                                changePeriod = 0;
820                        }
821        }
822        nutrientenergywaiting = nutrientenergywaiting + 1;
823        if (nutrientenergywaiting >= secToSimSteps(ExpProperties.foodperiod))
824        {
825                for (var i = 0; i < ExpProperties.nutrient_pop; i++)
826                {   
827                        addNutrient();
828                }
829
830                nutrientenergywaiting = 0.0;
831                Simulator.checkpoint();
832
833                if (ExpProperties.logging == 1)
834                {
835                        log([ExpProperties.nutrient_pop],ExpProperties.logPref+"nutrients_log.txt");
836                }
837        }
838
839}
840
841// -------------------------------- nutrient end --------------------------------
842
843// -------------------------------- step begin --------------------------------
844
845function onStep()
846{
847
848        nutrientGrowth();
849        if (ExpProperties.logging == 1)
850        {
851                createStatistics();
852        }
853
854        //reproduction --------------------------------------------
855        reprocounter += 1;
856        if (reprocounter > secToSimSteps(ExpProperties.reproTimeSec))
857        {
858                reprocounter = 0;
859                for (var s = 0; s < species_genes.size; s++)
860                {
861                        reproduce_parents(s);
862                }
863               
864        }
865
866        //check for extinction -----------------------------------------------
867        if (Populations[0].size == 0)
868        {
869                if (ExpProperties.autorestart)
870                {
871                        Simulator.print("no more creatures, restarting...");
872                        onExpInit();
873                }
874                else
875                {
876                        Simulator.print("no more creatures, stopped.");
877                        Simulator.stop();
878                }
879        }
880        if (ExpProperties.maxSteps > 0)
881        {
882                if (Simulator.stepNumber >= ExpProperties.maxSteps)
883                        Simulator.stop();
884        }
885}
886
887function createStatistics()
888{       
889        var number = [];
890        var e_inc = [];
891        var e_nut = 0.0;
892
893        for (var s = 0; s < species_genes.size; s++)
894        {
895                number.add([0,0]);// [haplo][diplo]
896                e_inc.add([0,0]);
897        }
898
899        for (var i = 0; i < Populations[0].size; i++)
900        {
901                var cr = Populations[0].get(i);
902                var gen = cr.data->lifeparams->gen;
903                var species = cr.data->lifeparams->species;
904
905                number[species][gen] = number[species][gen] + 1;
906                e_inc[species][gen] = e_inc[species][gen] + cr.energy;
907        }
908
909        for (var i = 0; i < Populations[1].size; i++)
910        {
911                var cr = Populations[1].get(i);
912                e_nut += cr.energy;
913        }
914
915        var log_numbers = [];
916        var log_energies = [];
917
918        for (var s = 0; s < species_genes.size; s++)
919        {
920                for (var p = 0; p < 2; p++)
921                {
922                        log_numbers.add(number[s][p]);
923                        log_energies.add(e_inc[s][p]);
924                }
925        }
926       
927        log_numbers.add(Populations[1].size);
928        log_energies.add(e_nut);
929
930        log(log_numbers, ExpProperties.logPref+"forams_log.txt");
931    log(log_energies,  ExpProperties.logPref+"energies_log.txt");
932}
933
934function log(tolog, fname)
935{
936        var f = File.appendDirect(fname, "forams data");
937        f.writeString("" + Simulator.stepNumber);
938        for (var  i = 0; i < tolog.size; i++)
939        {
940                f.writeString(";" + tolog[i]);
941        }
942        f.writeString("\n");
943        f.close();
944}
945
946function createLogVector(cr, value)
947{
948        var vec = Vector.new();
949        for (var i = 0; i < species_genes.size; i++)
950        {
951                for (var j = 0; j < 2; j++)
952                {
953                        vec.add(0);
954                }
955                if (cr.data->lifeparams->species == i)
956                {
957                        vec[i*2+cr.data->lifeparams->gen] = value;             
958                }
959        }
960        return vec;
961}
962
963
964// -------------------------------- step end --------------------------------
965//TODO default params values in frams instead of microns/seconds
966
967@include "standard_events.inc"
968
969~
970
971property:
972id:visualize
973name:Show reticulopodia and nutrients
974type:d 0 1 0
975group:
976
977property:
978id:maxSteps
979name:Maximum number of steps
980type:d 0 10000000 0
981group:
982
983property:
984id:scalingFactor
985name:Scaling factor for micrometers
986type:f 0 -1 0.01
987group:
988
989property:
990id:logging
991name:Log statistics to file
992type:d 0 1 0
993group:
994
995property:
996id:logPref
997name:Log prefix
998type:s
999
1000property:
1001id:secPerStep
1002name:Seconds per simulation step
1003help:~
1004Number of seconds of foraminifera time per simulation step.
1005Lower values mean smoother animation.~
1006type:f 1 480 300
1007flags: 16
1008group:
1009
1010property:
1011id:foramSpeedMmPerMin
1012name:Speed of foraminfera in mm/min
1013type:f 0.01 0.1 0.05
1014flags: 16
1015group:Foraminifera
1016
1017property:
1018id:dir_change_sec
1019name:Number of seconds before direction change
1020type:d 300 300000 6000
1021group:Foraminifera
1022
1023property:
1024id:foramPop
1025name:Initial forams population size
1026type:d 1 1000 20
1027group:Foraminifera
1028
1029property:
1030id:gametoPeriodSec
1031name:Time of gametogenesis
1032type:f 300 300000 21600
1033group:Reproduction
1034
1035property:
1036id:gametSuccessRate
1037name:Ratio of successful gamets
1038type:f 0.0001 0.01 0.001
1039group:Reproduction
1040
1041property:
1042id:divisionCost
1043name:Cost of division in pG
1044type:f 15 25 20
1045group:Reproduction
1046
1047property:
1048id:min_repro_energ_haplo
1049name:Min reproduction energy of haploid in pg
1050type:f 0 -1 350000
1051group:Energy
1052
1053property:
1054id:min_repro_energ_diplo
1055name:Min reproduction energy of diploid in pg
1056type:f 0 -1 600000
1057group:Energy
1058
1059property:
1060id:repro_prob
1061name:Probability of reproduction
1062type:f 0 1 0.8
1063group:Reproduction
1064
1065property:
1066id:energies0_haplo
1067name:Energy of offspring from diploid forams
1068type:f 0 -1 20
1069group:Energy
1070
1071property:
1072id:energies0_diplo
1073name:Energy of offspring from diploid forams
1074type:f 0 -1 1.25
1075group:Energy
1076
1077property:
1078id:max_chamber_num_haplo
1079name:Maximum number of haploid chambers
1080type:f 1 50 35
1081group:Energy
1082
1083property:
1084id:max_chamber_num_diplo
1085name:Maximum number of diploid chambers
1086type:f 1 50 35
1087group:Energy
1088
1089property:
1090id:crossprob
1091name:Crossover probability
1092type:f 0 1 0
1093group:Reproduction
1094
1095property:
1096id:mutationprob
1097name:Mutation probability
1098type:f 0 1 0
1099group:Reproduction
1100
1101property:
1102id:reproTimeSec
1103name:Time before reproduction
1104type:d 0 10000 720
1105group:Reproduction
1106
1107property:
1108id:chamberGrowthSec
1109name:Time of the chamber growth in seconds
1110type:f 720 43200 43200
1111group:Foraminifera
1112
1113property:
1114id:chamber_proculus_haplo
1115name:Size of proculus
1116type:f
1117group:Foraminifera
1118
1119property:
1120id:chamber_proculus_diplo
1121name:Size of proculus
1122type:f
1123group:Foraminifera
1124
1125property:
1126id:hunted_prob
1127name:Probability of being hunted
1128type:f 0 1 0
1129group:Foraminifera
1130
1131property:
1132id:zone1_range
1133name:Zone 1 range in frams units
1134type:f 0 200 10
1135group:Foraminifera
1136
1137property:
1138id:zone2_range
1139name:Zone 2 range in frams units
1140type:f 0 3000 30
1141group:Foraminifera
1142
1143property:
1144id:chamberCostPerSec
1145name:Cost of growning chamber per second
1146type:f 0 1 0.000001
1147group:Energy
1148
1149property:
1150id:e_death_level_haplo
1151name:Minimal level of energy to sustain life of haploid
1152type:f 0 1 0.5
1153group:Energy
1154
1155property:
1156id:e_death_level_diplo
1157name:Minimal level of energy to sustain life of diploid
1158type:f 0 1 0.5
1159group:Energy
1160
1161property:
1162id:energy_hib
1163name:Energy used for hibernation during one step
1164type:f 0 1 0.0000001
1165group:Energy
1166
1167property:
1168id:energy_move
1169name:Energy used for movement during one step
1170type:f 0 1 0.0000005
1171group:Energy
1172
1173property:
1174id:e_meta
1175name:Idle metabolism
1176type:f 0 1 0.0000005
1177group:Energy
1178help:Foraminifera consumes this proportion of its energy in one time step
1179
1180property:
1181id:ingestion
1182name:Ingestion rate
1183type:f 0 -1 0.25
1184group:Energy
1185
1186property:
1187id:nutrient_pop
1188name:Nutrient population
1189type:f 0 1000000
1190group:Energy
1191help:How fast energy is created in the world
1192
1193property:
1194id:energy_nut
1195name:Nutrient energy
1196type:f 0 10000000
1197group:Energy
1198
1199property:
1200id:nutrientradius
1201name:Nutrient size
1202type:f 0.001 0.9 0.1
1203group:Energy
1204
1205property:
1206id:picoCarbonPerMikro
1207name:Picograms of carbon in cubic micrometer
1208type:f 0 -1 0.13
1209group:Energy
1210
1211property:
1212id:feedtrans
1213name:Energy transfer per second
1214type:f 0 1 0.001
1215group:Energy
1216
1217property:
1218id:foodperiod
1219name:Time between food occurrences
1220type:f 0 1000000 14400
1221group:Energy
1222
1223property:
1224id:foodPeriodChange
1225name:Set variable feed rate
1226type:f 0 -1 0
1227group:Energy
1228
1229property:
1230id:stress
1231name:Environmental stress
1232type:d 0 1 1
1233group:
1234
1235property:
1236id:repro_trigger
1237name:Reproduction trigger
1238type:d 0 1 1
1239group:Reproduction
1240
1241property:
1242id:creath
1243name:Creation height
1244type:f -1 50 -0.99
1245help:~
1246Vertical position (above the surface) where new Forams are revived.
1247Negative values are only used in the water area:
1248  0   = at the surface
1249-0.5 = half depth
1250-1   = just above the bottom~
1251
1252property:
1253id:autorestart
1254name:Restart after extinction
1255help:Restart automatically this experiment after the last creature has died?
1256type:d 0 1 0
1257
1258state:
1259id:nutrient
1260name:Nutrient locations
1261help:vector of vectors [x,y,energy]
1262type:x
1263flags:32
1264
1265state:
1266id:notes
1267name:Notes
1268type:s 1
1269help:~
1270You can write anything here
1271(it will be saved to the experiment file)~
1272
1273state:
1274id:totaltestedcr
1275name:Evaluated Forams
1276help:Total number of the Forams evaluated in the experiment
1277type:d
1278flags:16
Note: See TracBrowser for help on using the repository browser.