Skip to content

Commit

Permalink
Update count_species_matches (#475)
Browse files Browse the repository at this point in the history
* Expose Shape::dimension_kind

* Return the default dimension even if no species attribute is defined

* Enable to pickle TrajectoryObservers

* Only count the number of unique sets

* Update the test for get_dimension_from_model
  • Loading branch information
kaizu authored May 2, 2020
1 parent f6e6923 commit 58f6746
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 61 deletions.
8 changes: 1 addition & 7 deletions ecell4/core/CompartmentSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,7 @@ Integer CompartmentSpaceVectorImpl::num_molecules(const Species& sp) const
for (species_map_type::const_iterator i(index_map_.begin());
i != index_map_.end(); ++i)
{
if (sexp.match((*i).first))
{
do
{
retval += num_molecules_[(*i).second];
} while (sexp.next());
}
retval += num_molecules_[(*i).second] * sexp.count((*i).first);
}
return retval;
}
Expand Down
37 changes: 28 additions & 9 deletions ecell4/core/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,36 @@ class rule_based_expression_matcher<std::vector<UnitSpecies> >
Integer count(const value_type& another)
{
context_type::variable_container_type globals;
if (!match(another, globals))

std::vector<context_type::iterator_container_type> results;
boost::optional<context_type> ctx = match(another, globals);

if (!ctx)
{
return 0;
}
results.push_back(ctx.get().iterators);
std::sort(results.back().begin(), results.back().end());

Integer n(1);
while (next())
while (ctx = next())
{
++n;
results.push_back(ctx.get().iterators);
std::sort(results.back().begin(), results.back().end());
}
return n;

return static_cast<Integer>(std::distance(results.begin(), std::unique(results.begin(), results.end())));

// if (!match(another, globals))
// {
// return 0;
// }

// Integer n(1);
// while (next())
// {
// ++n;
// }
// return n;
}

// const context_type& context() const
Expand Down Expand Up @@ -493,10 +512,10 @@ struct SpeciesExpressionMatcher
return (pttrn.match(sp) ? true : false);
}

bool next()
{
return (pttrn.next() ? true : false);
}
// bool next()
// {
// return (pttrn.next() ? true : false);
// }

size_t count(const Species& sp)
{
Expand Down
18 changes: 2 additions & 16 deletions ecell4/core/SubvolumeSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ Integer SubvolumeSpaceVectorImpl::num_molecules(const Species& sp) const
for (matrix_type::const_iterator i(matrix_.begin());
i != matrix_.end(); ++i)
{
if (sexp.match((*i).first))
{
do
{
// retval += std::accumulate((*i).second.begin(), (*i).second.end(), 0);
retval += (*i).second->num_molecules();
} while (sexp.next());
}
retval += (*i).second->num_molecules() * sexp.count((*i).first);
}
return retval;
}
Expand All @@ -43,14 +36,7 @@ Integer SubvolumeSpaceVectorImpl::num_molecules(
for (matrix_type::const_iterator i(matrix_.begin());
i != matrix_.end(); ++i)
{
if (sexp.match((*i).first))
{
do
{
// retval += (*i).second[c];
retval += (*i).second->num_molecules(c);
} while (sexp.next());
}
retval += (*i).second->num_molecules(c) * sexp.count((*i).first);
}
return retval;
}
Expand Down
7 changes: 4 additions & 3 deletions ecell4/core/extras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ get_dimension_from_model(const Species& species, const std::shared_ptr<Model>& m

if (!model->has_species_attribute(species))
{
std::stringstream ss;
ss << "The model has no attribute for Specis(\"" << species.serial() << "\")";
throw NotFound(ss.str());
// std::stringstream ss;
// ss << "The model has no attribute for Specis(\"" << species.serial() << "\")";
// throw NotFound(ss.str());
return DEFAULT_DIMENSION;
}

const Species& attribute(model->apply_species_attributes(species));
Expand Down
107 changes: 97 additions & 10 deletions ecell4/core/observers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ struct FixedIntervalEvent
public:

Real t0, dt;
Integer num_steps;
Integer count;
size_t num_steps;
size_t count;
};

template <typename Tevent_>
Expand Down Expand Up @@ -763,6 +763,24 @@ class TrajectoryObserver
;
}

TrajectoryObserver(
const std::vector<ParticleID>& pids,
const bool resolve_boundary,
const Real subdt,
const std::vector<Real3>& prev_positions,
const std::vector<std::vector<Real3> >& trajectories,
const std::vector<Real3>& strides,
const std::vector<Real>& t
)
: base_type(false), event_(), subevent_(subdt > 0 ? subdt : std::numeric_limits<Real>::infinity()),
pids_(pids), resolve_boundary_(resolve_boundary), prev_positions_(prev_positions),
trajectories_(trajectories), strides_(strides), t_(t)
{
assert(pids_.size() == prev_positions_.size());
assert(pids_.size() == trajectories_.size());
assert(pids_.size() == strides_.size());
}

