------------------------------------------------------------ revno: 2446 committer: Chiara Modenese <c...@engs-018373> branch nick: trunk timestamp: Fri 2010-09-24 17:34:26 +0100 message: 1. Introduce a class for returning or computing scalar values based on combination of 2 ids. [experimental, don't use or ask questions about it now] added: pkg/common/DataClass/MatchMaker.cpp pkg/common/DataClass/MatchMaker.hpp
-- lp:yade https://code.launchpad.net/~yade-dev/yade/trunk Your team Yade developers is subscribed to branch lp:yade. To unsubscribe from this branch go to https://code.launchpad.net/~yade-dev/yade/trunk/+edit-subscription
=== added file 'pkg/common/DataClass/MatchMaker.cpp' --- pkg/common/DataClass/MatchMaker.cpp 1970-01-01 00:00:00 +0000 +++ pkg/common/DataClass/MatchMaker.cpp 2010-09-24 16:34:26 +0000 @@ -0,0 +1,30 @@ +// 2010 © Václav Šmilauer <[email protected]> + +#include<yade/pkg-common/MatchMaker.hpp> +#include<boost/foreach.hpp> +#ifndef FOREACH + #define FOREACH BOOST_FOREACH +#endif + +YADE_PLUGIN((MatchMaker)); +MatchMaker::~MatchMaker(){} + +Real MatchMaker::operator()(int id1, int id2, Real val1, Real val2) const { + FOREACH(const Vector3r& m, matches){ + if(((int)m[0]==id1 && (int)m[1]==id2) || ((int)m[0]==id2 && (int)m[1]==id1)) return m[2]; + } + // no match + if(isnan(val1) || isnan(val2)) throw std::invalid_argument("MatchMaker: no match for ("+lexical_cast<string>(id1)+","+lexical_cast<string>(id2)+") and values for fallback computation not specified."); + return computeFallback(val1,val2); +} + +void MatchMaker::postLoad(MatchMaker&){} + +Real MatchMaker::computeFallback(Real v1, Real v2) const { + if(fallback=="val") return val; + if(fallback=="avg") return (v1+v2)/2.; + if(fallback=="min") return min(v1,v2); + if(fallback=="max") return max(v1,v2); + if(fallback=="harmAvg") return 2*v1*v2/(v1+v2); + throw std::invalid_argument("MatchMaker:: fallback '"+fallback+"' not recognized (possible values: val, avg, min, max, harmAvg)."); +} === added file 'pkg/common/DataClass/MatchMaker.hpp' --- pkg/common/DataClass/MatchMaker.hpp 1970-01-01 00:00:00 +0000 +++ pkg/common/DataClass/MatchMaker.hpp 2010-09-24 16:34:26 +0000 @@ -0,0 +1,40 @@ +// 2010 © Václav Šmilauer <[email protected]> + +#include<yade/lib-serialization/Serializable.hpp> +#include<string> + +namespace py = boost::python; + +/* Future optimizations, in postLoad: + +1. Use matches to update lookup table/hash for faster matching, instead of traversing matches every time +2. Use fallback to update fallbackPtr, instead of string-comparison of fallback every time + +*/ +class MatchMaker: public Serializable { + #if 0 + Real fallbackAvg(Real v1, Real v2) const{ return (v1+v2)/2.; } + Real fallbackMin(Real v1, Real v2) const{ return min(v1,v2); } + Real fallbackMax(Real v1, Real v2) const{ return max(v1,v2); } + Real fallbackHarmAvg(Real v1, Real v2) const { return 2*v1*v2/(v1+v2); } + Real fallbackVal(Real v1, Real v2) const { return val; } + Real (MatchMaker::*)(Real,Real) fallbackPtr; + #endif + public: + virtual ~MatchMaker(); + Real computeFallback(Real val1, Real val2) const ; + void postLoad(MatchMaker&); + // return value looking up matches for id1+id2 (the order is arbitrary) + // if no match is found, use val1,val2 and fallback strategy to compute new value. + // if no match is found and val1 or val2 are not given, throw exception + Real operator()(const int id1, const int id2, const Real val1=NaN, const Real val2=NaN) const; + YADE_CLASS_BASE_DOC_ATTRS_CTOR_PY(MatchMaker,Serializable,"Class matching pair of ids to return pre-defined or derived value of a scalar parameter.", + ((std::vector<Vector3r>,matches,,,"Array of ``(id1,id2,value)`` items; queries matching ``id1``+``id2`` or ``id2``+``id1`` will return ``value``")) + ((std::string,fallback,"avg",Attr::triggerPostLoad,"Alogorithm used to compute value when no match for ids is found. Possible values are\n* 'avg' (arithmetic average)\n* 'min' (minimum value)\n* 'max' (maximum value)\n* 'harmAvg' (harmonic average)\n* 'val' (return value specified by :yref:`val<MatchMaker.val>`\n")) + ((Real,val,NaN,,"Constant value returned if there is no match and :yref:`fallback<MatchMaker::fallback>` is ``val``")) + , /*ctor*/ + , /*py*/ .def("__call__",&MatchMaker::operator(),(py::arg("id1"),py::arg("id2"),py::arg("val1")=NaN,py::arg("val2")=NaN),"Ask the instance for scalar value for given pair *id1*,*id2* (the order is irrelevant). Optionally, *val1*, *val2* can be given so that if there is no :yref:`match<MatchMaker.matches>`, return value can be computed using given :yref:`fallback<MatchMaker.fallback>`. If there is no match and *val1*, *val2* are not given, an exception is raised.") + .def("computeFallback",&MatchMaker::computeFallback,(py::arg("val1"),py::arg("val2")),"Compute fallback value for *val1* and *val2*, using algorithm specified by :yref:`fallback<MatchMaker.fallback>`.") + ); +}; +REGISTER_SERIALIZABLE(MatchMaker);
_______________________________________________ Mailing list: https://launchpad.net/~yade-dev Post to : [email protected] Unsubscribe : https://launchpad.net/~yade-dev More help : https://help.launchpad.net/ListHelp

