Debian Codegraph @

packages/nox/test/epetra/MultiPhysics/NOX_Matlab_Interface.H

raw
1 // @HEADER
2 // *****************************************************************************
3 // NOX: An Object-Oriented Nonlinear Solver Package
4 //
5 // Copyright 2002 NTESS and the NOX contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef NOX_MATLAB_INTERFACE_H
11 #define NOX_MATLAB_INTERFACE_H
12
13 #include "NOX_Epetra.H"
14
15 #ifdef HAVE_MATLAB
16
17 #include "EpetraExt_MatlabEngine.h"
18
19 //-----------------------------------------------------------------------------
20
21 #include "EpetraExt_MatlabEngine.h"
22
23 // the following deal with matlab provided headers:
24 #include "engine.h"
25 #include "mex.h"
26 #undef printf // matlab has its own printf that we don't want to use
27
28 #define BUFSIZE 200
29 #define MATLABBUF 1024 * 16
30
31 //-----------------------------------------------------------------------------
32
33 // This is a new class that may evantually get moved into NOX. For now,
34 // this is simply used as a testbed for driving NOX using Matlab
35
36 class Matlab_Interface
37 {
38 public:
39
40 // A base class for Matlab interface commands to NOX
41 class CommandBase
42 {
43
44 public:
45
46 enum CommandCategory {
47 GROUP_GET ,
48 GROUP_SET ,
49 GROUP_COMPUTE ,
50 CALLBACK_FUN ,
51 QUERY ,
52 INOUT ,
53 GENERIC
54 };
55
56 static map< NOX::Abstract::Group::ReturnType, std::string > returnMsg;
57
58 static map< CommandCategory, std::string > categoryDescription;
59
60 CommandBase( EpetraExt::EpetraExt_MatlabEngine & engine_,
61 NOX::Solver::Manager & solver_ );
62
63 virtual ~CommandBase() { };
64
65 // methods
66
67 virtual bool doCommand( std::string ) = 0 ;
68
69 virtual const std::string & getCommand()
70 { return command; }
71
72 virtual const std::string & getDescription()
73 { return description; }
74
75 virtual void describe()
76 {
77 cout << "\t\t#" << command << "\t" << description << std::endl;
78 return;
79 }
80
81 virtual bool isCategory( CommandCategory category )
82 {
83 return ( myCategory == category );
84 }
85
86 // Get the NOX solver
87 const NOX::Solver::Manager * const getSolver() const
88 { return &solver; }
89
90 virtual void registerDriver( Matlab_Interface * driver_ )
91 { driver = driver_; }
92
93 protected:
94
95 // methods
96
97 virtual void initialize();
98
99 // data
100
101 std::string command ;
102 std::string description ;
103
104 std::map< std::string, std::string > userMaps;
105
106 EpetraExt::EpetraExt_MatlabEngine & engine;
107
108 NOX::Solver::Manager & solver;
109
110 Matlab_Interface * driver;
111
112 CommandCategory myCategory;
113
114 // Convenient pointers to underlying data in solver
115
116 NOX::Epetra::Group * groupPtr;
117
118 Epetra_Vector * solnPtr;
119 };
120
121 // Derived classes that represent individual commands
122
123 class CMD_map : public CommandBase
124 {
125
126 public:
127
128 CMD_map( EpetraExt::EpetraExt_MatlabEngine & engine_,
129 NOX::Solver::Manager & solver_ ) :
130 CommandBase( engine_, solver_ )
131 {
132 command = "map" ;
133 description = " - Map a command" ;
134 }
135
136 virtual bool doCommand( std::string );
137
138 virtual void writeMaps( std::ofstream & );
139
140 std::map< std::string, std::string > & getUserMaps()
141 { return userMaps; }
142
143 };
144
145 //-----------------------------------------------------------------------------
146 class CMD_showStack : public CommandBase
147 {
148
149 public:
150
151 CMD_showStack( EpetraExt::EpetraExt_MatlabEngine & engine_,
152 NOX::Solver::Manager & solver_ ) :
153 CommandBase( engine_, solver_ )
154 {
155 myCategory = QUERY;
156 command = "showStack" ;
157 description = " - Show the command stack" ;
158 }
159
160 virtual bool doCommand( std::string );
161 };
162
163 //-----------------------------------------------------------------------------
164 class CMD_clearStack : public CommandBase
165 {
166
167 public:
168
169 CMD_clearStack( EpetraExt::EpetraExt_MatlabEngine & engine_,
170 NOX::Solver::Manager & solver_ ) :
171 CommandBase( engine_, solver_ )
172 {
173 myCategory = QUERY;
174 command = "clearStack" ;
175 description = " - Clear the command stack" ;
176 }
177
178 virtual bool doCommand( std::string );
179 };
180
181 //-----------------------------------------------------------------------------
182 class CMD_isF : public CommandBase
183 {
184
185 public:
186
187 CMD_isF( EpetraExt::EpetraExt_MatlabEngine & engine_,
188 NOX::Solver::Manager & solver_ ) :
189 CommandBase( engine_, solver_ )
190 {
191 myCategory = QUERY;
192 command = "isF" ;
193 description = " - Query if Residual is current wrt X" ;
194 }
195
196 virtual bool doCommand( std::string );
197 };
198
199 //-----------------------------------------------------------------------------
200 class CMD_isJacobian : public CommandBase
201 {
202
203 public:
204
205 CMD_isJacobian( EpetraExt::EpetraExt_MatlabEngine & engine_,
206 NOX::Solver::Manager & solver_ ) :
207 CommandBase( engine_, solver_ )
208 {
209 myCategory = QUERY;
210 command = "isJacobian" ;
211 description = " - Query if Jacobian is current wrt X" ;
212 }
213
214 virtual bool doCommand( std::string );
215 };
216
217 //-----------------------------------------------------------------------------
218 class CMD_isGradient : public CommandBase
219 {
220
221 public:
222
223 CMD_isGradient( EpetraExt::EpetraExt_MatlabEngine & engine_,
224 NOX::Solver::Manager & solver_ ) :
225 CommandBase( engine_, solver_ )
226 {
227 myCategory = QUERY;
228 command = "isGradient" ;
229 description = " - Query if Gradient is current wrt X" ;
230 }
231
232 virtual bool doCommand( std::string );
233 };
234
235 //-----------------------------------------------------------------------------
236 class CMD_isNewton : public CommandBase
237 {
238
239 public:
240
241 CMD_isNewton( EpetraExt::EpetraExt_MatlabEngine & engine_,
242 NOX::Solver::Manager & solver_ ) :
243 CommandBase( engine_, solver_ )
244 {
245 myCategory = QUERY;
246 command = "isNewton" ;
247 description = " - Query if Newton vector is current wrt X" ;
248 }
249
250 virtual bool doCommand( std::string );
251 };
252
253 //-----------------------------------------------------------------------------
254 class CMD_isNormNewtonSolveResidual : public CommandBase
255 {
256
257 public:
258
259 CMD_isNormNewtonSolveResidual( EpetraExt::EpetraExt_MatlabEngine & engine_,
260 NOX::Solver::Manager & solver_ ) :
261 CommandBase( engine_, solver_ )
262 {
263 myCategory = QUERY;
264 command = "isNormNewtonSolveResidual" ;
265 description = " - Query if norm of the Newton solve is current wrt X" ;
266 }
267
268 virtual bool doCommand( std::string );
269 };
270
271 //-----------------------------------------------------------------------------
272 class CMD_isPreconditioner : public CommandBase
273 {
274
275 public:
276
277 CMD_isPreconditioner( EpetraExt::EpetraExt_MatlabEngine & engine_,
278 NOX::Solver::Manager & solver_ ) :
279 CommandBase( engine_, solver_ )
280 {
281 myCategory = QUERY;
282 command = "isPreconditioner" ;
283 description = " - Query if Preconditioner is current wrt X" ;
284 }
285
286 virtual bool doCommand( std::string );
287 };
288
289 //-----------------------------------------------------------------------------
290 class CMD_isConditionNumber : public CommandBase
291 {
292
293 public:
294
295 CMD_isConditionNumber( EpetraExt::EpetraExt_MatlabEngine & engine_,
296 NOX::Solver::Manager & solver_ ) :
297 CommandBase( engine_, solver_ )
298 {
299 myCategory = QUERY;
300 command = "isConditionNumber" ;
301 description = " - Query if Condition Number is current wrt X" ;
302 }
303
304 virtual bool doCommand( std::string );
305 };
306
307 //-----------------------------------------------------------------------------
308 class CMD_showValid : public CommandBase
309 {
310
311 public:
312
313 CMD_showValid( EpetraExt::EpetraExt_MatlabEngine & engine_,
314 NOX::Solver::Manager & solver_ ) :
315 CommandBase( engine_, solver_ )
316 {
317 myCategory = QUERY;
318 command = "showValid" ;
319 description = " - Show status of all \"is***\" query methods" ;
320 }
321
322 virtual bool doCommand( std::string );
323
324 // Static member to allow use by others
325 static bool showValid( const NOX::Epetra::Group * );
326 };
327
328 //-----------------------------------------------------------------------------
329 class CMD_getJacobianConditionNumber : public CommandBase
330 {
331
332 public:
333
334 CMD_getJacobianConditionNumber( EpetraExt::EpetraExt_MatlabEngine & engine_,
335 NOX::Solver::Manager & solver_ ) :
336 CommandBase( engine_, solver_ )
337 {
338 myCategory = GROUP_GET;
339 command = "getJacobianConditionNumber" ;
340 description = " - Report the condition number of the Jacobian matrix" ;
341 }
342
343 virtual bool doCommand( std::string );
344 };
345
346 //-----------------------------------------------------------------------------
347 class CMD_getNormF : public CommandBase
348 {
349
350 public:
351
352 CMD_getNormF( EpetraExt::EpetraExt_MatlabEngine & engine_,
353 NOX::Solver::Manager & solver_ ) :
354 CommandBase( engine_, solver_ )
355 {
356 myCategory = GROUP_GET;
357 command = "getNormF" ;
358 description = " - Report the Residual (2-)norm" ;
359 }
360
361 virtual bool doCommand( std::string );
362 };
363
364 //-----------------------------------------------------------------------------
365 class CMD_setX : public CommandBase
366 {
367
368 public:
369
370 CMD_setX( EpetraExt::EpetraExt_MatlabEngine & engine_,
371 NOX::Solver::Manager & solver_ ) :
372 CommandBase( engine_, solver_ )
373 {
374 myCategory = GROUP_GET;
375 command = "setX" ;
376 description = " - Set the group solution, X" ;
377 }
378
379 virtual bool doCommand( std::string );
380 };
381
382 //-----------------------------------------------------------------------------
383 class CMD_computeF : public CommandBase
384 {
385
386 public:
387
388 CMD_computeF( EpetraExt::EpetraExt_MatlabEngine & engine_,
389 NOX::Solver::Manager & solver_ ) :
390 CommandBase( engine_, solver_ )
391 {
392 myCategory = GROUP_COMPUTE;
393 command = "computeF" ;
394 description = " - Compute the Residual vector" ;
395 }
396
397 virtual bool doCommand( std::string );
398 };
399
400 //-----------------------------------------------------------------------------
401 class CMD_computeJacobian : public CommandBase
402 {
403
404 public:
405
406 CMD_computeJacobian( EpetraExt::EpetraExt_MatlabEngine & engine_,
407 NOX::Solver::Manager & solver_ ) :
408 CommandBase( engine_, solver_ )
409 {
410 myCategory = GROUP_COMPUTE;
411 command = "computeJacobian" ;
412 description = " - Compute the Jacobian" ;
413 }
414
415 virtual bool doCommand( std::string );
416 };
417
418 //-----------------------------------------------------------------------------
419 class CMD_computeGradient : public CommandBase
420 {
421
422 public:
423
424 CMD_computeGradient( EpetraExt::EpetraExt_MatlabEngine & engine_,
425 NOX::Solver::Manager & solver_ ) :
426 CommandBase( engine_, solver_ )
427 {
428 myCategory = GROUP_COMPUTE;
429 command = "computeGradient" ;
430 description = " - Compute the Gradient vector" ;
431 }
432
433 virtual bool doCommand( std::string );
434 };
435
436 //-----------------------------------------------------------------------------
437 class CMD_computeNewton : public CommandBase
438 {
439
440 public:
441
442 CMD_computeNewton( EpetraExt::EpetraExt_MatlabEngine & engine_,
443 NOX::Solver::Manager & solver_ ) :
444 CommandBase( engine_, solver_ )
445 {
446 myCategory = GROUP_COMPUTE;
447 command = "computeNewton" ;
448 description = " - Compute the Newton vector" ;
449 }
450
451 virtual bool doCommand( std::string );
452 };
453
454 //-----------------------------------------------------------------------------
455 class CMD_getX : public CommandBase
456 {
457
458 public:
459
460 CMD_getX( EpetraExt::EpetraExt_MatlabEngine & engine_,
461 NOX::Solver::Manager & solver_ ) :
462 CommandBase( engine_, solver_ )
463 {
464 myCategory = GROUP_GET;
465 command = "getX" ;
466 description = " - Get the solution vector, X" ;
467 }
468
469 virtual bool doCommand( std::string );
470 };
471
472 //-----------------------------------------------------------------------------
473 class CMD_getF : public CommandBase
474 {
475
476 public:
477
478 CMD_getF( EpetraExt::EpetraExt_MatlabEngine & engine_,
479 NOX::Solver::Manager & solver_ ) :
480 CommandBase( engine_, solver_ )
481 {
482 myCategory = GROUP_GET;
483 command = "getF" ;
484 description = " - Get the Residual vector, F" ;
485 }
486
487 virtual bool doCommand( std::string );
488 };
489
490 //-----------------------------------------------------------------------------
491 class CMD_getGradient : public CommandBase
492 {
493
494 public:
495
496 CMD_getGradient( EpetraExt::EpetraExt_MatlabEngine & engine_,
497 NOX::Solver::Manager & solver_ ) :
498 CommandBase( engine_, solver_ )
499 {
500 myCategory = GROUP_GET;
501 command = "getGradient" ;
502 description = " - Get the Gradient vector" ;
503 }
504
505 virtual bool doCommand( std::string );
506 };
507
508 //-----------------------------------------------------------------------------
509 class CMD_getNewton : public CommandBase
510 {
511
512 public:
513
514 CMD_getNewton( EpetraExt::EpetraExt_MatlabEngine & engine_,
515 NOX::Solver::Manager & solver_ ) :
516 CommandBase( engine_, solver_ )
517 {
518 myCategory = GROUP_GET;
519 command = "getNewton" ;
520 description = " - Get the Newton vector" ;
521 }
522
523 virtual bool doCommand( std::string );
524 };
525
526 //-----------------------------------------------------------------------------
527 class CMD_getJacobian : public CommandBase
528 {
529
530 public:
531
532 CMD_getJacobian( EpetraExt::EpetraExt_MatlabEngine & engine_,
533 NOX::Solver::Manager & solver_ ) :
534 CommandBase( engine_, solver_ )
535 {
536 myCategory = GROUP_GET;
537 command = "getJacobian" ;
538 description = " - Get the Jacobian matrix" ;
539 }
540
541 virtual bool doCommand( std::string );
542 };
543
544 //-----------------------------------------------------------------------------
545 class CMD_newMacro : public CommandBase
546 {
547
548 public:
549
550 static std::istream * is;
551
552 CMD_newMacro( EpetraExt::EpetraExt_MatlabEngine & engine_,
553 NOX::Solver::Manager & solver_ ) :
554 CommandBase( engine_, solver_ )
555 {
556 command = "newMacro" ;
557 description = " - Create a named macro" ;
558 }
559
560 virtual bool doCommand( std::string );
561
562 };
563
564 //-----------------------------------------------------------------------------
565 class CMD_macro : public CommandBase
566 {
567
568 public:
569
570 CMD_macro( EpetraExt::EpetraExt_MatlabEngine & engine_,
571 NOX::Solver::Manager & solver_ ,
572 std::string name_ ) :
573 CommandBase( engine_, solver_ ),
574 macroName( name_ )
575 {
576 command = "macro" ;
577 description = " - invoke a user macro" ;
578 }
579
580 virtual bool doCommand( std::string );
581
582 virtual bool addLineCommand( std::string & com )
583 { macroCommands.push_back(com); return true; }
584
585 virtual void describe();
586
587 virtual void writeMacro( std::ofstream & );
588
589 virtual std::string getName()
590 { return macroName; }
591
592 protected:
593
594 std::string macroName;
595
596 std::list< std::string > macroCommands;
597
598 };
599
600 //-----------------------------------------------------------------------------
601 class CMD_read : public CommandBase
602 {
603
604 public:
605
606 CMD_read( EpetraExt::EpetraExt_MatlabEngine & engine_,
607 NOX::Solver::Manager & solver_ ) :
608 CommandBase( engine_, solver_ )
609 {
610 myCategory = INOUT;
611 command = "read" ;
612 description = " - read a script file" ;
613 }
614
615 virtual bool doCommand( std::string );
616
617 };
618
619 //-----------------------------------------------------------------------------
620 class CMD_write : public CommandBase
621 {
622
623 public:
624
625 CMD_write( EpetraExt::EpetraExt_MatlabEngine & engine_,
626 NOX::Solver::Manager & solver_ ) :
627 CommandBase( engine_, solver_ )
628 {
629 myCategory = INOUT;
630 command = "write" ;
631 description = " - write a script file" ;
632 }
633
634 virtual bool doCommand( std::string );
635
636 };
637
638 //-----------------------------------------------------------------------------
639
640 Matlab_Interface(NOX::Solver::Manager & solver);
641
642 virtual ~Matlab_Interface();
643
644 // Begin an interactive session
645 virtual void interact();
646
647 virtual bool doCommand( std::string );
648
649 virtual bool doNOXCommand( std::string & );
650
651 virtual void addCommand( CommandBase * com )
652 { commands.push_back( com ); return; }
653
654 virtual void removeCommand( CommandBase * com )
655 { commands.remove( com ); return; }
656
657 CMD_map * getMapHandler()
658 { return mapHandler; }
659
660 std::list< std::string > & getCommandStack()
661 { return commandStack; }
662
663 std::map< std::string, CMD_macro * > & getUserMacros()
664 { return userMacros; }
665
666
667 protected:
668
669 // Methods
670
671 // Data
672
673 // A needed communicator - can be replaced later by initialization argument
674 Epetra_SerialComm comm;
675
676 EpetraExt::EpetraExt_MatlabEngine * enginePtr;
677 EpetraExt::EpetraExt_MatlabEngine & engine;
678
679 NOX::Solver::Manager & solver;
680 NOX::Epetra::Group * groupPtr;
681 Epetra_Vector * solnPtr;
682
683 // A user command mapping handler
684 CMD_map * mapHandler;
685
686 std::map< std::string, CMD_macro * > userMacros;
687
688 std::list< CommandBase* > commands;
689
690 std::list< std::string > commandStack;
691 };
692
693 #endif
694 #endif