Skip to content

Commit 58f6746

Browse files
authored
Update count_species_matches (#475)
* 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
1 parent f6e6923 commit 58f6746

File tree

8 files changed

+240
-61
lines changed

8 files changed

+240
-61
lines changed

ecell4/core/CompartmentSpace.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,7 @@ Integer CompartmentSpaceVectorImpl::num_molecules(const Species& sp) const
8383
for (species_map_type::const_iterator i(index_map_.begin());
8484
i != index_map_.end(); ++i)
8585
{
86-
if (sexp.match((*i).first))
87-
{
88-
do
89-
{
90-
retval += num_molecules_[(*i).second];
91-
} while (sexp.next());
92-
}
86+
retval += num_molecules_[(*i).second] * sexp.count((*i).first);
9387
}
9488
return retval;
9589
}

ecell4/core/Context.hpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,36 @@ class rule_based_expression_matcher<std::vector<UnitSpecies> >
164164
Integer count(const value_type& another)
165165
{
166166
context_type::variable_container_type globals;
167-
if (!match(another, globals))
167+
168+
std::vector<context_type::iterator_container_type> results;
169+
boost::optional<context_type> ctx = match(another, globals);
170+
171+
if (!ctx)
168172
{
169173
return 0;
170174
}
175+
results.push_back(ctx.get().iterators);
176+
std::sort(results.back().begin(), results.back().end());
171177

172-
Integer n(1);
173-
while (next())
178+
while (ctx = next())
174179
{
175-
++n;
180+
results.push_back(ctx.get().iterators);
181+
std::sort(results.back().begin(), results.back().end());
176182
}
177-
return n;
183+
184+
return static_cast<Integer>(std::distance(results.begin(), std::unique(results.begin(), results.end())));
185+
186+
// if (!match(another, globals))
187+
// {
188+
// return 0;
189+
// }
190+
191+
// Integer n(1);
192+
// while (next())
193+
// {
194+
// ++n;
195+
// }
196+
// return n;
178197
}
179198

180199
// const context_type& context() const
@@ -493,10 +512,10 @@ struct SpeciesExpressionMatcher
493512
return (pttrn.match(sp) ? true : false);
494513
}
495514

496-
bool next()
497-
{
498-
return (pttrn.next() ? true : false);
499-
}
515+
// bool next()
516+
// {
517+
// return (pttrn.next() ? true : false);
518+
// }
500519

501520
size_t count(const Species& sp)
502521
{

ecell4/core/SubvolumeSpace.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@ Integer SubvolumeSpaceVectorImpl::num_molecules(const Species& sp) const
1212
for (matrix_type::const_iterator i(matrix_.begin());
1313
i != matrix_.end(); ++i)
1414
{
15-
if (sexp.match((*i).first))
16-
{
17-
do
18-
{
19-
// retval += std::accumulate((*i).second.begin(), (*i).second.end(), 0);
20-
retval += (*i).second->num_molecules();
21-
} while (sexp.next());
22-
}
15+
retval += (*i).second->num_molecules() * sexp.count((*i).first);
2316
}
2417
return retval;
2518
}
@@ -43,14 +36,7 @@ Integer SubvolumeSpaceVectorImpl::num_molecules(
4336
for (matrix_type::const_iterator i(matrix_.begin());
4437
i != matrix_.end(); ++i)
4538
{
46-
if (sexp.match((*i).first))
47-
{
48-
do
49-
{
50-
// retval += (*i).second[c];
51-
retval += (*i).second->num_molecules(c);
52-
} while (sexp.next());
53-
}
39+
retval += (*i).second->num_molecules(c) * sexp.count((*i).first);
5440
}
5541
return retval;
5642
}

ecell4/core/extras.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ get_dimension_from_model(const Species& species, const std::shared_ptr<Model>& m
2121

2222
if (!model->has_species_attribute(species))
2323
{
24-
std::stringstream ss;
25-
ss << "The model has no attribute for Specis(\"" << species.serial() << "\")";
26-
throw NotFound(ss.str());
24+
// std::stringstream ss;
25+
// ss << "The model has no attribute for Specis(\"" << species.serial() << "\")";
26+
// throw NotFound(ss.str());
27+
return DEFAULT_DIMENSION;
2728
}
2829

2930
const Species& attribute(model->apply_species_attributes(species));

ecell4/core/observers.hpp

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,8 @@ struct FixedIntervalEvent
727727
public:
728728

729729
Real t0, dt;
730-
Integer num_steps;
731-
Integer count;
730+
size_t num_steps;
731+
size_t count;
732732
};
733733

734734
template <typename Tevent_>
@@ -763,6 +763,24 @@ class TrajectoryObserver
763763
;
764764
}
765765

766+
TrajectoryObserver(
767+
const std::vector<ParticleID>& pids,
768+
const bool resolve_boundary,
769+
const Real subdt,
770+
const std::vector<Real3>& prev_positions,
771+
const std::vector<std::vector<Real3> >& trajectories,
772+
const std::vector<Real3>& strides,
773+
const std::vector<Real>& t
774+
)
775+
: base_type(false), event_(), subevent_(subdt > 0 ? subdt : std::numeric_limits<Real>::infinity()),
776+
pids_(pids), resolve_boundary_(resolve_boundary), prev_positions_(prev_positions),
777+
trajectories_(trajectories), strides_(strides), t_(t)
778+
{
779+
assert(pids_.size() == prev_positions_.size());
780+
assert(pids_.size() == trajectories_.size());
781+
assert(pids_.size() == strides_.size());
782+
}
783+
766784
virtual ~TrajectoryObserver()
767785
{
768786
;
@@ -842,21 +860,61 @@ class TrajectoryObserver
842860
t_.clear();
843861
}
844862

