[Debian-astro-commits] [gyoto] 105/221: Add new Property types string_t and filename_t, convert KerrKS, Minkowski and RotStar3_1 to Property-based objects

Thibaut Jean-Claude Paumard thibaut at moszumanska.debian.org
Fri May 22 20:52:37 UTC 2015


This is an automated email from the git hooks/post-receive script.

thibaut pushed a commit to branch master
in repository gyoto.

commit e5ed94416615058394c582c560f426c611c3a688
Author: Thibaut Paumard <paumard at users.sourceforge.net>
Date:   Tue Nov 25 17:44:50 2014 +0100

    Add new Property types string_t and filename_t, convert KerrKS, Minkowski and RotStar3_1 to Property-based objects
---
 include/GyotoKerrKS.h       |   6 +--
 include/GyotoMinkowski.h    |  10 ++--
 include/GyotoObject.h       |  44 ++++++++++++++++--
 include/GyotoRotStar3_1.h   |  11 +++--
 lib/KerrKS.C                |  27 +++--------
 lib/Minkowski.C             |  27 +++++------
 lib/NumericalMetricLorene.C |   2 +-
 lib/Object.C                | 108 +++++++++++++++++++++++++++++++-------------
 lib/RotStar3_1.C            |  47 ++++++++++---------
 9 files changed, 170 insertions(+), 112 deletions(-)

diff --git a/include/GyotoKerrKS.h b/include/GyotoKerrKS.h
index 3200d44..5f648ad 100644
--- a/include/GyotoKerrKS.h
+++ b/include/GyotoKerrKS.h
@@ -71,6 +71,7 @@ class Gyoto::Metric::KerrKS
   // Constructors - Destructor
   // -------------------------
  public: 
+  GYOTO_PROPERTY;
   KerrKS(); ///< Default constructor
   virtual KerrKS* clone () const;         ///< Copy constructor
   
@@ -115,11 +116,6 @@ class Gyoto::Metric::KerrKS
   virtual void circularVelocity(double const pos[4], double vel [4],
 				double dir=1.) const ;
 
-  virtual int setParameter(std::string, std::string, std::string);
-#ifdef GYOTO_USE_XERCES
-  virtual void fillElement(FactoryMessenger *fmp);
-#endif
-
  public:
 
   void MakeCst(const double* coord, double* cst) const;
diff --git a/include/GyotoMinkowski.h b/include/GyotoMinkowski.h
index fc5d5a8..e9d8832 100644
--- a/include/GyotoMinkowski.h
+++ b/include/GyotoMinkowski.h
@@ -51,15 +51,17 @@ class Gyoto::Metric::Minkowski
  private:
   
  public:
+  // Those are mere wrappers arround Generic::coordKind(), useful for
+  // declaring a boolen property using the macro GYOTO_PROPERTY_BOOL:
+  void spherical(bool);
+  bool spherical() const;
   // This is the bare minimum of what a Metric class must implement:
+  GYOTO_PROPERTY;
   Minkowski();
   virtual Minkowski* clone() const ;
+
   void gmunu(double g[4][4], const double * x) const ;
   int christoffel(double dst[4][4][4], const double * x) const ;
-  virtual int setParameter(std::string, std::string, std::string);
-#ifdef GYOTO_USE_XERCES
-  virtual void fillElement(FactoryMessenger *fmp); ///< called from Factory
-#endif
 
   // Those two are implemented as examples.
   double gmunu(const double * x, int mu, int nu) const ;
