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

Last change on this file since 582 was 582, checked in by Maciej Komosinski, 8 years ago

Added comments and TODOs

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