Physics Engine -> Create new agents at runtime

30 views
Skip to first unread message

Benjamin Mewes

unread,
Feb 26, 2019, 7:26:46 AM2/26/19
to gama-p...@googlegroups.com
Dear all,

I do some experiments with the physics engine. If I create all necessary agents at the init, my model works (limited to 1.000 agents). If I add them at runtime, they are created but the physics are not calculated anymore. Any ideas?

Update: I got it working for some cycles, then it crashes with an OutOfIndex error for the world.

Here is the new code:

/**
* Name: geometrie
* Author: Mewes
* */

model geometrie



global {

//Variables
float globalWaterTransport <- 0.3; // mm³
int dimension <- 100;

//Physics engine
physic_world world2;

init {

create ground {
location <- { dimension / 2, dimension / 2, 0 };
collisionBound <- ["shape"::"floor", "x"::dimension / 2, "y"::dimension / 2, "z"::0];
shape <- rectangle({ dimension, dimension });
mass <- 0.0;
}

create Tube number: 1 {
shape <- rectangle({ 100, 25 });
mass <- 0.0;
location <- {50,50,0};
collisionBound <- ["shape"::"floor","x"::100, "y":: 25/2, "z"::10];

}


create WaterAgent number: 10 {
location <- {rnd(dimension),rnd(dimension),50};
collisionBound <- ["shape"::"sphere", "radius"::waterTransportPerAgent];
mass <- waterTransportPerAgent;
color <- #yellow;
}

create physic_world {
world2 <- self;

ask world2 {
agents <- (WaterAgent as list)+(Tube as list)+(ground as list);
}

world2.gravity <- true;
}


}

reflex createWatAgents {
if (cycle > 100){

ask world2 {
create WaterAgent number: 2 {
location <- {rnd(dimension),rnd(dimension),50};
collisionBound <- ["shape"::"sphere", "radius"::waterTransportPerAgent];
mass <- waterTransportPerAgent;
color <- #red;
}
}

ask world2 {
agents <- nil;
agents <- (WaterAgent as list)+(Tube as list)+(ground as list);
}


}
}

reflex getPressureHeight {
ask Environment {do WaterInside;}
}


reflex computeForces {
ask world2 {
do compute_forces step: 1;
}

}
}

species Tube skills: [physics] {
aspect cube {
draw shape depth: 10 color:#blue ;
}
}

species ground skills: [physics] {
aspect default {
draw shape color: rgb(100, 20, 20);
}

}

species WaterAgent skills: [physics] {
float waterTransportPerAgent <- globalWaterTransport;
rgb color;

reflex checkifdead {
if (location.z < -1) {
do die;
}

if (location.x < -1) {
do die;
}

if (location.x > 101) {
do die;
}

}

aspect sphere{
draw sphere(waterTransportPerAgent) color: color ;
}

}

species physic_world parent: physical_world ;


grid Environment height: 100 width: 100 neighbors: 8 {
float pressureHeigth;
int countWaterAgents;

action WaterInside {

countWaterAgents <- 0;

ask WaterAgent inside self {
myself.countWaterAgents <- myself.countWaterAgents + 1;
}

pressureHeigth <-float(countWaterAgents) * globalWaterTransport;

}
}

experiment Init type: gui {
output {
display cells type:opengl draw_env: true {
species ground;
species Tube aspect:cube;
species WaterAgent aspect:sphere;
}
}
}

Patrick Taillandier

unread,
Feb 26, 2019, 12:53:06 PM2/26/19
to gama-p...@googlegroups.com
Hi,

I clean a bit the model, and now it works normally. However, there is a current limitation concerning the number of objects that can be added to the physical engine (something like 1000 agents). I just commit a fix that enables to increase the max number of maximal agents (something like 16 000 objects) and the performance of the physical engine when adding agents.

Cheers,

Patrick

/***
* Name: physicEng
* Author: admin_ptaillandie
* Description: 
* Tags: Tag1, Tag2, TagN
***/

model physicEng




