1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/modules/module-template/module-template.cc,v 1.36 2017/01/12 14:58:00 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati  <masarati@aero.polimi.it>
9  *
10  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
11  * via La Masa, 34 - 20156 Milano, Italy
12  * http://www.aero.polimi.it
13  *
14  * Changing this copyright notice is forbidden.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation (version 2 of the License).
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  */
30 
31 #include "mbconfig.h"           /* This goes first in every *.c,*.cc file */
32 
33 #include <iostream>
34 #include <cfloat>
35 
36 #include <dataman.h>
37 #include "loadable.h"
38 
39 /*
40  * user-defined struct
41  */
42 struct module_template {
43 	int i;
44 };
45 
46 /* default funcs */
47 static void *
read(LoadableElem * pEl,DataManager * pDM,MBDynParser & HP)48 read( LoadableElem* pEl, DataManager* pDM, MBDynParser& HP)
49 {
50 	DEBUGCOUTFNAME("read");
51 
52 	/*
53 	 * allocation of user-defined struct
54 	 */
55 	module_template* p = NULL;
56 	SAFENEW(p, module_template);
57 
58 	/*
59 	 * read data
60 	 */
61 	if (HP.IsKeyWord("help")) {
62 		silent_cout("Module template" << std::endl);
63 	}
64 
65 	return (void *)p;
66 }
67 
68 static unsigned int
i_get_num_dof(const LoadableElem * pEl)69 i_get_num_dof(const LoadableElem* pEl)
70 {
71 	DEBUGCOUTFNAME("i_get_num_dof");
72 	return 0;
73 }
74 
75 static DofOrder::Order
set_dof(const LoadableElem *,unsigned int i)76 set_dof(const LoadableElem*, unsigned int i)
77 {
78 	DEBUGCOUTFNAME("set_dof");
79 	return DofOrder::UNKNOWN;
80 }
81 
82 static void
output(const LoadableElem * pEl,OutputHandler & OH)83 output(const LoadableElem* pEl, OutputHandler& OH)
84 {
85 	DEBUGCOUTFNAME("output");
86 }
87 
88 static std::ostream&
restart(const LoadableElem * pEl,std::ostream & out)89 restart(const LoadableElem* pEl, std::ostream& out)
90 {
91 	DEBUGCOUTFNAME("restart");
92 	return out << "not implemented yet;" << std::endl;
93 }
94 
95 static void
work_space_dim(const LoadableElem * pEl,integer * piNumRows,integer * piNumCols)96 work_space_dim(const LoadableElem* pEl, integer* piNumRows, integer* piNumCols)
97 {
98 	DEBUGCOUTFNAME("work_space_dim");
99 	*piNumRows = 0;
100 	*piNumCols = 0;
101 }
102 
103 static VariableSubMatrixHandler&
ass_jac(LoadableElem * pEl,VariableSubMatrixHandler & WorkMat,doublereal dCoef,const VectorHandler & XCurr,const VectorHandler & XPrimeCurr)104 ass_jac(
105 		LoadableElem* pEl,
106 		VariableSubMatrixHandler& WorkMat,
107 		doublereal dCoef,
108 		const VectorHandler& XCurr,
109 		const VectorHandler& XPrimeCurr
110 )
111 {
112 	DEBUGCOUTFNAME("ass_jac");
113 	integer iNumRows = 0;
114 	integer iNumCols = 0;
115 	pEl->WorkSpaceDim(&iNumRows, &iNumCols);
116 
117 	FullSubMatrixHandler& WM = WorkMat.SetFull();
118 	WM.ResizeReset(iNumRows, iNumCols);
119 
120 #if 0
121 	module_template* p = (module_template *)pEl->pGetData();
122 #endif /* 0 */
123 
124 	/*
125 	 * set sub-matrix indices and coefs
126 	 */
127 
128 	return WorkMat;
129 }
130 
131 static void
ass_mats(LoadableElem * pEl,VariableSubMatrixHandler & WorkMatA,VariableSubMatrixHandler & WorkMatB,const VectorHandler & XCurr,const VectorHandler & XPrimeCurr)132 ass_mats(
133 		LoadableElem* pEl,
134 		VariableSubMatrixHandler& WorkMatA,
135 		VariableSubMatrixHandler& WorkMatB,
136 		const VectorHandler& XCurr,
137 		const VectorHandler& XPrimeCurr
138 )
139 {
140 	DEBUGCOUTFNAME("ass_mats");
141 	integer iNumRows = 0;
142 	integer iNumCols = 0;
143 	pEl->WorkSpaceDim(&iNumRows, &iNumCols);
144 
145 	FullSubMatrixHandler& WMA = WorkMatA.SetFull();
146 	WMA.ResizeReset(iNumRows, iNumCols);
147 
148 	FullSubMatrixHandler& WMB = WorkMatB.SetFull();
149 	WMB.ResizeReset(iNumRows, iNumCols);
150 
151 #if 0
152 	module_template* p = (module_template *)pEl->pGetData();
153 #endif /* 0 */
154 
155 	/*
156 	 * set sub-matrix indices and coefs
157 	 */
158 }
159 
160 static SubVectorHandler&
ass_res(LoadableElem * pEl,SubVectorHandler & WorkVec,doublereal dCoef,const VectorHandler & XCurr,const VectorHandler & XPrimeCurr)161 ass_res(
162 		LoadableElem* pEl,
163 		SubVectorHandler& WorkVec,
164 		doublereal dCoef,
165 		const VectorHandler& XCurr,
166 		const VectorHandler& XPrimeCurr
167 )
168 {
169 	DEBUGCOUTFNAME("ass_res");
170 	integer iNumRows = 0;
171 	integer iNumCols = 0;
172 	pEl->WorkSpaceDim(&iNumRows, &iNumCols);
173 
174 	WorkVec.Resize(iNumRows);
175 
176 #if 0
177 	module_template* p = (module_template *)pEl->pGetData();
178 #endif /* 0 */
179 
180 	/*
181 	 * set sub-vector indices and coefs
182 	 */
183 
184 	return WorkVec;
185 }
186 
187 static void
before_predict(const LoadableElem * pEl,VectorHandler & X,VectorHandler & XP,VectorHandler & XPrev,VectorHandler & XPPrev)188 before_predict(
189 		const LoadableElem* pEl,
190 		VectorHandler& X,
191 		VectorHandler& XP,
192 		VectorHandler& XPrev,
193 		VectorHandler& XPPrev
194 )
195 {
196 	DEBUGCOUTFNAME("before_predict");
197 }
198 
199 static void
after_predict(const LoadableElem * pEl,VectorHandler & X,VectorHandler & XP)200 after_predict(
201 		const LoadableElem* pEl,
202 		VectorHandler& X,
203 		VectorHandler& XP
204 )
205 {
206 	DEBUGCOUTFNAME("after_predict");
207 }
208 
209 static void
update(LoadableElem * pEl,const VectorHandler & X,const VectorHandler & XP)210 update(
211 		LoadableElem* pEl,
212 		const VectorHandler& X,
213 		const VectorHandler& XP
214 )
215 {
216 	DEBUGCOUTFNAME("update");
217 }
218 
219 static void
after_convergence(const LoadableElem *,const VectorHandler &,const VectorHandler &)220 after_convergence(const LoadableElem* /* pEl */ ,
221 		const VectorHandler& /* X */ ,
222 		const VectorHandler& /* XP */ )
223 {
224 	DEBUGCOUTFNAME("after_convergence");
225 }
226 
227 static unsigned int
i_get_initial_num_dof(const LoadableElem * pEl)228 i_get_initial_num_dof(const LoadableElem* pEl)
229 {
230 	DEBUGCOUTFNAME("i_get_initial_num_dof");
231 	return 0;
232 }
233 
234 static void
initial_work_space_dim(const LoadableElem * pEl,integer * piNumRows,integer * piNumCols)235 initial_work_space_dim(
236 		const LoadableElem* pEl,
237 		integer* piNumRows,
238 		integer* piNumCols
239 )
240 {
241 	DEBUGCOUTFNAME("initial_work_space_dim");
242 	*piNumRows = 0;
243 	*piNumCols = 0;
244 }
245 
246 static VariableSubMatrixHandler&
initial_ass_jac(LoadableElem * pEl,VariableSubMatrixHandler & WorkMat,const VectorHandler & XCurr)247 initial_ass_jac(
248 		LoadableElem* pEl,
249 		VariableSubMatrixHandler& WorkMat,
250 		const VectorHandler& XCurr
251 )
252 {
253 	DEBUGCOUTFNAME("initial_ass_jac");
254 	integer iNumRows = 0;
255 	integer iNumCols = 0;
256 	pEl->InitialWorkSpaceDim(&iNumRows, &iNumCols);
257 
258 	FullSubMatrixHandler& WM = WorkMat.SetFull();
259 		WM.ResizeReset(iNumRows, iNumCols);
260 
261 #if 0
262 	module_template* p = (module_template *)pEl->pGetData();
263 #endif /* 0 */
264 
265 	/*
266 	 * set sub-matrix indices and coefs
267 	 */
268 
269 	return WorkMat;
270 }
271 
272 static SubVectorHandler&
initial_ass_res(LoadableElem * pEl,SubVectorHandler & WorkVec,const VectorHandler & XCurr)273 initial_ass_res(
274 		LoadableElem* pEl,
275 		SubVectorHandler& WorkVec,
276 		const VectorHandler& XCurr
277 )
278 {
279 	DEBUGCOUTFNAME("initial_ass_res");
280 	integer iNumRows = 0;
281 	integer iNumCols = 0;
282 	pEl->WorkSpaceDim(&iNumRows, &iNumCols);
283 
284 	WorkVec.Resize(iNumRows);
285 
286 #if 0
287 	module_template* p = (module_template *)pEl->pGetData();
288 #endif /* 0 */
289 
290 	/*
291 	 * set sub-vector indices and coefs
292 	 */
293 
294 	return WorkVec;
295 }
296 
297 static void
set_value(const LoadableElem * pEl,DataManager * pDM,VectorHandler & X,VectorHandler & XP,SimulationEntity::Hints * ph)298 set_value(const LoadableElem* pEl, DataManager *pDM,
299 		VectorHandler& X, VectorHandler& XP,
300 		SimulationEntity::Hints *ph)
301 {
302 	DEBUGCOUTFNAME("set_value");
303 }
304 
305 static void
set_initial_value(const LoadableElem * pEl,VectorHandler & X)306 set_initial_value(const LoadableElem* pEl, VectorHandler& X)
307 {
308 	DEBUGCOUTFNAME("set_initial_value");
309 }
310 
311 static unsigned int
i_get_num_priv_data(const LoadableElem * pEl)312 i_get_num_priv_data(const LoadableElem* pEl)
313 {
314 	DEBUGCOUTFNAME("i_get_num_priv_data");
315 	return 0;
316 }
317 
318 static unsigned int
i_get_priv_data_idx(const LoadableElem * pEl,const char * s)319 i_get_priv_data_idx(const LoadableElem* pEl, const char *s)
320 {
321 	DEBUGCOUTFNAME("i_get_priv_data_idx");
322 	silent_cerr("Module-template Elem(" << pEl->GetLabel() << "): "
323 		"priv data \"" << s << "\" is unknown" << std::endl);
324 	throw ErrGeneric(MBDYN_EXCEPT_ARGS);
325 
326 	return 0;
327 }
328 
329 static doublereal
d_get_priv_data(const LoadableElem * pEl,unsigned int i)330 d_get_priv_data(const LoadableElem* pEl, unsigned int i)
331 {
332 	DEBUGCOUTFNAME("d_get_priv_data");
333 	ASSERT(pEl->iGetNumPrivData() > 0);
334 	if (i > pEl->iGetNumPrivData()) {
335 		silent_cerr("Module-template Elem(" << pEl->GetLabel() << "): "
336 			"illegal private data index " << i << std::endl);
337 		throw ErrGeneric(MBDYN_EXCEPT_ARGS);
338 	}
339 
340 	/*
341 	 * return i-th priv data
342 	 */
343 	return 0.;
344 }
345 
346 static void
destroy(LoadableElem * pEl)347 destroy(LoadableElem* pEl)
348 {
349 	DEBUGCOUTFNAME("destroy");
350 
351 	module_template* p = (module_template *)pEl->pGetData();
352 
353 	/*
354 	 * delete private data
355 	 */
356 
357 	SAFEDELETE(p);
358 }
359 
360 static int
i_get_num_connected_nodes(const LoadableElem * pEl)361 i_get_num_connected_nodes(const LoadableElem* pEl)
362 {
363 	DEBUGCOUTFNAME("i_get_num_connected_nodes");
364 
365 #if 0
366 	module_template* p = (module_template *)pEl->pGetData();
367 #endif /* 0 */
368 
369 	return 0;
370 }
371 
372 static void
get_connected_nodes(const LoadableElem * pEl,std::vector<const Node * > & connectedNodes)373 get_connected_nodes(const LoadableElem* pEl,
374 		std::vector<const Node *>& connectedNodes)
375 {
376 	DEBUGCOUTFNAME("get_connected_nodes");
377 
378 #if 0
379 	module_template* p = (module_template *)pEl->pGetData();
380 #endif /* 0 */
381 
382 	/*
383 	 * set args according to element connections
384 	 */
385 	connectedNodes.resize(i_get_num_connected_nodes(pEl));
386 }
387 
388 static struct
389 LoadableCalls lc = {
390 	LOADABLE_VERSION_SET(1, 5, 0),
391 
392 	"template",
393 	"1.1",
394 	"Dipartimento di Ingegneria Aerospaziale, Politecnico di Milano",
395 	"template module; use it as guideline for user-defined elements",
396 
397 	read,
398 	i_get_num_dof,
399 	set_dof,
400 	output,
401 	restart,
402 	work_space_dim,
403 	ass_jac,
404 	ass_mats,
405 	ass_res,
406 	before_predict,
407 	after_predict,
408 	update,
409 	after_convergence,
410 	i_get_initial_num_dof,
411 	initial_work_space_dim,
412 	initial_ass_jac,
413 	initial_ass_res,
414 	set_value,
415 	set_initial_value,
416 	i_get_num_priv_data,
417 	i_get_priv_data_idx,
418 	d_get_priv_data,
419 	i_get_num_connected_nodes,
420 	get_connected_nodes,
421 	destroy
422 };
423 
424 extern "C" {
425 void *calls = &lc;
426 }
427 
428