diff --git a/include/GyotoObject.h b/include/GyotoObject.h
index 9b3fb6f..1be811d 100644
--- a/include/GyotoObject.h
+++ b/include/GyotoObject.h
@@ -82,6 +82,24 @@ namespace Gyoto {
 	 (Gyoto::Property::get_double_unit_t)&class::fname,	\
          name##_ancestors)
 
+/// Define a Property of type Filename
+#define GYOTO_PROPERTY_FILENAME(class, name, fname, ancestor)	\
+  GYOTO_PROPERTY_MAKE_ANCESTORS(name, ancestor); \
+  Property const name \
+        (#name, \
+	   (Gyoto::Property::set_string_t)&class::fname,	\
+	   (Gyoto::Property::get_string_t)&class::fname,	\
+         name##_ancestors, true)
+
+/// Define a Property of type String
+#define GYOTO_PROPERTY_STRING(class, name, fname, ancestor)	\
+  GYOTO_PROPERTY_MAKE_ANCESTORS(name, ancestor); \
+  Property const name \
+        (#name, \
+	   (Gyoto::Property::set_string_t)&class::fname,	\
+	   (Gyoto::Property::get_string_t)&class::fname,	\
+         name##_ancestors, false)
+
 /// Define class::properties and class::getProperties() 
 #define GYOTO_PROPERTY_FINALIZE(class, property)		\
   Property const * const class::properties = &property;		\
@@ -114,10 +132,10 @@ class Gyoto::Object
   void set(Property const &p, bool val);
   void set(Property const &p, std::string const &val);
   //void set(std::string const pname, double val);
-  void get(Property const &p, double &val);
-  void get(Property const &p, double &val, std::string const &unit);
-  void get(Property const &p, bool &val);
-  void set(Property const &p, std::string &val);
+  void get(Property const &p, double &val) const ;
+  void get(Property const &p, double &val, std::string const &unit) const ;
+  void get(Property const &p, bool &val) const ;
+  void get(Property const &p, std::string &val) const ;
   //void get(std::string const pname, double &val);
   Property const * property(std::string const pname) const;
   virtual Property const * getProperties() const;
@@ -182,6 +200,10 @@ int MyKind::setParameter(std::string name, std::string content, std::string unit
 			    std::string content,
 			    std::string unit);
 
+  virtual void setParameter(Gyoto::Property const &p,
+			    std::string const &name,
+			    std::string const &content,
+			    std::string const &unit);
 
 };
 
@@ -194,7 +216,7 @@ class Gyoto::Property
  private:
 
  public:
-  enum type_e {double_t, long_t, bool_t};
+  enum type_e {double_t, long_t, bool_t, string_t, filename_t};
   std::string name;
   std::string name_false;
   int type;
@@ -207,15 +229,21 @@ class Gyoto::Property
   typedef long (Object::* get_long_t)() const;
   typedef void (Object::* set_bool_t)(bool val);
   typedef bool (Object::* get_bool_t)() const;
+  typedef void (Object::* set_string_t)(std::string const&);
+  typedef std::string (Object::* get_string_t)() const;
+  typedef void (Object::* set_fname_t)(std::string const&);
+  typedef std::string (Object::* get_fname_t)() const;
   union setter_t {
     set_double_t set_double;
     set_long_t set_long;
     set_bool_t set_bool;
+    set_string_t set_string;
   };
   union getter_t {
     get_double_t get_double;
     get_long_t get_long;
     get_bool_t get_bool;
+    get_string_t get_string;
   };
   union setter_unit_t {set_double_unit_t set_double;};
   union getter_unit_t {get_double_unit_t get_double;};
@@ -243,6 +271,12 @@ class Gyoto::Property
 	   get_bool_t get_bool,
 	   Property const * const * ancestors);
 
+  Property(std::string name,
+	   set_string_t set_string,
+	   get_string_t get_string,
+	   Property const * const * ancestors,
+	   bool is_filename=false);
+
   Property const * find(std::string name) const;
 
 };