global {

        //Variables
        float globalWaterTransport <- 0.3; // mm³
        int dimension <- 100;

        //Physics engine
        physic_world world2;

        init {

                create ground {
                        location <- { dimension / 2, dimension / 2, 0 };
                        collisionBound <- ["shape"::"floor", "x"::dimension / 2, "y"::dimension / 2, "z"::0];
                        shape <- rectangle({ dimension, dimension });
                        mass <- 0.0; 
                }

                create Tube number: 1 {
                        shape <- rectangle({ 100, 25 });
                        mass <- 0.0;
                        location <-  {50,50,0};
                        collisionBound <-  ["shape"::"floor","x"::100, "y":: 25/2, "z"::10];

                }


                 create WaterAgent number: 100 {
                        location <-  {rnd(dimension),rnd(dimension),50};
                        collisionBound <- ["shape"::"sphere", "radius"::waterTransportPerAgent];
                        mass <- waterTransportPerAgent;
                        color <- #yellow;
                }

                create physic_world {
                        world2 <- self;
  agents <- (WaterAgent as list)+(Tube as list)+(ground as list);
                        gravity <- true;
                }


        }

        reflex createWatAgents {
                if (every(100 #cycle)){
                     create WaterAgent number: 10 {
                         location <-  {rnd(dimension),rnd(dimension),50};
                         collisionBound <- ["shape"::"sphere", "radius"::waterTransportPerAgent];
                         mass <- waterTransportPerAgent;
                         color <- #red;
                                             
                     }
                     world2.agents <- (WaterAgent as list)+(Tube as list)+(ground as list);
}       
                         
        }




        reflex computeForces {
                ask world2 {
                        do compute_forces step: 1.0;
/**
* Name: geometrie
* Author: Mewes
* Description: Deichgeometrie
* Tags: Deich, Physics

* */

model geometrie



global {

        //Variables
        float globalWaterTransport <- 0.3; // mm³
        int dimension <- 100;

        //Physics engine
        physic_world world2;

        init {

                create ground {
                        location <- { dimension / 2, dimension / 2, 0 };
                        collisionBound <- ["shape"::"floor", "x"::dimension / 2, "y"::dimension / 2, "z"::0];
                        shape <- rectangle({ dimension, dimension });
                        mass <- 0.0;
                }

                create Tube number: 1 {
                        shape <- rectangle({ 100, 25 });
                        mass <- 0.0;
                        location <-  {50,50,0};
                        collisionBound <-  ["shape"::"floor","x"::100, "y":: 25/2, "z"::10];

                }


                create WaterAgent number: 1000 {

                        location <-  {rnd(dimension),rnd(dimension),50};
                        collisionBound <- ["shape"::"sphere", "radius"::waterTransportPerAgent];
                        mass <- waterTransportPerAgent;
                        color <- #yellow;
                }

                create physic_world {
                        world2 <- self;

                        ask world2 {
                                agents <- (WaterAgent as list)+(Tube as list)+(ground as list);
                        }

                        world2.gravity <- true;
                }


        }

        reflex createWatAgents {
                if (cycle > 300){

                        create WaterAgent number: 10 {
                                location <-  {rnd(dimension),rnd(dimension),50};
                                collisionBound <- ["shape"::"sphere", "radius"::waterTransportPerAgent];
                                mass <- waterTransportPerAgent;
                                color <- #red;
                                write name;                             
                        }



                        ask world2{
                                write name + " dies";
                                do die;

                        }

                        create physic_world {
                                world2 <- self;

                                ask world2 {
                                        agents <- (WaterAgent as list)+(Tube as list)+(ground as list);
                                        }

                                world2.gravity <- true;
                        }

                        write "world recreated";
experiment Init type: gui {
        output {
                display cells type:opengl draw_env: true  {
                                species ground;
                                species Tube aspect:cube;
                                species WaterAgent aspect:sphere;
                                }                                                                       
                }
}

--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-platfor...@googlegroups.com.
To post to this group, send email to gama-p...@googlegroups.com.
Visit this group at https://groups.google.com/group/gama-platform.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Patrick Taillandier

unread,
Feb 27, 2019, 9:37:48 AM2/27/19
to gama-p...@googlegroups.com
Hi,

You can try to create a new workspace.

Patrick

Le mer. 27 févr. 2019 à 08:47, Benjamin Mewes <bennam...@gmail.com> a écrit :
Dear Patrick,

thanks for your fix! I fetched the newest GIT but the models don't open in the gaml editor. The canvas remains grey. Is this a common error?

Benjamin Mewes

unread,
Mar 6, 2019, 4:47:24 AM3/6/19
to GAMA
I just had to update XTEXT. So, I got it working.
Reply all
Reply to author
Forward
0 new messages