Counting different cells

27 views
Skip to first unread message

αννα αλ

unread,
Sep 5, 2023, 12:55:13 AM9/5/23
to BioDynaMo Forum
Hello and thank you for your previews response. I would also like to know, how can I collect the number of agents with a specific density? I tried using :

auto cond = [](Cell* a) { return a->GetDensity() < 5; };
ts->AddCollector("agents_dn_5", new bdm::experimental::Counter<real_t>(cond));

but the Counter works only for Agents, even though Cell is a derived class of Agent. 
I also tried to manipulate the GetNumAgents function but the elements in the vectors(numa_agents) inside agents_ where also Agents and the GetDensity function did not apply. 

Is there any way to do this?

Nicolò Cogno

unread,
Sep 5, 2023, 4:11:17 AM9/5/23
to BioDynaMo Forum
You can cast the "Agent* a" to a "Cell*" in this way: 

auto cond = [ ](Agent* a) {
    return dynamic_cast<Cell*>(a)->GetDensity() < 5; 
  };

Have a look at this file, lines 140-155.

Tobias Duswald

unread,
Sep 5, 2023, 6:17:00 AM9/5/23
to BioDynaMo Forum
Hi,

First, we recommend not changing any code in the BioDynaMo source code, e.g., the GetNumAgents function. I do not exclude that some use cases require interacting with the core, but changing the code of the main repository may break parts of BioDynaMo if you are not careful. If you change parts of the core, ensure you run the unit tests after building/compiling BioDynaMo.

To define counters for a derived class of Agent checking for specific attributes, try the following:

namespace bdm {

using experimental::Counter;

// Define how to get the simulation time values for the TimeSeries
auto get_time = [](Simulation *sim) {
return static_cast<double>(sim->GetScheduler()->GetSimulatedTime());
};

// How to define a counter
auto count_a_given_criteria = [](Agent *a) { // receives pointer to agent
// Assuming you have a specific agent that inherits from Agent
auto *my_agent = dynamic_cast<MyAgent *>(a);
if (my_agent) {
// Check if the agent meets a certain criteria
return (my_agent->GetSomeAttribute() < some_value);
} else {
// If the agent is not of the type MyAgent, return false
return false;
}
};

ts->AddCollector("MyCounter", new Counter<double>(my_agent), get_time);

}


T
Reply all
Reply to author
Forward
0 new messages