diff --git a/include/GyotoRotStar3_1.h b/include/GyotoRotStar3_1.h
index 4f84353..a5342c2 100644
--- a/include/GyotoRotStar3_1.h
+++ b/include/GyotoRotStar3_1.h
@@ -60,7 +60,7 @@ class Gyoto::Metric::RotStar3_1 : public Gyoto::Metric::Generic {
   int integ_kind_;///< 1 if RotStar3_1::myrk4(), 0 if Metric::myrk4()
 
  public:
-
+  GYOTO_PROPERTY;
   RotStar3_1(); ///< Constructor
   RotStar3_1(const RotStar3_1& ) ;                ///< Copy constructor
   virtual ~RotStar3_1() ;        ///< Destructor
@@ -70,8 +70,13 @@ class Gyoto::Metric::RotStar3_1 : public Gyoto::Metric::Generic {
   void fileName(char const *); ///< Set filename_
   char const * fileName() const; ///< Get filename_
 
+  void file(std::string const &); ///< Set filename_
+  std::string file() const; ///< Get filename_
+
   void integKind(int); ///< Set integ_kind_
   int integKind() const ; ///< Get integ_kind_
+  void genericIntegrator(bool); ///< Set !integ_kind_
+  bool genericIntegrator() const ;///< Get !integ_kind_
 
   using Metric::Generic::myrk4;
 
@@ -127,10 +132,6 @@ class Gyoto::Metric::RotStar3_1 : public Gyoto::Metric::Generic {
 		    const double u1[4], const double u2[4]) const ;
 
   virtual int setParameter(std::string, std::string, std::string);
-#ifdef GYOTO_USE_XERCES
-  virtual void fillElement(FactoryMessenger *fmp); ///< called from Factory
-  virtual void setParameters(Gyoto::FactoryMessenger *fmp) ;
-#endif
 
 };
 
diff --git a/lib/KerrKS.C b/lib/KerrKS.C
index 2667fc8..c1a2e7d 100644
--- a/lib/KerrKS.C
+++ b/lib/KerrKS.C
@@ -35,6 +35,13 @@ using namespace std ;
 using namespace Gyoto ; 
 using namespace Gyoto::Metric ; 
 
+GYOTO_PROPERTY_DOUBLE(KerrKS, HorizonSecurity, horizonSecurity,
+		      Generic::properties);
+GYOTO_PROPERTY_BOOL(KerrKS, GenericIntegrator, SpecificIntegrator,
+		    genericIntegrator, &HorizonSecurity);
+GYOTO_PROPERTY_DOUBLE(KerrKS, Spin, spin, &GenericIntegrator);
+GYOTO_PROPERTY_FINALIZE(KerrKS, Spin);
+
 /*
 NOTA BENE: to improve KerrKS
 So far (March 2011) KerrKS is just a stub ; to improve it, it's necessary to imporve myrk4_adaptive, which is really obsolete (and longer!) as compared to KerrBL ; a check of cst of motion conservation should be implemented: so far, the norm behaves badly when approaching (not-so-)close to horizon, that's why drhor is chosen so big.
@@ -673,23 +680,3 @@ int KerrKS::isStopCondition(double const * const coord) const {
 
   return (r<rsink_ && rdot >0 && Tdot>0);
 }
-
-int KerrKS::setParameter(string name, string content, string unit) {
-  if (name=="Spin") spin(atof(content.c_str()));
-  else if (name=="HorizonSecurity") horizonSecurity(atof(content.c_str()));
-  else if (name=="GenericIntegrator") genericIntegrator(true);
-  else if (name=="SpecificIntegrator") genericIntegrator(false);
-  else return Generic::setParameter(name, content, unit);
-  return 0;
-}
-
-#ifdef GYOTO_USE_XERCES
-void KerrKS::fillElement(Gyoto::FactoryMessenger *fmp) {
-  fmp -> setParameter("Spin", spin_);
-  Metric::Generic::fillElement(fmp);
-  fmp -> setParameter("HorizonSecurity", drhor_);
-  fmp -> setParameter(generic_integrator_?
-		      "GenericIntegrator":
-		      "SpecificIntegrator");
-}
-#endif
diff --git a/lib/Minkowski.C b/lib/Minkowski.C
index 8aab3b6..68328cf 100644
--- a/lib/Minkowski.C
+++ b/lib/Minkowski.C
@@ -27,6 +27,13 @@ using namespace std ;
 using namespace Gyoto ; 
 using namespace Gyoto::Metric ; 
 
+// We create one property; its "parent" is the Generic Property list
+GYOTO_PROPERTY_BOOL(Minkowski, Spherical, Cartesian,
+		    spherical, Generic::properties);
+// We still need to connect this (single) property to
+// Minkowski::properties and define getProperties():
+GYOTO_PROPERTY_FINALIZE(Minkowski, Spherical);
+
 // This is the minimal constructor: it just sets the coordinate kind and
 // the metric kind name.
 Minkowski::Minkowski() :
@@ -202,22 +209,10 @@ void Minkowski::observerTetrad(string const obskind,
   Generic::observerTetrad(obskind,pos,fourvel,screen1,screen2,screen3);
 }
 
-
-
-// Fillelement is required to be able to export the Metric to an XML file.
-#ifdef GYOTO_USE_XERCES
-void Minkowski::fillElement(Gyoto::FactoryMessenger *fmp) {
-  fmp -> setParameter ((coordKind()==GYOTO_COORDKIND_SPHERICAL)?
-		       "Spherical":
-		       "Cartesian");
-  Generic::fillElement(fmp);
+void Minkowski::spherical(bool t) {
+  coordKind(t?GYOTO_COORDKIND_SPHERICAL:GYOTO_COORDKIND_CARTESIAN);
 }
-#endif
 
-// setParameter is the minimal interface to read data from the XML.
-int Minkowski::setParameter(string name, string content, string unit){
-  if      (name=="Spherical")     coordKind(GYOTO_COORDKIND_SPHERICAL);
-  else if (name=="Cartesian")     coordKind(GYOTO_COORDKIND_CARTESIAN);
-  else return Generic::setParameter(name, content, unit);
-  return 0;
+bool Minkowski::spherical() const {
+  return coordKind() == GYOTO_COORDKIND_SPHERICAL;
 }
diff --git a/lib/NumericalMetricLorene.C b/lib/NumericalMetricLorene.C
index e3061eb..20718e2 100644
--- a/lib/NumericalMetricLorene.C
+++ b/lib/NumericalMetricLorene.C
@@ -1586,7 +1586,7 @@ void NumericalMetricLorene::setParticleProperties(Worldline * line,
   line -> setCst(cst,3);
 }
 
-void  NumericalMetricLorene::setParameter(string name,
+int  NumericalMetricLorene::setParameter(string name,
 					  string content,
 					  string unit) {
   GYOTO_DEBUG << endl;
diff --git a/lib/Object.C b/lib/Object.C
index 6f09348..3db6577 100644
--- a/lib/Object.C
+++ b/lib/Object.C
@@ -45,11 +45,15 @@ void Object::set(Property const &p, bool val) {
 }
 
 void Object::set(Property const &p, string const &val) {
-  throwError("called");
-  if (p.type != Property::bool_t)
-    throwError("Property is no a bool");
-  cerr << "Object::set("<<p.name<<", "<<val<<")"<<std::endl<<std::endl;
-  set(p, val==p.name);
+  if (p.type == Property::bool_t) {
+    cerr << "Object::set("<<p.name<<", "<<val<<")"<<std::endl<<std::endl;
+    set(p, val==p.name);
+  } else if (   p.type == Property::string_t
+	     || p.type == Property::filename_t) {
+    Property::set_string_t set = p.setter.set_string;
+    if (!set) throwError("Can't set this Property");
+    (this->*set)(val);
+  } else throwError("Cannot set this Property from a string");
 }
 
 // void Object::set(std::string const pname, double val) {
@@ -58,7 +62,7 @@ void Object::set(Property const &p, string const &val) {
 //   set(*p, val);
 // }
 
-void Object::get(Property const &p, double &val) {
+void Object::get(Property const &p, double &val) const {
   if (p.type != Property::double_t)
     throwError("Property is no a double");
   Property::get_double_t get = p.getter.get_double;
@@ -66,7 +70,7 @@ void Object::get(Property const &p, double &val) {
   val = (this->*get)();
 }
 
-void Object::get(Property const &p, double &val, std::string const &unit) {
+void Object::get(Property const &p, double &val, std::string const &unit) const {
   if (p.type != Property::double_t)
     throwError("Property is no a double");
   Property::get_double_unit_t getu = p.getter_unit.get_double;
@@ -79,7 +83,7 @@ void Object::get(Property const &p, double &val, std::string const &unit) {
 }
 
 
-void Object::get(Property const &p, bool &val) {
+void Object::get(Property const &p, bool &val) const {
   if (p.type != Property::bool_t)
     throwError("Property is no a bool");
   Property::get_bool_t get = p.getter.get_bool;
@@ -87,13 +91,18 @@ void Object::get(Property const &p, bool &val) {
   val = (this->*get)();
 }
 
-void Object::set(Property const &p, string &val) {
-  if (p.type != Property::bool_t)
-    throwError("Property is no a bool");
-  bool bval;
-  get(p, bval);
-  if (bval) val=p.name;
-  else val=p.name_false;
+void Object::get(Property const &p, string &val) const {
+  if (p.type == Property::bool_t) {
+    bool bval;
+    get(p, bval);
+    if (bval) val=p.name;
+    else val=p.name_false;
+  } else if (   p.type == Property::string_t
+	     || p.type == Property::filename_t) {
+    Property::get_string_t get = p.getter.get_string;
+    if (!get) throwError("Can't get this Property as a string");
+    val = (this->*get)();
+  }
 }
 
 // void Object::get(std::string const pname, double &val) {
@@ -129,8 +138,16 @@ void Object::fillProperty(Gyoto::FactoryMessenger *fmp, Property const &p) {
       fmp->setParameter(name, val);
     }
     break;
+  case Property::string_t:
+  case Property::filename_t:
+    {
+      string val;
+      get(p, val);
+      fmp->setParameter(name, val);
+    }
+    break;
   default:
-    throwError("Unimplemented");
+    throwError("Property type unimplemented in Object::fillProperty()");
   }
   Property const * const * parent = p.parents;
   if (parent) {
@@ -150,29 +167,48 @@ void Object::fillElement(Gyoto::FactoryMessenger *fmp) {
 void Object::setParameters(Gyoto::FactoryMessenger *fmp)  {
   string name="", content="", unit="";
   if (fmp)
-    while (fmp->getNextParameter(&name, &content, &unit))
-      setParameter(name, content, unit);
+    while (fmp->getNextParameter(&name, &content, &unit)) {
+      cerr << "Setting '" << name << "' to '" << content
+	   << "' (unit='"<<unit<<"')" << endl;
+      Property const * prop = property(name);
+      if (!prop) {;
+	// The specific setParameter() implementation may well know
+	// this entity
+	setParameter(name, content, unit);
+      } else {
+	if (prop->type == Property::filename_t)
+	  content = fmp->fullPath(content);
+	setParameter(*prop, name, content, unit);
+      }
+    }
+  cerr << "Done processing parameters" << endl;
 }
 
 #endif
 
-int Object::setParameter(string name, string content, string unit) {
-  Property const * prop = property(name);
-  if (!prop) {
-    std::string errmsg="Object::setParameter(): no such property: ";
-    errmsg += name;
-    throwError(errmsg);
-  }
-  switch (prop->type) {
+void Object::setParameter(Property const &p, string const &name,
+			  string const & content, string const & unit) {
+  switch (p.type) {
   case Property::bool_t:
-    Object::set(*prop, name==prop->name);
-    return 0;
+    Object::set(p, name==p.name);
+    return;
   case Property::double_t:
-    set(*prop, atof(content.c_str()), unit);
-    return 0;
-  default:;
+    set(p, atof(content.c_str()), unit);
+    return;
+  case Property::filename_t:
+  case Property::string_t:
+    set(p, content);
+    return;
+  default:
+    throwError("Property type unimplemented in Object::setParameter()");
   }
-  return 1;
+}
+
+int Object::setParameter(string name, string content, string unit) {
+  Property const * prop = property(name);
+  if (!prop) return 1;
+  setParameter(*prop, name, content, unit);
+  return 0;
 }
 
 //// Property
@@ -203,6 +239,14 @@ Property::Property(string n, string nf, set_bool_t set, get_bool_t get,
   getter.get_bool=get;
 }
 
+Property::Property(string n, set_string_t set, get_string_t get,
+		   Property const * const * ancestors,
+		   bool is_filename)
+  : name(n), type(is_filename?filename_t:string_t), parents(ancestors) {
+  setter.set_string=set;
+  getter.get_string=get;
+}
+
 Property const * Property::find(std::string n) const {
   if (this == NULL) return NULL;
   if (name == n || (type == bool_t && name_false == n)) return this;
diff --git a/lib/RotStar3_1.C b/lib/RotStar3_1.C
index 6c5288e..9b338e1 100644
--- a/lib/RotStar3_1.C
+++ b/lib/RotStar3_1.C
@@ -43,6 +43,11 @@ using namespace Lorene;
 using namespace Gyoto;
 using namespace Gyoto::Metric;
 
+GYOTO_PROPERTY_FILENAME(RotStar3_1, File, file, Generic::properties);
+GYOTO_PROPERTY_BOOL(RotStar3_1, GenericIntegrator, SpecificIntegrator,
+		    genericIntegrator, &File);
+GYOTO_PROPERTY_FINALIZE(RotStar3_1, GenericIntegrator);
+
 RotStar3_1::RotStar3_1() : 
 Generic(GYOTO_COORDKIND_SPHERICAL, "RotStar3_1"),
   filename_(NULL),
@@ -78,6 +83,15 @@ RotStar3_1::~RotStar3_1()
   if (debug()) cout << "RotStar3_1 Destruction" << endl;
 }
 
+void RotStar3_1::file(std::string const &fname) {
+  cerr << "Setting file name to '" << fname << "'" << endl;
+  fileName(fname.c_str());
+}
+
+std::string RotStar3_1::file() const {
+  if (!filename_) return "";
+  return filename_;
+}
 
 void RotStar3_1::fileName(char const * lorene_res) {
   if (filename_) { delete[] filename_; filename_=NULL; }
@@ -110,6 +124,9 @@ char const * RotStar3_1::fileName() const { return filename_; }
 void RotStar3_1::integKind(int ik) { integ_kind_ = ik; }
 int RotStar3_1::integKind() const { return integ_kind_; }
 
+bool RotStar3_1::genericIntegrator() const {return !integ_kind_;}
+void RotStar3_1::genericIntegrator(bool t) {integ_kind_=!t;}
+
 int RotStar3_1::diff(const double coord[8], double res[8]) const
 {
   //4-DIMENSIONAL INTEGRATION
@@ -757,29 +774,11 @@ double RotStar3_1::ScalarProd(const double pos[4],
   return g_tt*u1[0]*u2[0]+g_rr*u1[1]*u2[1]+g_thth*u1[2]*u2[2]+g_pp*u1[3]*u2[3]+g_tp*u1[0]*u2[3]+g_tp*u1[3]*u2[0];
 }
 
-#ifdef GYOTO_USE_XERCES
-void RotStar3_1::fillElement(Gyoto::FactoryMessenger *fmp) {
-  if (filename_) fmp -> setParameter("File", filename_);
-  fmp -> setParameter("IntegKind", integ_kind_);
-  Generic::fillElement(fmp);
-}
-
-void RotStar3_1::setParameter(string name, string content, string unit){
-  if      (name=="IntegKind")     integKind(atoi(content.c_str()));
-  else if (name=="File")          fileName(content.c_str());
-  else return Generic::setParameter(name, content, unit);
+int RotStar3_1::setParameter(string name, string content, string unit){
+  if      (name=="IntegKind") {
+    GYOTO_WARNING << "IntegKind is deprecated, please use "
+      "<GenericIntegrator/> or <SpecificIntegrator/> instead\n";
+    integKind(atoi(content.c_str()));
+  } else return Generic::setParameter(name, content, unit);
   return 0;
 }
-
-void RotStar3_1::setParameters(FactoryMessenger* fmp) {
-  string name="", content="", unit="";
-  if (fmp) {
-    while (fmp->getNextParameter(&name, &content, &unit)) {
-      if (name == "File") content = fmp -> fullPath( content );
-      GYOTO_DEBUG << "Setting \"" << name << "\" to \"" << content
-		  << "\" (in unit \"" << unit << "\")\n";
-      setParameter(name, content, unit);
-    }
-  }
-}
-#endif

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-astro/packages/gyoto.git



More information about the Debian-astro-commits mailing list