863+
const event_type& event() const
864+
{
865+
return event_;
866+
}
867+
868+
void set_event(const event_type& event)
869+
{
870+
event_ = event;
871+
}
872+
873+
const FixedIntervalEvent& subevent() const
874+
{
875+
return subevent_;
876+
}
877+
878+
void set_subevent(const FixedIntervalEvent& subevent)
879+
{
880+
subevent_ = subevent;
881+
}
882+
883+
const std::vector<ParticleID>& pids() const
884+
{
885+
return pids_;
886+
}
887+
888+
const bool resolve_boundary() const
889+
{
890+
return resolve_boundary_;
891+
}
892+
893+
const std::vector<Real3>& prev_positions() const
894+
{
895+
return prev_positions_;
896+
}
897+
845898
const std::vector<std::vector<Real3> >& data() const
846899
{
847900
return trajectories_;
848901
}
849902

850-
const Integer num_tracers() const
903+
const std::vector<Real3>& strides() const
851904
{
852-
return pids_.size();
905+
return strides_;
853906
}
854907

855908
const std::vector<Real>& t() const
856909
{
857910
return t_;
858911
}
859912

913+
const Integer num_tracers() const
914+
{
915+
return pids_.size();
916+
}
917+
860918
protected:
861919

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

966-
typedef TrajectoryObserver<FixedIntervalEvent> base_type;
1024+
typedef FixedIntervalEvent event_type;
1025+
typedef TrajectoryObserver<event_type> base_type;
9671026

9681027
public:
9691028

@@ -985,6 +1044,21 @@ class FixedIntervalTrajectoryObserver
9851044
event_.set_dt(dt);
9861045
}
9871046

1047+
FixedIntervalTrajectoryObserver(
1048+
const Real& dt,
1049+
const std::vector<ParticleID>& pids,
1050+
const bool resolve_boundary,
1051+
const Real subdt,
1052+
const std::vector<Real3>& prev_positions,
1053+
const std::vector<std::vector<Real3> >& trajectories,
1054+
const std::vector<Real3>& strides,
1055+
const std::vector<Real>& times
1056+
)
1057+
: base_type(pids, resolve_boundary, subdt, prev_positions, trajectories, strides, times)
1058+
{
1059+
event_.set_dt(dt);
1060+
}
1061+
9881062
virtual ~FixedIntervalTrajectoryObserver()
9891063
{
9901064
;
@@ -996,24 +1070,37 @@ class TimingTrajectoryObserver
9961070
{
9971071
public:
9981072

999-
typedef TrajectoryObserver<TimingEvent> base_type;
1073+
typedef TimingEvent event_type;
1074+
typedef TrajectoryObserver<event_type> base_type;
10001075

10011076
public:
10021077

10031078
TimingTrajectoryObserver(
10041079
const std::vector<Real>& t, const std::vector<ParticleID>& pids,
1005-
const bool resolve_boundary = default_resolve_boundary(),
10061080
const Real subdt = default_subdt())
1007-
: base_type(pids, resolve_boundary, subdt)
1081+
: base_type(pids, (subdt > 0), subdt)
10081082
{
10091083
event_.set_times(t);
10101084
}
10111085

10121086
TimingTrajectoryObserver(
10131087
const std::vector<Real>& t,
1014-
const bool resolve_boundary = default_resolve_boundary(),
10151088
const Real subdt = default_subdt())
1016-
: base_type(resolve_boundary, subdt)
1089+
: base_type((subdt > 0), subdt)
1090+
{
1091+
event_.set_times(t);
1092+
}
1093+
1094+
TimingTrajectoryObserver(
1095+
const std::vector<Real>& t,
1096+
const std::vector<ParticleID>& pids,
1097+
const Real subdt,
1098+
const std::vector<Real3>& prev_positions,
1099+
const std::vector<std::vector<Real3> >& trajectories,
1100+
const std::vector<Real3>& strides,
1101+
const std::vector<Real>& times
1102+
)
1103+
: base_type(pids, (subdt > 0), subdt, prev_positions, trajectories, strides, times)
10171104
{
10181105
event_.set_times(t);
10191106
}

ecell4/core/tests/extras_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ BOOST_AUTO_TEST_CASE(DimensionAttributeTest)
3535
BOOST_CHECK_EQUAL(extras::get_dimension_from_model(Species("B"), model), Shape::THREE);
3636
BOOST_CHECK_EQUAL(extras::get_dimension_from_model(Species("C"), model), Shape::TWO);
3737
BOOST_CHECK_EQUAL(extras::get_dimension_from_model(Species("D"), model), Shape::TWO);
38-
BOOST_CHECK_THROW(extras::get_dimension_from_model(Species("E"), model), NotFound);
38+
BOOST_CHECK_EQUAL(extras::get_dimension_from_model(Species("E"), model), Shape::THREE);
39+
// BOOST_CHECK_THROW(extras::get_dimension_from_model(Species("E"), model), NotFound);
3940
}
4041

4142
BOOST_AUTO_TEST_CASE(VersionInformationTest)

ecell4/ode/ODEWorld.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,7 @@ class ODEWorld
171171
for (species_map_type::const_iterator i(index_map_.begin());
172172
i != index_map_.end(); ++i)
173173
{
174-
if (sexp.match((*i).first))
175-
{
176-
do
177-
{
178-
retval += num_molecules_[(*i).second];
179-
} while (sexp.next());
180-
}
174+
retval += num_molecules_[(*i).second] * sexp.count((*i).first);
181175
}
182176
return retval;
183177
}

0 commit comments

Comments
 (0)