1 /* -*- c++ -*- */
2 #ifndef FORCE_H
3 #define FORCE_H
4 
5 #include "Makeable.h"
6 #include "MakeableDefinition.h"
7 
8 #include <vector>
9 
10 namespace ProtoMol {
11 
12   class ForceGroup;
13   class ScalarStructure;
14   class GenericTopology;
15   class Vector3DBlock;
16   class CompareForce;
17   class TimeForce;
18   class ForceGroup;
19   //_________________________________________________________________ Force
20 
21   class Force : public Makeable{
22     // This class contains the definition of one force
23 
24     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25     // Constructors, destructors, assignment
26     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27   public:
Force()28     Force(){}
~Force()29     virtual ~Force(){}
30 
31     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32     // New methods of class Force
33     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34   public:
35 
numberOfBlocks(const GenericTopology *,const Vector3DBlock *)36     virtual unsigned int numberOfBlocks(const GenericTopology*,const Vector3DBlock*){return 1;}
37     //< Returns the number of blocks the actual force can be split up. To
38     //< run it sequential return 1. The number of blocks must match with the
39     //< number of next() calls in parallelEvaluate(){...} running with > 1 node.
40 
41     virtual std::string getKeyword() const =0;
42     //< Returns the actual keyword of the concrete force:
43     //<
44     //< Your force *Force has private inheritance from *ForceBase
45     //< and needs following virtual method:
46     //< virtual std::string getKeyword() const{return keyword;}
47     //<
48     //< Define all static and basics, which do not depend on templates in *ForceBase.
49 
50     Force* make(std::string& errMsg, const std::vector<Value>& values) const;
51     //< Factory method
52 
uncache()53     virtual void uncache(){};
54     //< Marks the cache as out of date, forces to call initialize
55 
56     virtual void addToForceGroup(ForceGroup* forceGroup)=0;
57     //< Adds the force to corresponding force subgroups System or Extended
58 
59     virtual CompareForce* makeCompareForce(Force* actualForce, CompareForce* compareForce) const;
60     //< Creating the right instance of a ComparForce object.
61 
62     virtual TimeForce* makeTimeForce(Force* actualForce) const;
63     //< Creating the right instance of a TimeForce object.
64 
65     void setParameters(std::string& errMsg, std::vector<Value> value);
66     //< update of parameters
67 
68     template<class T>
setParameter(std::string & errMsg,const std::string & key,T val)69     void setParameter(std::string& errMsg, const std::string& key, T val){
70       errMsg = "";
71       std::vector<Parameter> parameters;
72       getParameters(parameters);
73       std::vector<Value> values(parameters.size());
74       bool update = false;
75       for(unsigned int i = 0;i<parameters.size();i++){
76 	values[i] = parameters[i].value;
77 	if(equalNocase(key,parameters[i].keyword) && parameters[i].value != val){
78 	  values[i] = val;
79 	  update = true;
80 	}
81       }
82       if(update)
83 	setParameters(errMsg,values);
84     }
85     //< update of parameters with given value which match the keyword
86 
87     template<class T>
setParameter(std::string & errMsg,unsigned int index,T val)88     void setParameter(std::string& errMsg, unsigned int index, T val){
89       errMsg = "";
90       std::vector<Parameter> parameters;
91       getParameters(parameters);
92       std::vector<Value> values(parameters.size());
93       bool update = false;
94       for(unsigned int i = 0;i<parameters.size();i++){
95 	values[i] = parameters[i].value;
96 	if(i == index && parameters[i].value != val){
97 	  values[i] = val;
98 	  update = true;
99 	}
100       }
101       if(update)
102 	setParameters(errMsg,values);
103     }
104     //< update of parameter index with given value
105 
106   private:
107     virtual Force* doMake(std::string& errMsg, std::vector<Value> values) const=0;
108     //< implementation of make and actual instantiation of object
109 
110   private:
doSetParameters(std::string &,std::vector<Value>)111     virtual void doSetParameters(std::string& , std::vector<Value> ){Report::report << Report::error << "setParameters not implemented for force "<<this->getId()<<Report::endr;};
112   protected:
113     template<class T>
setParametersBySwapping(T * obj,std::string & errMsg,std::vector<Value> values)114     void setParametersBySwapping(T* obj, std::string& errMsg, std::vector<Value> values){
115       T* tmp = dynamic_cast<T*>(obj->make(errMsg,values));
116       if(tmp != NULL){
117 	std::swap(*obj,*tmp);
118 	delete tmp;
119       }
120     }
121 
122     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123     // From class Makable
124     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125   public:
getScope()126     virtual std::string getScope() const{return scope;}
127 
128     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129     // My data members
130     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131   public:
132     static const std::string scope;
133   };
134 
135   //______________________________________________________________________ INLINES
136 }
137 #endif /* FORCE_H */
138