virtual ~TrajectoryObserver()
{
;
Expand Down Expand Up @@ -842,21 +860,61 @@ class TrajectoryObserver
t_.clear();
}

const event_type& event() const
{
return event_;
}

void set_event(const event_type& event)
{
event_ = event;
}

const FixedIntervalEvent& subevent() const
{
return subevent_;
}

void set_subevent(const FixedIntervalEvent& subevent)
{
subevent_ = subevent;
}

const std::vector<ParticleID>& pids() const
{
return pids_;
}

const bool resolve_boundary() const
{
return resolve_boundary_;
}

const std::vector<Real3>& prev_positions() const
{
return prev_positions_;
}

const std::vector<std::vector<Real3> >& data() const
{
return trajectories_;
}

const Integer num_tracers() const
const std::vector<Real3>& strides() const
{
return pids_.size();
return strides_;
}

const std::vector<Real>& t() const
{
return t_;
}

const Integer num_tracers() const
{
return pids_.size();
}

protected:

void fire_event(const Simulator* sim, const std::shared_ptr<WorldInterface>& world)
Expand Down Expand Up @@ -963,7 +1021,8 @@ class FixedIntervalTrajectoryObserver
{
public:

typedef TrajectoryObserver<FixedIntervalEvent> base_type;
typedef FixedIntervalEvent event_type;
typedef TrajectoryObserver<event_type> base_type;

public:

Expand All @@ -985,6 +1044,21 @@ class FixedIntervalTrajectoryObserver
event_.set_dt(dt);
}

FixedIntervalTrajectoryObserver(
const Real& dt,
const std::vector<ParticleID>& pids,
const bool resolve_boundary,
const Real subdt,
const std::vector<Real3>& prev_positions,
const std::vector<std::vector<Real3> >& trajectories,
const std::vector<Real3>& strides,
const std::vector<Real>& times
)
: base_type(pids, resolve_boundary, subdt, prev_positions, trajectories, strides, times)
{
event_.set_dt(dt);
}

virtual ~FixedIntervalTrajectoryObserver()
{
;
Expand All @@ -996,24 +1070,37 @@ class TimingTrajectoryObserver
{
public:

typedef TrajectoryObserver<TimingEvent> base_type;
typedef TimingEvent event_type;
typedef TrajectoryObserver<event_type> base_type;

public:

TimingTrajectoryObserver(
const std::vector<Real>& t, const std::vector<ParticleID>& pids,
const bool resolve_boundary = default_resolve_boundary(),
const Real subdt = default_subdt())
: base_type(pids, resolve_boundary, subdt)
: base_type(pids, (subdt > 0), subdt)
{
event_.set_times(t);
}

TimingTrajectoryObserver(
const std::vector<Real>& t,
const bool resolve_boundary = default_resolve_boundary(),
const Real subdt = default_subdt())
: base_type(resolve_boundary, subdt)
: base_type((subdt > 0), subdt)
{
event_.set_times(t);
}

TimingTrajectoryObserver(
const std::vector<Real>& t,
const std::vector<ParticleID>& pids,
const Real subdt,
const std::vector<Real3>& prev_positions,
const std::vector<std::vector<Real3> >& trajectories,
const std::vector<Real3>& strides,
const std::vector<Real>& times
)
: base_type(pids, (subdt > 0), subdt, prev_positions, trajectories, strides, times)
{
event_.set_times(t);
}
Expand Down
3 changes: 2 additions & 1 deletion ecell4/core/tests/extras_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ BOOST_AUTO_TEST_CASE(DimensionAttributeTest)
BOOST_CHECK_EQUAL(extras::get_dimension_from_model(Species("B"), model), Shape::THREE);
BOOST_CHECK_EQUAL(extras::get_dimension_from_model(Species("C"), model), Shape::TWO);
BOOST_CHECK_EQUAL(extras::get_dimension_from_model(Species("D"), model), Shape::TWO);
BOOST_CHECK_THROW(extras::get_dimension_from_model(Species("E"), model), NotFound);
BOOST_CHECK_EQUAL(extras::get_dimension_from_model(Species("E"), model), Shape::THREE);
// BOOST_CHECK_THROW(extras::get_dimension_from_model(Species("E"), model), NotFound);
}

BOOST_AUTO_TEST_CASE(VersionInformationTest)
Expand Down
8 changes: 1 addition & 7 deletions ecell4/ode/ODEWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,7 @@ class ODEWorld
for (species_map_type::const_iterator i(index_map_.begin());
i != index_map_.end(); ++i)
{
if (sexp.match((*i).first))
{
do
{
retval += num_molecules_[(*i).second];
} while (sexp.next());
}
retval += num_molecules_[(*i).second] * sexp.count((*i).first);
}
return retval;
}
Expand Down
Loading

0 comments on commit 58f6746

Please sign in to comment.