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

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

Enhanced nutrient visualization corrected.

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        placeCreatureRandomly(cr, 0, 0);
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                        return cr;
413        }
414
415        Populations[0].delete(cr);
416}
417
418function reticulopodiaExists(cr)
419{
420        var has_ret = 0;
421
422        if (cr.data->reticulopodiacreature != null)
423        {
424                if (Populations[2].findUID(cr.data->reticulopodiacreature.uid) != null)
425                {
426                        has_ret = 1;
427                }
428        }
429
430        return has_ret;
431}
432
433function visualization(cr)
434{
435        return reticulopodiaExists(cr);
436}
437
438function foramGrow(cr, chamber_num, lastchambergrowth)
439{
440        if ((chamber_num+1) <= max_chamber_volume[cr.data->lifeparams->gen].size)
441        {
442                curColor = colors[cr.data->lifeparams->gen];
443                var ploid = cr.data->lifeparams->gen;
444                var geno = create_genotype(getPloidRadius(ploid), chamber_num+1, colors[ploid], lastchambergrowth);
445                var cr2 = Populations[0].add(geno);
446
447                cr2.energy0 = cr.energy;
448                cr2.energy = cr2.energy0;
449
450                setGenotype({"cr" : cr2, "parent_genes" : cr.data->genes, "parent_lifeparams" : cr.data->lifeparams, "opt" : "growth", "energy0" : cr.energy0});
451                cr2.moveAbs(cr.pos_x, cr.pos_y,cr.pos_z);
452                setForamMeta(cr2);
453
454                if (reticulopodiaExists(cr))
455                {
456                        Populations[2].delete(cr.data->reticulopodiacreature);
457                }
458                Populations[0].delete(cr);
459                return cr2;
460        }
461        return cr;
462}
463
464function visualizeChamberGrowth(cr, chamber_time)
465{
466        var total_time = secToSimSteps(ExpProperties.chamberGrowthSec);
467        var ret_unit = total_time/chamber_vis_denominator;
468        var chamber_unit = total_time-ret_unit;
469
470        if (chamber_time < ret_unit || chamber_time >= chamber_unit)
471        {
472                var new_rad = Math.min(Math.max((chamber_time%ret_unit)/ret_unit*getZoneRange(cr,1),0.01),getZoneRange(cr,1));
473
474                if(chamber_time < ret_unit)
475                {
476                        new_rad = getZoneRange(cr,1)-new_rad;
477                }
478
479                curColor = colors[cr.data->lifeparams->gen];
480                addReticulopodia(cr,new_rad);
481
482                if (chamber_time == 0)//checking for end of chamber growth process
483                {
484                        cr.data->lifeparams->chamber_growth = -1;       
485                }
486        }
487        else
488        {
489                var new_rad = 1 - Math.min(Math.max((chamber_time-ret_unit)/chamber_unit,0.01),1);
490                curRadius = cr.data->reticulopodiacreature.getPart(1).sy;
491                               
492                if (chamber_time == ret_unit)
493                {
494                        new_rad = 1;
495                }
496
497                var new_cr = foramGrow(cr, chamberNumFromEnergy(cr.data->lifeparams->max_energy_level, cr.data->lifeparams->gen)-1, new_rad);
498                curRadius = getZoneRange(new_cr,1);
499        }
500}
501
502function stepToNearest(cr)
503{
504        var p = cr.getMechPart(0);
505        var n = cr.signals.receiveSet("nutrient", getZoneRange(cr,2));
506
507        //if signals are received find the source of the nearest
508        if (n.size > 0)
509        {
510                var i;
511                var mp;
512                var distvec = XYZ.new(0, 0, 0);
513                var dist;
514                var mindist = 100000000000.0;
515                var mindistvec = null;
516                var eating = 0;
517
518                for (i = 0; i < n.size; i++)
519                {
520                        mp = n[i].value.getMechPart(0);
521                        distvec.set(mp.pos);
522                        distvec.sub(p.pos);
523                        dist = distvec.length;
524                        if (dist < getZoneRange(cr,1))
525                        {
526                                if (n[i].value != null)
527                                {
528                                        energyTransfer(cr, n[i].value);
529                                        eating = 1;
530                                }
531                        }
532                        else if (eating == 0 && cr.data->lifeparams->hibernated == 0 && dist < mindist)
533                        {
534                                mindist = dist;
535                                mindistvec = distvec.clone();
536                        }
537                }
538
539                if (!eating && cr.data->lifeparams->hibernated == 0)
540                {
541                        mindistvec.normalize();
542                        mindistvec.scale(-1*movePerStep);
543                        cr.localDrive = mindistvec;
544                        moveEnergyDec(cr);
545                }
546
547                return 1;
548        }
549       
550        else
551        {
552                return 0;
553        }
554}
555
556function moveEnergyDec(cr)
557{
558        if (cr.data->lifeparams->hibernated == 0)
559        {
560                //percent of maximal energy
561                cr.energy -= (ExpProperties.energy_move * cr.data->lifeparams->max_energy_level)*ExpProperties.secPerStep;
562        }
563}
564
565function fence(center, zone)
566{
567        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
568}
569
570function foramMove(cr)
571{
572        //adjustment in z axis
573        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)
574
575        //are there any nutrients in zone 1 or 2?
576        {
577                var moved = stepToNearest(cr); //TODO weighted sum of distance and energy
578                if (moved==1)
579                {
580                        moveReticulopodia(cr);
581                        return;
582                }
583        }
584
585        //Prevents forams from crossing the world border. In the case of touching the border with the reticulopodia direction of the movement should be changed.
586        var change_direction = 0;
587        var new_x = fence(cr.center_x, getZoneRange(cr, 1));
588        var new_y = fence(cr.center_y, getZoneRange(cr, 1));
589
590        if ((new_x != cr.center_x) || (new_y != cr.center_y) || (cr.data->lifeparams->dir_counter >= int(secToSimSteps(ExpProperties.dir_change_sec))))
591        {
592                change_direction = 1;
593                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)
594        }
595
596        //no nutrients in zone 2
597        if (getGene(cr, "hibernation",0) == 1)
598        {
599                reverseHib(cr);
600                cr.localDrive = XYZ.new(0,0,0);
601        }
602        //random move
603        else if (change_direction == 1)
604        {
605                cr.data->lifeparams->dir = randomDir();
606                cr.data->lifeparams->dir_counter = 0;
607                cr.localDrive = cr.data->lifeparams->dir;
608                moveEnergyDec(cr);
609        }
610        else
611        {
612                cr.localDrive = cr.data->lifeparams->dir;
613        }
614        moveReticulopodia(cr);
615}
616
617function moveReticulopodia(cr)
618{
619        if (visualization(cr))
620        {
621                cr.data->reticulopodiacreature.moveAbs(cr.center_x-getZoneRange(cr,1), cr.center_y-getZoneRange(cr,1), cr.center_z-getZoneRange(cr,1));
622                cr.data->reticulopodiacreature.localDrive = cr.localDrive;
623        }
624}
625
626function randomDir()
627{
628        var dir = (Math.rndUni(-ExpProperties.zone2_range, ExpProperties.zone2_range), Math.rndUni(-ExpProperties.zone2_range, ExpProperties.zone2_range), 0); 
629        dir.normalize();
630        dir.scale(-1*movePerStep);
631        return dir;
632}
633
634function energyTransfer(cr1, cr2)
635{
636        cr1.localDrive = XYZ.new(0,0,0);
637        var e =  ExpProperties.feedtrans*cr1.energy*ExpProperties.secPerStep; //TODO efficiency dependent on age
638        //Simulator.print("transferring "+e +"("+e*ExpProperties.ingestion+")"+" to "+cr1.name +" ("+ cr1.energy+") " +" from "+cr2.uid+" ("+cr2.energy+") "+ e/ExpProperties.secPerStep+ " per sec");
639        var transferred = cr2.transferEnergyTo(cr1, e);
640        cr1.energy -= transferred*(1-ExpProperties.ingestion);
641        if (cr1.data->lifeparams->hibernated == 1)
642        {
643                reverseHib(cr1);
644        }
645}
646
647function reverseHib(cr)
648{
649        if (cr.data->lifeparams->hibernated == 1)
650        {
651                setForamMeta(cr); //unhibernate
652        }
653        else
654        {
655                cr.idleen = (ExpProperties.energy_hib * cr.energy)*ExpProperties.secPerStep; //hibernate
656        }
657        cr.data->lifeparams->hibernated = 1 - cr.data->lifeparams->hibernated;
658}
659
660function onForamsStep(cr)
661{
662        //checking for gametogenesis process
663        if (cr.data->lifeparams->division_time > 0)
664        {
665                cr.data->lifeparams->division_time = Math.max(cr.data->lifeparams->division_time-1,0);
666        }
667        //checking for end of gametogenesis
668        else if (cr.data->lifeparams->division_time == 0)
669        {
670                //waiting for gamets fusion
671        }
672        //checking for chamber growth process
673        else if (cr.data->lifeparams->chamber_growth > 0)
674        {
675                var chamber_time = Math.max(cr.data->lifeparams->chamber_growth-1,0);
676                cr.data->lifeparams->chamber_growth = chamber_time;
677                cr.energy -= ExpProperties.chamberCostPerSec * cr.energy * ExpProperties.secPerStep;
678
679                if (visualization(cr))
680                {
681                        visualizeChamberGrowth(cr, chamber_time);
682                }
683        }
684        //checking for end of the chamber growth process
685        else if (cr.data->lifeparams->chamber_growth == 0 && visualization(cr)==0)
686        {       
687                foramGrow(cr, lastChamberNum(cr), 1);
688                cr.data->lifeparams->chamber_growth = -1;
689                //Simulator.print("chamber "+ (lastChamberNum(cr) + 1) +" complete");
690        }
691        else
692        {
693                //update of metabolism rate
694                if (cr.data->lifeparams->hibernated == 0)
695                {
696                        setForamMeta(cr);
697                }
698
699                if (deathConditions(cr) == 1)
700                {
701                        if (ExpProperties.logging == 1)
702                        {
703                                log(createLogVector(cr, cr.data->lifeparams->max_energy_level),ExpProperties.logPref+"fossil_log.txt");
704                                log(createLogVector(cr, cr.lifespan),ExpProperties.logPref+"lifespan_log.txt");
705                        }                       
706                        Populations[0].kill(cr);
707                        return;
708                }
709
710                //update direction change counter
711                cr.data->lifeparams->dir_counter += 1;
712
713                foramMove(cr);
714
715                var repro = foramReproduce(cr);
716                if (repro == 1)
717                {
718                        return;
719                }
720
721                cr.data->lifeparams->max_energy_level = Math.max(cr.energy, cr.data->lifeparams->max_energy_level);
722
723                //cheking conditions of chamber growth process start
724                if  (lastChamberNum(cr) < max_chamber_volume[cr.data->lifeparams->gen].size)
725                {
726                        if ((cr.data->lifeparams->max_energy_level >= energyFromVolume(max_chamber_volume[cr.data->lifeparams->gen][lastChamberNum(cr)-1],0))) 
727                        {
728                                cr.data->lifeparams->chamber_growth = int(secToSimSteps(ExpProperties.chamberGrowthSec));
729                        }       
730                }
731        }       
732}
733
734function deathConditions(cr)
735{
736        if ((cr.energy <= getProperty(cr.data->lifeparams->gen,"e_death_level")*cr.data->lifeparams->max_energy_level) || (Math.rnd01 < ExpProperties.hunted_prob))
737        {
738                return 1;
739        }
740        else
741                return 0;
742}
743
744function onForamsDied(cr)
745{
746        if (visualization(cr))
747        {
748                Populations[2].delete(cr.data->reticulopodiacreature);
749        }
750        //fossilization
751        var geno = GenePools[0].add(cr.genotype);
752        geno.data->genes = cr.data->genes;
753        geno.data->lifeparams = cr.data->lifeparams;
754        if (ExpProperties.logging == 1) Simulator.print("\"" + cr.name + "\" died...");
755        ExpState.totaltestedcr++;
756}
757
758// --------------------------------foram end -------------------------------------
759
760// -------------------------------- nutrient begin --------------------------------
761
762function createNutrientGenotype(nutrientradius)
763{
764        return "//0\nm:Vstyle=nutrient\np:sh=3,sx="+nutrientradius+",sy="+nutrientradius+",sz="+nutrientradius+",ry=1.57,vr=0.0,1.0,0.0";
765}
766
767function onNutrientsStep(cr)
768{
769        cr.moveAbs(cr.pos_x % World.wrldsiz, cr.pos_y % World.wrldsiz, 0.5);
770}
771
772function addNutrient()
773{
774        var cr = Populations[1].add(createNutrientGenotype(ExpProperties.nutrientradius));
775
776        cr.name = "Nutrients";
777        cr.idleen = 0;
778        cr.energy0 = ExpProperties.energy_nut;
779        cr.energy = cr.energy0;
780        cr.signals.add("nutrient");
781
782        cr.signals[0].value = cr;
783
784        placeCreatureRandomly(cr, 0, 0);
785        if (ExpProperties.visualize == 1)
786        {
787                var nutsize = ExpProperties.nutrientradius*10;
788                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");
789                cr.data->reticulopodiacreature = nut;
790                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);
791        }
792}
793
794function onNutrientsDied(cr)
795{
796        if (visualization(cr))
797        {
798                Populations[2].delete(cr.data->reticulopodiacreature);
799        }
800}
801
802function nutrientGrowth()
803{
804        if (ExpProperties.foodPeriodChange > 0)
805        {
806                        changePeriod += 1;
807                        if (phase=="low" && changePeriod >= secToSimSteps(23328000)) //9 months
808                        {
809                                ExpProperties.foodperiod = ExpProperties.foodperiod/ExpProperties.foodPeriodChange;
810                                phase = "high";
811                                changePeriod = 0;
812                        }
813               
814                        else if (phase == "high" && changePeriod >= secToSimSteps(7776000)) //3 months
815                        {
816                                ExpProperties.foodperiod = ExpProperties.foodperiod*ExpProperties.foodPeriodChange;
817                                phase = "low";
818                                changePeriod = 0;
819                        }
820        }
821        nutrientenergywaiting = nutrientenergywaiting + 1;
822        if (nutrientenergywaiting >= secToSimSteps(ExpProperties.foodperiod))
823        {
824                for (var i = 0; i < ExpProperties.nutrient_pop; i++)
825                {   
826                        addNutrient();
827                }
828
829                nutrientenergywaiting = 0.0;
830                Simulator.checkpoint();
831
832                if (ExpProperties.logging == 1)
833                {
834                        log([ExpProperties.nutrient_pop],ExpProperties.logPref+"nutrients_log.txt");
835                }
836        }
837
838}
839
840// -------------------------------- nutrient end --------------------------------
841
842// -------------------------------- step begin --------------------------------
843
844function onStep()
845{
846
847        nutrientGrowth();
848        if (ExpProperties.logging == 1)
849        {
850                createStatistics();
851        }
852
853        //reproduction --------------------------------------------
854        reprocounter += 1;
855        if (reprocounter > secToSimSteps(ExpProperties.reproTimeSec))
856        {
857                reprocounter = 0;
858                for (var s = 0; s < species_genes.size; s++)
859                {
860                        reproduce_parents(s);
861                }
862               
863        }
864
865        //check for extinction -----------------------------------------------
866        if (Populations[0].size == 0)
867        {
868                if (ExpProperties.autorestart)
869                {
870                        Simulator.print("no more creatures, restarting...");
871                        onExpInit();
872                }
873                else
874                {
875                        Simulator.print("no more creatures, stopped.");
876                        Simulator.stop();
877                }
878        }
879        if (ExpProperties.maxSteps > 0)
880        {
881                if (Simulator.stepNumber >= ExpProperties.maxSteps)
882                        Simulator.stop();
883        }
884}
885
886function createStatistics()
887{       
888        var number = [];
889        var e_inc = [];
890        var e_nut = 0.0;
891
892        for (var s = 0; s < species_genes.size; s++)
893        {
894                number.add([0,0]);// [haplo][diplo]
895                e_inc.add([0,0]);
896        }
897
898        for (var i = 0; i < Populations[0].size; i++)
899        {
900                var cr = Populations[0].get(i);
901                var gen = cr.data->lifeparams->gen;
902                var species = cr.data->lifeparams->species;
903
904                number[species][gen] = number[species][gen] + 1;
905                e_inc[species][gen] = e_inc[species][gen] + cr.energy;
906        }
907
908        for (var i = 0; i < Populations[1].size; i++)
909        {
910                var cr = Populations[1].get(i);
911                e_nut += cr.energy;
912        }
913
914        var log_numbers = [];
915        var log_energies = [];
916
917        for (var s = 0; s < species_genes.size; s++)
918        {
919                for (var p = 0; p < 2; p++)
920                {
921                        log_numbers.add(number[s][p]);
922                        log_energies.add(e_inc[s][p]);
923                }
924        }
925       
926        log_numbers.add(Populations[1].size);
927        log_energies.add(e_nut);
928
929        log(log_numbers, ExpProperties.logPref+"forams_log.txt");
930    log(log_energies,  ExpProperties.logPref+"energies_log.txt");
931}
932
933function log(tolog, fname)
934{
935        var f = File.appendDirect(fname, "forams data");
936        f.writeString("" + Simulator.stepNumber);
937        for (var  i = 0; i < tolog.size; i++)
938        {
939                f.writeString(";" + tolog[i]);
940        }
941        f.writeString("\n");
942        f.close();
943}
944
945function createLogVector(cr, value)
946{
947        var vec = Vector.new();
948        for (var i = 0; i < species_genes.size; i++)
949        {
950                for (var j = 0; j < 2; j++)
951                {
952                        vec.add(0);
953                }
954                if (cr.data->lifeparams->species == i)
955                {
956                        vec[i*2+cr.data->lifeparams->gen] = value;             
957                }
958        }
959        return vec;
960}
961
962
963// -------------------------------- step end --------------------------------
964//TODO default params values in frams instead of microns/seconds
965
966@include "standard_events.inc"
967
968~
969
970property:
971id:visualize
972name:Show reticulopodia and nutrients
973type:d 0 1 0
974group:
975
976property:
977id:maxSteps
978name:Maximum number of steps
979type:d 0 10000000 0
980group:
981
982property:
983id:scalingFactor
984name:Scaling factor for micrometers
985type:f 0 -1 0.01
986group:
987
988property:
989id:logging
990name:Log statistics to file
991type:d 0 1 0
992group:
993
994property:
995id:logPref
996name:Log prefix
997type:s
998
999property:
1000id:secPerStep
1001name:Seconds per simulation step
1002help:~
1003Number of seconds of foraminifera time per simulation step.
1004Lower values mean smoother animation.~
1005type:f 1 480 300
1006flags: 16
1007group:
1008
1009property:
1010id:foramSpeedMmPerMin
1011name:Speed of foraminfera in mm/min
1012type:f 0.01 0.1 0.05
1013flags: 16
1014group:Foraminifera
1015
1016property:
1017id:dir_change_sec
1018name:Number of seconds before direction change
1019type:d 300 300000 6000
1020group:Foraminifera
1021
1022property:
1023id:foramPop
1024name:Initial forams population size
1025type:d 1 1000 20
1026group:Foraminifera
1027
1028property:
1029id:gametoPeriodSec
1030name:Time of gametogenesis
1031type:f 300 300000 21600
1032group:Reproduction
1033
1034property:
1035id:gametSuccessRate
1036name:Ratio of successful gamets
1037type:f 0.0001 0.01 0.001
1038group:Reproduction
1039
1040property:
1041id:divisionCost
1042name:Cost of division in pG
1043type:f 15 25 20
1044group:Reproduction
1045
1046property:
1047id:min_repro_energ_haplo
1048name:Min reproduction energy of haploid in pg
1049type:f 0 -1 350000
1050group:Energy
1051
1052property:
1053id:min_repro_energ_diplo
1054name:Min reproduction energy of diploid in pg
1055type:f 0 -1 600000
1056group:Energy
1057
1058property:
1059id:repro_prob
1060name:Probability of reproduction
1061type:f 0 1 0.8
1062group:Reproduction
1063
1064property:
1065id:energies0_haplo
1066name:Energy of offspring from diploid forams
1067type:f 0 -1 20
1068group:Energy
1069
1070property:
1071id:energies0_diplo
1072name:Energy of offspring from diploid forams
1073type:f 0 -1 1.25
1074group:Energy
1075
1076property:
1077id:max_chamber_num_haplo
1078name:Maximum number of haploid chambers
1079type:f 1 50 35
1080group:Energy
1081
1082property:
1083id:max_chamber_num_diplo
1084name:Maximum number of diploid chambers
1085type:f 1 50 35
1086group:Energy
1087
1088property:
1089id:crossprob
1090name:Crossover probability
1091type:f 0 1 0
1092group:Reproduction
1093
1094property:
1095id:mutationprob
1096name:Mutation probability
1097type:f 0 1 0
1098group:Reproduction
1099
1100property:
1101id:reproTimeSec
1102name:Time before reproduction
1103type:d 0 10000 720
1104group:Reproduction
1105
1106property:
1107id:chamberGrowthSec
1108name:Time of the chamber growth in seconds
1109type:f 720 43200 43200
1110group:Foraminifera
1111
1112property:
1113id:chamber_proculus_haplo
1114name:Size of proculus
1115type:f
1116group:Foraminifera
1117
1118property:
1119id:chamber_proculus_diplo
1120name:Size of proculus
1121type:f
1122group:Foraminifera
1123
1124property:
1125id:hunted_prob
1126name:Probability of being hunted
1127type:f 0 1 0
1128group:Foraminifera
1129
1130property:
1131id:zone1_range
1132name:Zone 1 range in frams units
1133type:f 0 200 10
1134group:Foraminifera
1135
1136property:
1137id:zone2_range
1138name:Zone 2 range in frams units
1139type:f 0 3000 30
1140group:Foraminifera
1141
1142property:
1143id:chamberCostPerSec
1144name:Cost of growning chamber per second
1145type:f 0 1 0.000001
1146group:Energy
1147
1148property:
1149id:e_death_level_haplo
1150name:Minimal level of energy to sustain life of haploid
1151type:f 0 1 0.5
1152group:Energy
1153
1154property:
1155id:e_death_level_diplo
1156name:Minimal level of energy to sustain life of diploid
1157type:f 0 1 0.5
1158group:Energy
1159
1160property:
1161id:energy_hib
1162name:Energy used for hibernation during one step
1163type:f 0 1 0.0000001
1164group:Energy
1165
1166property:
1167id:energy_move
1168name:Energy used for movement during one step
1169type:f 0 1 0.0000005
1170group:Energy
1171
1172property:
1173id:e_meta
1174name:Idle metabolism
1175type:f 0 1 0.0000005
1176group:Energy
1177help:Foraminifera consumes this proportion of its energy in one time step
1178
1179property:
1180id:ingestion
1181name:Ingestion rate
1182type:f 0 -1 0.25
1183group:Energy
1184
1185property:
1186id:nutrient_pop
1187name:Nutrient population
1188type:f 0 1000000
1189group:Energy
1190help:How fast energy is created in the world
1191
1192property:
1193id:energy_nut
1194name:Nutrient energy
1195type:f 0 10000000
1196group:Energy
1197
1198property:
1199id:nutrientradius
1200name:Nutrient size
1201type:f 0.001 0.9 0.1
1202group:Energy
1203
1204property:
1205id:picoCarbonPerMikro
1206name:Picograms of carbon in cubic micrometer
1207type:f 0 -1 0.13
1208group:Energy
1209
1210property:
1211id:feedtrans
1212name:Energy transfer per second
1213type:f 0 1 0.001
1214group:Energy
1215
1216property:
1217id:foodperiod
1218name:Time between food occurrences
1219type:f 0 1000000 14400
1220group:Energy
1221
1222property:
1223id:foodPeriodChange
1224name:Set variable feed rate
1225type:f 0 -1 0
1226group:Energy
1227
1228property:
1229id:stress
1230name:Environmental stress
1231type:d 0 1 1
1232group:
1233
1234property:
1235id:repro_trigger
1236name:Reproduction trigger
1237type:d 0 1 1
1238group:Reproduction
1239
1240property:
1241id:creath
1242name:Creation height
1243type:f -1 50 -0.99
1244help:~
1245Vertical position (above the surface) where new Forams are revived.
1246Negative values are only used in the water area:
1247  0   = at the surface
1248-0.5 = half depth
1249-1   = just above the bottom~
1250
1251property:
1252id:autorestart
1253name:Restart after extinction
1254help:Restart automatically this experiment after the last creature has died?
1255type:d 0 1 0
1256
1257state:
1258id:nutrient
1259name:Nutrient locations
1260help:vector of vectors [x,y,energy]
1261type:x
1262flags:32
1263
1264state:
1265id:notes
1266name:Notes
1267type:s 1
1268help:~
1269You can write anything here
1270(it will be saved to the experiment file)~
1271
1272state:
1273id:totaltestedcr
1274name:Evaluated Forams
1275help:Total number of the Forams evaluated in the experiment
1276type:d
1277flags:16
Note: See TracBrowser for help on using the repository browser.