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

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

Vertical position of foraminifera lowered. Color of body and reticulopodia unified.

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