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
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);
99        colors = ["1.0,1.0,0.0","1.0,0.5,0.0"];
[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
[493]527function fence(pos, zone)
[487]528{
[496]529        return Math.min(Math.max(0,pos),World.wrldsiz);
[487]530}
531
[422]532function foramMove(cr)
[421]533{
[422]534        //TODO moving inside sediment?
[421]535
[422]536        //adjustment in z axis
[554]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));
[421]540
[554]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
[579]546        cr.moveAbs(new_x, new_y, -cr.getPart(cr.numparts-1).sx);
[554]547
[430]548        //are there any nutrients in zone 1 or 2?
[401]549        {
[435]550                var moved = stepToNearest(cr); //TODO weighted sum of distance and energy
[422]551                if (moved==1)
[430]552                {
[502]553                        moveReticulopodia(cr);
[422]554                        return;
555                }
[401]556        }
557
[422]558        //no nutrients in zone 2
[487]559        if (getGene(cr, "hibernation",0) == 1)
[401]560        {
[430]561                reverseHib(cr);
[422]562                cr.localDrive = XYZ.new(0,0,0);
[380]563        }
[422]564        //random move
[554]565        else if (change_direction == 1)
[380]566        {
[476]567                cr.data->lifeparams->dir = randomDir();
[552]568                cr.data->lifeparams->dir_counter = 0;
[476]569                cr.localDrive = cr.data->lifeparams->dir;
[422]570                moveEnergyDec(cr);
571        }
[474]572        else
573        {
[476]574                cr.localDrive = cr.data->lifeparams->dir;
[474]575        }
[502]576        moveReticulopodia(cr);
[422]577}
[401]578
[502]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
[474]588function randomDir()
589{
[486]590        var dir = (Math.rndUni(-ExpProperties.zone2_range, ExpProperties.zone2_range), Math.rndUni(-ExpProperties.zone2_range, ExpProperties.zone2_range), 0); 
[474]591        dir.normalize();
592        dir.scale(-1*movePerStep);
593        return dir;
594}
595
[430]596function energyTransfer(cr1, cr2)
[422]597{
[430]598        cr1.localDrive = XYZ.new(0,0,0);
[510]599        var e =  ExpProperties.feedtrans*cr1.energy*ExpProperties.secPerStep; //TODO efficiency dependent on age
[502]600        //Simulator.print("transferring "+e +"("+e*ExpProperties.ingestion+")"+" to "+cr1.name +" ("+ cr1.energy+") " +" from "+cr2.uid+" ("+cr2.energy+") "+ e/ExpProperties.secPerStep+ " per sec");
[510]601        var transferred = cr2.transferEnergyTo(cr1, e);
602        cr1.energy -= transferred*(1-ExpProperties.ingestion);
[476]603        if (cr1.data->lifeparams->hibernated == 1)
[422]604        {
[430]605                reverseHib(cr1);
[422]606        }
607}
[401]608
[430]609function reverseHib(cr)
610{
[476]611        if (cr.data->lifeparams->hibernated == 1)
[430]612        {
[479]613                setForamMeta(cr); //unhibernate
[430]614        }
615        else
616        {
[486]617                cr.idleen = (ExpProperties.energy_hib * cr.energy)*ExpProperties.secPerStep; //hibernate
[430]618        }
[476]619        cr.data->lifeparams->hibernated = 1 - cr.data->lifeparams->hibernated;
[430]620}
621
[422]622function onForamsStep(cr)
623{
[479]624        //checking for gametogenesis process
625        if (cr.data->lifeparams->division_time > 0)
[444]626        {
[479]627                cr.data->lifeparams->division_time = Math.max(cr.data->lifeparams->division_time-1,0);
[444]628        }
[479]629        //checking for end of gametogenesis
630        else if (cr.data->lifeparams->division_time == 0)
[422]631        {
[479]632                //waiting for gamets fusion
[422]633        }
[479]634        //checking for chamber growth process
635        else if (cr.data->lifeparams->chamber_growth > 0)
636        {
[567]637                var chamber_time = Math.max(cr.data->lifeparams->chamber_growth-1,0);
638                cr.data->lifeparams->chamber_growth = chamber_time;
[504]639                cr.energy -= ExpProperties.chamberCostPerSec * cr.energy * ExpProperties.secPerStep;
[422]640
[567]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
[579]654                                curColor = colors[cr.data->lifeparams->gen];
[567]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                }
[479]675        }
676        //checking for end of chamber growth process
[567]677        else if (cr.data->lifeparams->chamber_growth == 0 && visualization(cr)==0)
678        {       
679                foramGrow(cr, lastChamberNum(cr), 1);
[479]680                cr.data->lifeparams->chamber_growth = -1;
681                //Simulator.print("chamber "+ (lastChamberNum(cr) + 1) +" complete");
[430]682        }
[479]683        else
[422]684        {
[479]685                //update of metabolism rate
686                if (cr.data->lifeparams->hibernated == 0)
[401]687                {
[479]688                        setForamMeta(cr);
689                }
690
691                if (deathConditions(cr) == 1)
692                {
[493]693                        if (ExpProperties.logging == 1)
694                        {
695                                log(createLogVector(cr, cr.data->lifeparams->max_energy_level),ExpProperties.logPref+"fossil_log.txt");
[552]696                                log(createLogVector(cr, cr.lifespan),ExpProperties.logPref+"lifespan_log.txt");
[493]697                        }                       
[479]698                        Populations[0].kill(cr);
699                        return;
700                }
701
[552]702                //update direction change counter
703                cr.data->lifeparams->dir_counter += 1;
704
[479]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
[567]716                if  (lastChamberNum(cr) < max_chamber_volume[cr.data->lifeparams->gen].size)
[479]717                {
[567]718                        if ((cr.data->lifeparams->max_energy_level >= energyFromVolume(max_chamber_volume[cr.data->lifeparams->gen][lastChamberNum(cr)-1],0))) 
[479]719                        {
[554]720                                cr.data->lifeparams->chamber_growth = int(secToSimSteps(ExpProperties.chamberGrowthSec));
[479]721                        }       
722                }
[430]723        }       
[380]724}
725
[422]726function deathConditions(cr)
727{
[486]728        if ((cr.energy <= getProperty(cr.data->lifeparams->gen,"e_death_level")*cr.data->lifeparams->max_energy_level) || (Math.rnd01 < ExpProperties.hunted_prob))
[479]729        {
[422]730                return 1;
[479]731        }
[422]732        else
733                return 0;
734}
735
[421]736function onForamsDied(cr)
[380]737{
[444]738        if (visualization(cr))
[430]739        {
[476]740                Populations[2].delete(cr.data->reticulopodiacreature);
[430]741        }
[380]742        //fossilization
743        var geno = GenePools[0].add(cr.genotype);
[476]744        geno.data->genes = cr.data->genes;
745        geno.data->lifeparams = cr.data->lifeparams;
[486]746        if (ExpProperties.logging == 1) Simulator.print("\"" + cr.name + "\" died...");
[380]747        ExpState.totaltestedcr++;
748}
749
[421]750// --------------------------------foram end -------------------------------------
[380]751
[421]752// -------------------------------- nutrient begin --------------------------------
[380]753
[479]754function createNutrientGenotype(nutrientradius)
[422]755{
[524]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";
[422]757}
758
[421]759function onNutrientsStep(cr)
[380]760{
[444]761        cr.moveAbs(cr.pos_x % World.wrldsiz, cr.pos_y % World.wrldsiz, 0.5);
[380]762}
763
[421]764function addNutrient()
[380]765{
[486]766        var cr = Populations[1].add(createNutrientGenotype(ExpProperties.nutrientradius));
[380]767
[421]768        cr.name = "Nutrients";
[380]769        cr.idleen = 0;
[486]770        cr.energy0 = ExpProperties.energy_nut;
[416]771        cr.energy = cr.energy0;
[421]772        cr.signals.add("nutrient");
[380]773
[430]774        cr.signals[0].value = cr;
[380]775
[422]776        placeCreatureRandomly(cr, 0, 0);
[486]777        if (ExpProperties.visualize == 1)
[444]778        {
[486]779                var nutsize = ExpProperties.nutrientradius*10;
[555]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");
[476]781                cr.data->reticulopodiacreature = nut;
[444]782                nut.moveAbs(cr.pos_x-1.5*nutsize, cr.pos_y-1.5*nutsize, 0.5);
783        }
[380]784}
785
[444]786function onNutrientsDied(cr)
787{
788        if (visualization(cr))
789        {
[476]790                Populations[2].delete(cr.data->reticulopodiacreature);
[444]791        }
792}
793
[422]794function nutrientGrowth()
[380]795{
[486]796        if (ExpProperties.foodPeriodChange > 0)
[481]797        {
798                        changePeriod += 1;
[554]799                        if (phase=="low" && changePeriod >= secToSimSteps(23328000)) //9 months
[481]800                        {
[486]801                                ExpProperties.foodperiod = ExpProperties.foodperiod/ExpProperties.foodPeriodChange;
[481]802                                phase = "high";
803                                changePeriod = 0;
804                        }
805               
[554]806                        else if (phase == "high" && changePeriod >= secToSimSteps(7776000)) //3 months
[481]807                        {
[486]808                                ExpProperties.foodperiod = ExpProperties.foodperiod*ExpProperties.foodPeriodChange;
[481]809                                phase = "low";
810                                changePeriod = 0;
811                        }
812        }
[474]813        nutrientenergywaiting = nutrientenergywaiting + 1;
[554]814        if (nutrientenergywaiting >= secToSimSteps(ExpProperties.foodperiod))
[380]815        {
[493]816                for (var i = 0; i < ExpProperties.nutrient_pop; i++)
[432]817                {   
[422]818                        addNutrient();
819                }
820
821                nutrientenergywaiting = 0.0;
822                Simulator.checkpoint();
[493]823
824                if (ExpProperties.logging == 1)
825                {
826                        log([ExpProperties.nutrient_pop],ExpProperties.logPref+"nutrients_log.txt");
827                }
[380]828        }
[493]829
[380]830}
831
[421]832// -------------------------------- nutrient end --------------------------------
[380]833
[422]834// -------------------------------- step begin --------------------------------
[380]835
[422]836function onStep()
837{
[432]838
839        nutrientGrowth();
[486]840        if (ExpProperties.logging == 1)
[422]841        {
842                createStatistics();
843        }
844
845        //reproduction --------------------------------------------
846        reprocounter += 1;
[554]847        if (reprocounter > secToSimSteps(ExpProperties.reproTimeSec))
[422]848        {
849                reprocounter = 0;
[567]850                for (var s = 0; s < species_genes.size; s++)
851                {
852                        reproduce_parents(s);
853                }
854               
[422]855        }
856
857        //check for extinction -----------------------------------------------
858        if (Populations[0].size == 0)
859        {
[486]860                if (ExpProperties.autorestart)
[422]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        }
[486]871        if (ExpProperties.maxSteps > 0)
[432]872        {
[486]873                if (Simulator.stepNumber >= ExpProperties.maxSteps)
[432]874                        Simulator.stop();
875        }
[422]876}
877
878function createStatistics()
879{       
[487]880        var number = [];
881        var e_inc = [];
[422]882        var e_nut = 0.0;
883
[487]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
[422]890        for (var i = 0; i < Populations[0].size; i++)
891        {
892                var cr = Populations[0].get(i);
[476]893                var gen = cr.data->lifeparams->gen;
894                var species = cr.data->lifeparams->species;
[430]895
896                number[species][gen] = number[species][gen] + 1;
897                e_inc[species][gen] = e_inc[species][gen] + cr.energy;
[422]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
[487]906        var log_numbers = [];
907        var log_energies = [];
[422]908
[487]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
[493]921        log(log_numbers, ExpProperties.logPref+"forams_log.txt");
922    log(log_energies,  ExpProperties.logPref+"energies_log.txt");
[422]923}
924
[380]925function log(tolog, fname)
926{
[430]927        var f = File.appendDirect(fname, "forams data");
[380]928        f.writeString("" + Simulator.stepNumber);
[401]929        for (var  i = 0; i < tolog.size; i++)
[380]930        {
931                f.writeString(";" + tolog[i]);
932        }
933        f.writeString("\n");
934        f.close();
935}
936
[567]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
[422]955// -------------------------------- step end --------------------------------
[479]956//TODO default params values in frams instead of microns/seconds
[380]957
[401]958@include "standard_events.inc"
[380]959
[401]960~
[380]961
[486]962property:
[444]963id:visualize
964name:Show reticulopodia and nutrients
[430]965type:d 0 1 0
[554]966group:
[430]967
[486]968property:
[434]969id:maxSteps
[554]970name:Maximum number of steps
[552]971type:d 0 10000000 0
[554]972group:
[432]973
[486]974property:
[567]975id:scalingFactor
976name:Scaling factor for micrometers
977type:f 0 -1 0.01
978group:
979
980property:
[554]981id:logging
982name:Log statistics to file
983type:d 0 1 0
984group:
985
986property:
[493]987id:logPref
988name:Log prefix
989type:s
990
991property:
[554]992id:secPerStep
993name:Seconds per simulation step
[568]994help:~
[570]995Number of seconds of foraminifera time per simulation step.
996Lower values mean smoother animation.~
[554]997type:f 1 480 300
998flags: 16
999group:
1000
1001property:
[474]1002id:foramSpeedMmPerMin
1003name:Speed of foraminfera in mm/min
[554]1004type:f 0.01 0.1 0.05
[474]1005flags: 16
1006group:Foraminifera
1007
[486]1008property:
[554]1009id:dir_change_sec
1010name:Number of seconds before direction change
1011type:d 300 300000 6000
[479]1012group:Foraminifera
1013
[486]1014property:
[554]1015id:foramPop
1016name:Initial forams population size
1017type:d 1 1000 20
[552]1018group:Foraminifera
1019
1020property:
[554]1021id:gametoPeriodSec
[479]1022name:Time of gametogenesis
[554]1023type:f 300 300000 21600
1024group:Reproduction
[479]1025
[486]1026property:
[554]1027id:gametSuccessRate
1028name:Ratio of successful gamets
1029type:f 0.0001 0.01 0.001
1030group:Reproduction
[479]1031
[486]1032property:
[554]1033id:divisionCost
1034name:Cost of division in pG
1035type:f 15 25 20
1036group:Reproduction
[474]1037
[486]1038property:
[554]1039id:min_repro_energ_haplo
[567]1040name:Min reproduction energy of haploid in pg
1041type:f 0 -1 350000
[554]1042group:Energy
[380]1043
[486]1044property:
[554]1045id:min_repro_energ_diplo
[567]1046name:Min reproduction energy of diploid in pg
1047type:f 0 -1 600000
[554]1048group:Energy
[479]1049
[486]1050property:
[554]1051id:repro_prob
1052name:Probability of reproduction
1053type:f 0 1 0.8
1054group:Reproduction
[380]1055
[486]1056property:
[554]1057id:energies0_haplo
1058name:Energy of offspring from diploid forams
1059type:f 0 -1 20
1060group:Energy
[479]1061
[486]1062property:
[554]1063id:energies0_diplo
1064name:Energy of offspring from diploid forams
1065type:f 0 -1 1.25
1066group:Energy
1067
1068property:
[567]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:
[554]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
[567]1101type:f 720 43200 43200
[479]1102group:Foraminifera
1103
[486]1104property:
[422]1105id:chamber_proculus_haplo
[423]1106name:Size of proculus
[422]1107type:f
[421]1108group:Foraminifera
[380]1109
[486]1110property:
[422]1111id:chamber_proculus_diplo
[423]1112name:Size of proculus
[422]1113type:f
[380]1114group:Foraminifera
1115
[486]1116property:
[422]1117id:hunted_prob
1118name:Probability of being hunted
1119type:f 0 1 0
[554]1120group:Foraminifera
[380]1121
[486]1122property:
[557]1123id:zone1_range
[556]1124name:Zone 1 range in frams units
[554]1125type:f 0 200 10
[422]1126group:Foraminifera
[380]1127
[486]1128property:
[422]1129id:zone2_range
[556]1130name:Zone 2 range in frams units
[554]1131type:f 0 3000 30
[421]1132group:Foraminifera
[380]1133
[486]1134property:
[554]1135id:chamberCostPerSec
1136name:Cost of growning chamber per second
1137type:f 0 1 0.000001
1138group:Energy
[380]1139
[486]1140property:
[475]1141id:e_death_level_haplo
1142name:Minimal level of energy to sustain life of haploid
[554]1143type:f 0 1 0.5
1144group:Energy
[475]1145
[486]1146property:
[475]1147id:e_death_level_diplo
1148name:Minimal level of energy to sustain life of diploid
[554]1149type:f 0 1 0.5
1150group:Energy
[475]1151
[486]1152property:
[422]1153id:energy_hib
1154name:Energy used for hibernation during one step
[554]1155type:f 0 1 0.0000001
1156group:Energy
[422]1157
[486]1158property:
[422]1159id:energy_move
1160name:Energy used for movement during one step
[554]1161type:f 0 1 0.0000005
1162group:Energy
[422]1163
[486]1164property:
[380]1165id:e_meta
1166name:Idle metabolism
[554]1167type:f 0 1 0.0000005
[380]1168group:Energy
[556]1169help:Foraminifera consumes this proportion of its energy in one time step
[380]1170
[486]1171property:
[554]1172id:ingestion
1173name:Ingestion rate
1174type:f 0 -1 0.25
1175group:Energy
1176
1177property:
[493]1178id:nutrient_pop
[554]1179name:Nutrient population
[474]1180type:f 0 1000000
[380]1181group:Energy
1182help:How fast energy is created in the world
1183
[486]1184property:
[554]1185id:energy_nut
1186name:Nutrient energy
1187type:f 0 10000000
[481]1188group:Energy
1189
[486]1190property:
[554]1191id:nutrientradius
1192name:Nutrient size
1193type:f 0.001 0.9 0.1
[479]1194group:Energy
1195
[486]1196property:
[554]1197id:picoCarbonPerMikro
[556]1198name:Picograms of carbon in cubic micrometer
[554]1199type:f 0 -1 0.13
[380]1200group:Energy
1201
[486]1202property:
[380]1203id:feedtrans
[479]1204name:Energy transfer per second
[554]1205type:f 0 1 0.001
[380]1206group:Energy
[422]1207
[486]1208property:
[479]1209id:foodperiod
1210name:Time between food occurrences
[554]1211type:f 0 1000000 14400
[422]1212group:Energy
1213
[486]1214property:
[554]1215id:foodPeriodChange
1216name:Set variable feed rate
1217type:f 0 -1 0
[421]1218group:Energy
1219
[486]1220property:
[422]1221id:stress
1222name:Environmental stress
1223type:d 0 1 1
[554]1224group:
[422]1225
[486]1226property:
[422]1227id:repro_trigger
1228name:Reproduction trigger
1229type:d 0 1 1
[554]1230group:Reproduction
[422]1231
[486]1232property:
[422]1233id:creath
1234name:Creation height
[554]1235type:f -1 50 -0.99
[422]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
[554]1243property:
1244id:autorestart
1245name:Restart after extinction
1246help:Restart automatically this experiment after the last creature has died?
1247type:d 0 1 0
1248
[421]1249state:
1250id:nutrient
[428]1251name:Nutrient locations
[421]1252help:vector of vectors [x,y,energy]
1253type:x
1254flags:32
1255
[380]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
[421]1266name:Evaluated Forams
1267help:Total number of the Forams evaluated in the experiment
[380]1268type:d
1269flags:16
Note: See TracBrowser for help on using the repository browser.