1 /*
2 *	Copyright (C) 2008-2012 Thorsten Liebig (Thorsten.Liebig@gmx.de)
3 *
4 *	This program is free software: you can redistribute it and/or modify
5 *	it under the terms of the GNU Lesser General Public License as published
6 *	by the Free Software Foundation, either version 3 of the License, or
7 *	(at your option) any later version.
8 *
9 *	This program is distributed in the hope that it will be useful,
10 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *	GNU Lesser General Public License for more details.
13 *
14 *	You should have received a copy of the GNU Lesser General Public License
15 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "tinyxml.h"
19 
20 #include "CSPropExcitation.h"
21 
CSPropExcitation(ParameterSet * paraSet,unsigned int number)22 CSPropExcitation::CSPropExcitation(ParameterSet* paraSet,unsigned int number) : CSProperties(paraSet) {Type=EXCITATION;Init();uiNumber=number;}
CSPropExcitation(CSProperties * prop)23 CSPropExcitation::CSPropExcitation(CSProperties* prop) : CSProperties(prop) {Type=EXCITATION;Init();}
CSPropExcitation(unsigned int ID,ParameterSet * paraSet)24 CSPropExcitation::CSPropExcitation(unsigned int ID, ParameterSet* paraSet) : CSProperties(ID,paraSet) {Type=EXCITATION;Init();}
~CSPropExcitation()25 CSPropExcitation::~CSPropExcitation() {}
26 
SetNumber(unsigned int val)27 void CSPropExcitation::SetNumber(unsigned int val) {uiNumber=val;}
GetNumber()28 unsigned int CSPropExcitation::GetNumber() {return uiNumber;}
29 
SetExcitType(int val)30 void CSPropExcitation::SetExcitType(int val) {iExcitType=val;}
GetExcitType()31 int CSPropExcitation::GetExcitType() {return iExcitType;}
32 
SetExcitation(double val,int Component)33 void CSPropExcitation::SetExcitation(double val, int Component)
34 {
35 	if ((Component<0) || (Component>=3)) return;
36 	Excitation[Component].SetValue(val);
37 }
38 
SetExcitation(const std::string val,int Component)39 void CSPropExcitation::SetExcitation(const std::string val, int Component)
40 {
41 	if ((Component<0) || (Component>=3)) return;
42 	Excitation[Component].SetValue(val);
43 }
44 
GetExcitation(int Component)45 double CSPropExcitation::GetExcitation(int Component)
46 {
47 	if ((Component<0) || (Component>=3)) return 0;
48 	return Excitation[Component].GetValue();
49 }
50 
GetExcitationString(int Comp)51 const std::string CSPropExcitation::GetExcitationString(int Comp)
52 {
53 	if ((Comp<0) || (Comp>=3)) return NULL;
54 	return Excitation[Comp].GetString();
55 }
56 
SetActiveDir(bool active,int Component)57 void CSPropExcitation::SetActiveDir(bool active, int Component)
58 {
59 	if ((Component<0) || (Component>=3)) return;
60 	ActiveDir[Component]=active;
61 }
62 
GetActiveDir(int Component)63 bool CSPropExcitation::GetActiveDir(int Component)
64 {
65 	if ((Component<0) || (Component>=3)) return false;
66 	return ActiveDir[Component];
67 }
68 
SetWeightFunction(const std::string fct,int ny)69 int CSPropExcitation::SetWeightFunction(const std::string fct, int ny)
70 {
71 	if ((ny>=0) && (ny<3))
72 		return WeightFct[ny].SetValue(fct);
73 	return 0;
74 }
75 
GetWeightFunction(int ny)76 const std::string CSPropExcitation::GetWeightFunction(int ny) {if ((ny>=0) && (ny<3)) {return WeightFct[ny].GetString();} else return std::string();}
77 
GetWeightedExcitation(int ny,const double * coords)78 double CSPropExcitation::GetWeightedExcitation(int ny, const double* coords)
79 {
80 	if ((ny<0) || (ny>=3)) return 0;
81 	//Warning: this is not reentrant....!!!!
82 	double loc_coords[3] = {coords[0],coords[1],coords[2]};
83 	double r,rho,alpha,theta;
84 	if (coordInputType==1)
85 	{
86 		loc_coords[0] = coords[0]*cos(coords[1]);
87 		loc_coords[1] = coords[0]*sin(coords[1]);
88 		rho = coords[0];
89 		alpha=coords[1];
90 		r = sqrt(pow(coords[0],2)+pow(coords[2],2));
91 		theta=asin(1)-atan(coords[2]/rho);
92 	}
93 	else
94 	{
95 		alpha=atan2(coords[1],coords[0]);
96 		rho = sqrt(pow(coords[0],2)+pow(coords[1],2));
97 		r = sqrt(pow(coords[0],2)+pow(coords[1],2)+pow(coords[2],2));
98 		theta=asin(1)-atan(coords[2]/rho);
99 	}
100 	coordPara[0]->SetValue(loc_coords[0]);
101 	coordPara[1]->SetValue(loc_coords[1]);
102 	coordPara[2]->SetValue(loc_coords[2]);
103 	coordPara[3]->SetValue(rho); //rho
104 	coordPara[4]->SetValue(r); //r
105 	coordPara[5]->SetValue(alpha);
106 	coordPara[6]->SetValue(theta); //theta
107 	int EC = WeightFct[ny].Evaluate();
108 	if (EC)
109 	{
110 		std::cerr << "CSPropExcitation::GetWeightedExcitation: Error evaluating the weighting function (ID: " << this->GetID() << ", n=" << ny << "): " << PSErrorCode2Msg(EC) << std::endl;
111 	}
112 
113 	return WeightFct[ny].GetValue()*GetExcitation(ny);
114 }
115 
SetDelay(double val)116 void CSPropExcitation::SetDelay(double val)	{Delay.SetValue(val);}
117 
SetDelay(const std::string val)118 void CSPropExcitation::SetDelay(const std::string val) {Delay.SetValue(val);}
119 
GetDelay()120 double CSPropExcitation::GetDelay(){return Delay.GetValue();}
121 
GetDelayString()122 const std::string CSPropExcitation::GetDelayString(){return Delay.GetString();}
123 
Init()124 void CSPropExcitation::Init()
125 {
126 	uiNumber=0;
127 	iExcitType=1;
128 	coordInputType=UNDEFINED_CS;
129 	m_Frequency.SetValue(0.0);
130 	for (unsigned int i=0;i<3;++i)
131 	{
132 		ActiveDir[i]=true;
133 		Excitation[i].SetValue(0.0);
134 		Excitation[i].SetParameterSet(clParaSet);
135 		WeightFct[i].SetValue(1.0);
136 		WeightFct[i].SetParameterSet(coordParaSet);
137 		Delay.SetValue(0.0);
138 		Delay.SetParameterSet(clParaSet);
139 	}
140 }
141 
SetPropagationDir(double val,int Component)142 void CSPropExcitation::SetPropagationDir(double val, int Component)
143 {
144 	if ((Component<0) || (Component>=3)) return;
145 	PropagationDir[Component].SetValue(val);
146 }
147 
SetPropagationDir(const std::string val,int Component)148 void CSPropExcitation::SetPropagationDir(const std::string val, int Component)
149 {
150 	if ((Component<0) || (Component>=3)) return;
151 	PropagationDir[Component].SetValue(val);
152 }
153 
GetPropagationDir(int Component)154 double CSPropExcitation::GetPropagationDir(int Component)
155 {
156 	if ((Component<0) || (Component>=3)) return 0;
157 	return PropagationDir[Component].GetValue();
158 }
159 
GetPropagationDirString(int Comp)160 const std::string CSPropExcitation::GetPropagationDirString(int Comp)
161 {
162 	if ((Comp<0) || (Comp>=3)) return NULL;
163 	return PropagationDir[Comp].GetString();
164 }
165 
166 
Update(std::string * ErrStr)167 bool CSPropExcitation::Update(std::string *ErrStr)
168 {
169 	bool bOK=true;
170 	int EC=0;
171 	for (unsigned int i=0;i<3;++i)
172 	{
173 		EC=Excitation[i].Evaluate();
174 		if (EC!=ParameterScalar::PS_NO_ERROR) bOK=false;
175 		if ((EC!=ParameterScalar::PS_NO_ERROR)  && (ErrStr!=NULL))
176 		{
177 			std::stringstream stream;
178 			stream << std::endl << "Error in Excitation-Property Excitaion-Value (ID: " << uiID << "): ";
179 			ErrStr->append(stream.str());
180 			PSErrorCode2Msg(EC,ErrStr);
181 		}
182 		EC=PropagationDir[i].Evaluate();
183 		if (EC!=ParameterScalar::PS_NO_ERROR) bOK=false;
184 		if ((EC!=ParameterScalar::PS_NO_ERROR)  && (ErrStr!=NULL))
185 		{
186 			std::stringstream stream;
187 			stream << std::endl << "Error in Excitation-Property PropagationDir-Value (ID: " << uiID << "): ";
188 			ErrStr->append(stream.str());
189 			PSErrorCode2Msg(EC,ErrStr);
190 		}
191 	}
192 	EC=m_Frequency.Evaluate();
193 	if (EC!=ParameterScalar::PS_NO_ERROR) bOK=false;
194 	if ((EC!=ParameterScalar::PS_NO_ERROR)  && (ErrStr!=NULL))
195 	{
196 		std::stringstream stream;
197 		stream << std::endl << "Error in Excitation-Property Frequency-Value";
198 		ErrStr->append(stream.str());
199 		PSErrorCode2Msg(EC,ErrStr);
200 	}
201 	EC=Delay.Evaluate();
202 	if (EC!=ParameterScalar::PS_NO_ERROR) bOK=false;
203 	if ((EC!=ParameterScalar::PS_NO_ERROR)  && (ErrStr!=NULL))
204 	{
205 		std::stringstream stream;
206 		stream << std::endl << "Error in Excitation-Property Delay-Value";
207 		ErrStr->append(stream.str());
208 		PSErrorCode2Msg(EC,ErrStr);
209 	}
210 	return bOK;
211 }
212 
Write2XML(TiXmlNode & root,bool parameterised,bool sparse)213 bool CSPropExcitation::Write2XML(TiXmlNode& root, bool parameterised, bool sparse)
214 {
215 	if (CSProperties::Write2XML(root,parameterised,sparse) == false) return false;
216 	TiXmlElement* prop=root.ToElement();
217 	if (prop==NULL) return false;
218 
219 	prop->SetAttribute("Number",(int)uiNumber);
220 	WriteTerm(m_Frequency,*prop,"Frequency",parameterised);
221 	WriteTerm(Delay,*prop,"Delay",parameterised);
222 
223 	prop->SetAttribute("Type",iExcitType);
224 	WriteVectorTerm(Excitation,*prop,"Excite",parameterised);
225 
226 	TiXmlElement Weight("Weight");
227 	WriteTerm(WeightFct[0],Weight,"X",parameterised);
228 	WriteTerm(WeightFct[1],Weight,"Y",parameterised);
229 	WriteTerm(WeightFct[2],Weight,"Z",parameterised);
230 	prop->InsertEndChild(Weight);
231 
232 	WriteVectorTerm(PropagationDir,*prop,"PropDir",parameterised);
233 
234 	return true;
235 }
236 
ReadFromXML(TiXmlNode & root)237 bool CSPropExcitation::ReadFromXML(TiXmlNode &root)
238 {
239 	if (CSProperties::ReadFromXML(root)==false) return false;
240 
241 	TiXmlElement *prop = root.ToElement();
242 	if (prop==NULL) return false;
243 
244 	int iHelp;
245 	if (prop->QueryIntAttribute("Number",&iHelp)!=TIXML_SUCCESS) uiNumber=0;
246 	else uiNumber=(unsigned int)iHelp;
247 
248 	if (prop->QueryIntAttribute("Type",&iExcitType)!=TIXML_SUCCESS) return false;
249 
250 	if (ReadVectorTerm(Excitation,*prop,"Excite",0.0)==false) return false;
251 
252 	ReadTerm(m_Frequency,*prop,"Frequency");
253 	ReadTerm(Delay,*prop,"Delay");
254 
255 	TiXmlElement *weight = prop->FirstChildElement("Weight");
256 	if (weight!=NULL)
257 	{
258 		ReadTerm(WeightFct[0],*weight,"X");
259 		ReadTerm(WeightFct[1],*weight,"Y");
260 		ReadTerm(WeightFct[2],*weight,"Z");
261 	}
262 
263 	ReadVectorTerm(PropagationDir,*prop,"PropDir",0.0);
264 
265 	return true;
266 }
267 
ShowPropertyStatus(std::ostream & stream)268 void CSPropExcitation::ShowPropertyStatus(std::ostream& stream)
269 {
270 	CSProperties::ShowPropertyStatus(stream);
271 	stream << " --- Excitation Properties --- " << std::endl;
272 	stream << "  Type: " << iExcitType << std::endl;
273 	stream << "  Active directions: " << ActiveDir[0] << "," << ActiveDir[1] << "," << ActiveDir[2] << std::endl;
274 	stream << "  Excitation\t: " << Excitation[0].GetValueString() << ", "  << Excitation[1].GetValueString() << ", "  << Excitation[2].GetValueString()  << std::endl;
275 	stream << "  Weighting\t: " << WeightFct[0].GetValueString() << ", "  << WeightFct[1].GetValueString() << ", "  << WeightFct[2].GetValueString()  << std::endl;
276 	stream << "  Propagation Dir: " << PropagationDir[0].GetValueString() << ", "  << PropagationDir[1].GetValueString() << ", "  << PropagationDir[2].GetValueString()  << std::endl;
277 	stream << "  Delay\t\t: " << Delay.GetValueString() << std::endl;
278 }
279