Overview
Comment:Fix for [ 547513 ]
Re-written to eliminate need for installation.
Single C++ file (cxxsim.cc) handles the entire transformation which is
automatically compiled on invocation by "mtt sys cxxsim view".
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | origin/master | trunk
Files: files | file ages | folders
SHA3-256: 7a95f8d106863fd8fe3873277f301d28de69c4cb810e78a0f32a281c22261b14
User & Date: geraint@users.sourceforge.net on 2002-12-12 16:07:13
Other Links: branch diff | manifest | tags
Context
2002-12-12
17:18:43
Fixed [ 647664 ] Namespace clash: LANG
Prepended MTT_ to SYS, OPTS and LANG environment variables.
check-in: 43c2af3a70 user: geraint@users.sourceforge.net tags: origin/master, trunk
16:07:13
Fix for [ 547513 ]
Re-written to eliminate need for installation.
Single C++ file (cxxsim.cc) handles the entire transformation which is
automatically compiled on invocation by "mtt sys cxxsim view".
check-in: 7a95f8d106 user: geraint@users.sourceforge.net tags: origin/master, trunk
16:00:11
Removed cruft. check-in: 441a80070a user: geraint@users.sourceforge.net tags: origin/master, trunk
Changes

Added mttroot/mtt/lib/rep/cxxsim.cc version [b135fb8c74].





























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*  cxxsim:  creates a C++ simulation from MTT elementary system equations
 *  Copyright (C) 2000,2002  Geraint Paul Bevan
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <fstream>
#include <list>
#include <map>
#include <string>

namespace cxxsim {

  class cr {
    
  public:

    cr		(std::string system_name);
    
    void read	(std::ifstream &file);
    void write	(std::ofstream &file);
   
    friend std::ifstream &operator>> (std::ifstream &file, cr &c);
    friend std::ofstream &operator<< (std::ofstream &file, cr &c);
    
  private:

    std::string			name;

    std::list<std::string>	L;
  };

  class equations {

  public:

    equations	(std::string system_name);    

    void read	(std::ifstream &file);
    void write	(std::ofstream &file);
   
    friend std::ifstream &operator>> (std::ifstream &file, equations &e);
    friend std::ofstream &operator<< (std::ofstream &file, equations &e);

  private:
    
    void descend	(std::string s);
    void parse		(void);

    std::string				name; // system name

    // list of statements collected by reader
    std::list<std::string>		statements;

    std::list<std::string>		leqn; // final expressions
    std::list<std::string>		lvar; // intermediate expressions
    std::list<std::string>		loop; // algebraic loops

    // table of final and intermediate expressions
    std::map<std::string, std::string>	meqn; // lhs, rhs mapping
    std::map<std::string, std::string>	mvar;

    // tag is symbol denoting an intermediate variable
    std::string				tag; // denotes intermediates

    // token delimiters (not ([{.`'" or alphanumeric)
    std::string				delim;
  };

  class parameter {

  public:

    parameter	(std::string system_name);

    void read	(std::ifstream &file);
    void write	(std::ofstream &file);

    friend std::ifstream &operator>> (std::ifstream &file, parameter &p);
    friend std::ofstream &operator<< (std::ofstream &file, parameter &p);

  private:

    std::string		name;

    struct record {
      std::string	variable;
      std::string	component;
    };
    
    typedef struct record record_t;
    std::list<record_t>			L;
  };

  class structure {
    
  public:

    structure		(std::string system_name);

    void read		(std::ifstream &file);
    void write		(std::ofstream &file);

    friend std::ifstream &operator>> (std::ifstream &file, structure &s);
    friend std::ofstream &operator<< (std::ofstream &file, structure &s);

  private:

    void write_declare	(std::ofstream &file);
    void write_input	(std::ofstream &file);
    void write_logic	(std::ofstream &file);
    void write_state	(std::ofstream &file);

    std::string		name;

    struct record {
      std::string	vec;	// vector: u, x, y, yz
      unsigned int	num;	// position of element in vector
      std::string	cmp;	// component
      std::string	fnm;	// full name
      unsigned int	rpt;	// number of repetitions
    };

    typedef struct record record_t;
    std::list<record_t>			Lu;
    std::list<record_t>			Lx;
    std::list<record_t>			Ly;
    std::list<record_t>			Lyz;
    std::list<record_t>::iterator	i;    
  };

  cr::cr (std::string system_name){
    name = system_name;
  }

  void
  cr::read (std::ifstream &file){
    std::string r;
    while (file >> r){
      L.push_back(r);
    }
  }

  void
  cr::write (std::ofstream &file){
    file << "enum causality { effort, flow, state };" << std::endl
	 << "typedef enum causality causality_t;" << std::endl;
    std::list<std::string>::iterator i;
    for (i = L.begin(); i != L.end(); i++){
      if ((*i != "SS") && (*i != "ISW")){
	file << "#include <" << *i << ".hh>" << std::endl;
      }
    }
    file << std::endl;
  }

  std::ifstream &operator>> (std::ifstream &file, cr &c){
    c.read(file);
    return file;
  }
  
  std::ofstream &operator<< (std::ofstream &file, cr &c){
    c.write(file);
    return file;
  }

  equations::equations (std::string system_name){
    name = system_name;
    tag = system_name;
    delim = "~!@#$%^&*)-+=]}\\|;:,<>/?";
  };

  void
  equations::read (std::ifstream &file){
    
    char				c;
    std::string				s;
    std::string				t;

    unsigned int			i;

    //    file.unsetf(ios::skipws);
    file.setf(ios::skipws);

    // read lines from file (no max length, unlike std::getline)
    while (file >> c){
      s = "";
      while (c != ';' && file){
	if (c == '%'){
	  file.unsetf(ios::skipws);
	  while ((c != '\n') && file){
	    file >> c;			     // eat comment
	  }
	  file.setf(ios::skipws);
	} else {
	  if ((c != ' ') && (c != '\t')){    // strip whitespace
	    s += c;
	  }
	  file >> c;
	}
      }
      
      // fix vector references
      t = "MTTu\(";
      while ((i = s.find(t)) < s.length()){
	s.replace(i, 5, "mttu[");
	i += s.substr(i, s.length()).find(",1)");
	s.replace(i, 3, "]");
      }
      t = "MTTx\(";
      while ((i = s.find(t)) < s.length()){
	s.replace(i, 5, "mttx[");
	i += s.substr(i, s.length()).find(",1)");
	s.replace(i, 3, "]");
      }
      t = "MTTdX\(";
      while ((i = s.find(t)) < s.length()){
	s.replace(i, 6, "mttdx[");
	i += s.substr(i, s.length()).find(",1)");
	s.replace(i, 3, "]");
      }
      t = "MTTy\(";
      while ((i = s.find(t)) < s.length()){
	s.replace(i, 5, "mtty[");
	i += s.substr(i, s.length()).find(",1)");
	s.replace(i, 3, "]");
      }
      
      // strip newlines
      while ((i = s.find('\n')) < s.length()){
	s.replace(i, 1, "");
      }

      if ((s != "") && (s != "END")){
	// add statement to list
	statements.push_back(s);
      }
    }

    // finished reading, time to comprehend ...
    this->parse();
  }
  
  void
  equations::write (std::ofstream &file){

    std::list<std::string>::iterator	eqn;

    file << "void"					<< std::endl
	 << this->name << "_ode(const double t,"	<< std::endl
	 << "\tdouble *mttu,"				<< std::endl
	 << "\tdouble *mttx,"				<< std::endl
	 << "\tdouble *mttdx,"				<< std::endl
	 << "\tdouble *mtty)"				<< std::endl
	 << "{"						<< std::endl;
    for (eqn = leqn.begin(); eqn != leqn.end(); eqn++){
      file << *eqn << " = " << meqn[*eqn] << ";" << std::endl;
    }
    file << "} // " << this->name << "_ode"		<< std::endl;
  };

  void
  equations::descend (std::string s){

    char		c;
    unsigned int	i;
    bool		cont;

    std::list<std::string>::iterator	var;

    loop.push_back(s);

    // find algebraic loops
    for (var = loop.begin(); var != loop.end(); var++){
      if ((i = mvar[s].find(*var)) < mvar[s].length()){
	c = mvar[s].c_str()[i + var->length()];
	if (delim.find(c) < delim.length()){
	  std::cerr << std::endl
		    << "warning: algebraic loop" << std::endl
		    << '\t' << s << " = " << mvar[s].substr(0, 60);
	  if (mvar[s].length() > 60){
	    std::cerr << " ...";
	  }
	  std::cerr << std::endl << std::endl;
	  mvar[s] = s + "____loop";
	}
      }
    }

    // substitutes intermediate variables for their expansion
    do {
      cont = false;
      for (var = lvar.begin(); var != lvar.end(); var++){
	// check for token match
	while ((i = mvar[s].find(*var)) < mvar[s].length()){
	  // ensure exact match
	  c = mvar[s].c_str()[i + var->length()];
	  if (delim.find(c) < delim.length()){
	    // expand expression recursively
	    this->descend(*var);
	    // check match again, map may have changed
	    if ((i = mvar[s].find(*var)) < mvar[s].length()){
	      c = mvar[s].c_str()[i + var->length()];
	      if (delim.find(c) < delim.length()){
	    	// substitute expression
		mvar[s].replace(i, var->length(), mvar[*var]);
		// continue
		cont = true;
	      }
	    }	    
	  }
	}
      }
    } while (cont);
    loop.pop_back();
  }
  
  void
  equations::parse (void){
    
    char		c;
    unsigned int	i;

    std::string		lhs;
    std::string		rhs;
    
    std::list<std::string>::iterator	s;

    std::list<std::string>::iterator	eqn;
    std::list<std::string>::iterator	var;

    for (s = statements.begin(); s != statements.end(); s++){
      // parse statement
      if ((i = s->find(":=")) < s->length()){
	lhs = s->substr(0, i);
	rhs = s->substr(i+2, s->length());
	
	// separate intermediate and final variables
	if (s->substr(0, tag.length()) == tag){
	  // add to table of intermediate variables
	  lvar.push_back(lhs);
	  mvar[lhs] = "(" + rhs + ")";
	} else {
	  // add to table of final variables
	  leqn.push_back(lhs);
	  meqn[lhs] = rhs + ";";
	}
      } else {
	std::cerr << std::endl
		  << "warning: non-assignment -- ignoring"
		  << std::endl
		  << '\t' << *s << std::endl
		  << std::endl;
      }
    }
      
    // expand rhs of intermediate variables
    for (var = lvar.begin(); var != lvar.end(); var++){
      loop.clear();
      descend(*var);
    }
    
    // substitute expanded expressions into final equations
    for (eqn = leqn.begin(); eqn != leqn.end(); eqn++){
      for (var = lvar.begin(); var != lvar.end(); var++){
	while ((i = meqn[*eqn].find(*var)) < meqn[*eqn].length()){
	  c = meqn[*eqn].c_str()[i + var->length()];
	  if (delim.find(c) < delim.length()){
	    meqn[*eqn].replace(i, var->length(), mvar[*var]);
	  }
	}
      }
    }
  }

    std::ifstream &operator>> (std::ifstream &file, equations &e){
    e.read(file);
    return file;
  }
  
  std::ofstream &operator<< (std::ofstream &file, equations &e){
    e.write(file);
    return file;
  }

  parameter::parameter (std::string system_name){
    name = system_name;
  };

  void
  parameter::read (std::ifstream &file){
    record_t r;
    while (file >> r.variable >> r.component){
      if (r.variable.find("#") == 0){
	file.unsetf(ios::skipws);
	char c = '\0';
	while (c != '\n'){
	  file >> c;
	}
	file.setf(ios::skipws);
      } else {
	L.push_back(r);
      }
    }
  }

  void
  parameter::write (std::ofstream &file){
    std::list<record_t>::iterator i;
    std::string type;
    std::string value;
    for (i = L.begin(); i != L.end(); i++){
      if (i->variable.substr(0, 6) == "bool__"){
	type = "bool";
	value = "false";
      } else if (i->variable.substr(0, 6) == "char__"){
	type = "char";
	value = "\0";
      } else if (i->variable.substr(0, 8) == "double__"){
	type = "double";
	value = "1.0";
      } else if (i->variable.substr(0, 7) == "float__"){
	type = "float";
	value = "1.0";
      } else if (i->variable.substr(0, 5) == "int__"){
	type = "int";
	value = "1";
      } else if (i->variable.substr(0, 7) == "string__"){
	type = "std::string";
	value = "hello world!";
      } else {
	type = "double";
	value = "1.0";
      }
      file << "static " << type << "\t" << i->variable << " = " << value
	   << ";\t// " << i->component << std::endl;
    }
    file << std::endl;
  }

  std::ifstream &operator>> (std::ifstream &file, parameter &p){
    p.read(file);
    return file;
  }

  std::ofstream &operator<< (std::ofstream &file, parameter &p){
    p.write(file);
    return file;
  }

  structure::structure (std::string system_name){
    name = system_name;
  };
  
  void
  structure::read (std::ifstream &file){
    std::list<record_t> *p;
    record_t r;
    if (! file){
      std::cerr << "warning: no structure data found (empty file)" << std::endl;
    }
    while (file >> r.vec >> r.num >> r.cmp >> r.fnm >> r.rpt){
      if (r.vec == "input"){
	p = &(this->Lu);
      } else if (r.vec == "state"){
	p = &(this->Lx);
      } else if (r.vec == "output"){
	p = &(this->Ly);
      } else {
	p = &(this->Lyz);
      }
      p->push_back(r);
    }
  };

  void
  structure::write (std::ofstream &file){
    this->write_declare(file);
    file << std::endl;
    this->write_input(file);
    file << std::endl;
    this->write_logic(file);
    file << std::endl;
    this->write_state(file);
    file << std::endl;
  }

  void
  structure::write_declare (std::ofstream &file){
    file << "const int mttNu\t= " << this->Lu.size() + 1 << ";"	<< std::endl
	 << "const int mttNx\t= " << this->Lx.size() + 1 << ";"	<< std::endl
	 << "const int mttNy\t= " << this->Ly.size() + 1 << ";"	<< std::endl
	 << "static double mttu[mttNu];"			<< std::endl
	 << "static double mttx[mttNx];"			<< std::endl
	 << "static double mtty[mttNy];"			<< std::endl
	 << "static double mttdx[mttNx];"			<< std::endl
	 << std::endl;
  }

  void
  structure::write_input (std::ofstream &file){
    file << "void"					<< std::endl
	 << this->name << "_input(const double t,"	<< std::endl
	 << "\tdouble *mttu,"				<< std::endl
	 << "\tdouble *mttx,"				<< std::endl
	 << "\tdouble *mtty)"				<< std::endl
	 << "{"						<< std::endl;
    for (i = Lu.begin(); i != Lu.end(); i++){
      file << "\tmttu["	<< i->num << "]\t= 1.0;"
	   << "\t// "	<< i->cmp
	   << "\t("	<< i->fnm << ")"		<< std::endl;
    }
    file << "} // " << this->name << "_input"		<< std::endl;
  }

  void
  structure::write_logic (std::ofstream &file){
    file << "void"					<< std::endl
	 << this->name << "_logic(const double t,"	<< std::endl
	 << "\tdouble *mttu,"				<< std::endl
	 << "\tdouble *mttx,"				<< std::endl
	 << "\tdouble *mttdx,"				<< std::endl
	 << "\tdouble *mtty)"				<< std::endl
	 << "{"						<< std::endl;
    for (i = Lx.begin(); i != Lx.end(); i++){
      if (i->cmp == "MTT_SWITCH"){
	file << "int " << i->fnm << " = 1;"		<< std::endl
	     << "if ((" << i->fnm << " == 0)"
	     << " || ((" << i->fnm << " < 0)"
	     << " && (mttx[" << i->num << "] < 0.0))){"	<< std::endl
	     << "\tmttx[" << i->num << "]\t = 0.0;"	<< std::endl
	     << "\tmttdx[" << i->num << "]\t = 0.0;"	<< std::endl
	     << "}"					<< std::endl;
      }
    }
    file << "} // " << this->name << "_logic"		<< std::endl;
  }

  void
  structure::write_state (std::ofstream &file){
    file << "void"					<< std::endl
	 << this->name << "_state(double *mttx)"	<< std::endl
	 << "{"						<< std::endl;
    for (i = Lx.begin(); i != Lx.end(); i++){
      file << "\tmttx["	<< i->num << "]\t= 0.0;"
	   << "\t// "	<< i->cmp
	   << "\t("	<< i->fnm << ")"		<< std::endl;
    }    
    file << "} // " << this->name << "_state"		<< std::endl;      
  }

  std::ifstream &operator>> (std::ifstream &file, structure &s){
    s.read(file);
    return file;
  }

  std::ofstream &operator<< (std::ofstream &file, structure &s){
    s.write(file);
    return file;
  }

};

void
usage(const char *program){
  std::cerr << std::endl
	    << "usage: " << program << " system" << std::endl
	    << std::endl;
}

int main(int argc, char *argv[])
{
  // check usage
  if (argc != 2){
    usage(argv[0]);
    return 1;
  }
  std::string system_name = argv[1];
  
  // open files for reading and writing
  std::string cr_filename		= system_name + "_cr.txt";
  std::string parameter_filename	= system_name + "_sympar.txt";
  std::string structure_filename	= system_name + "_struc.txt";
  std::string equations_filename	= system_name + "_ese.r";
  std::string cxxsim_filename		= system_name + "_cxxsim.cc";

  std::ifstream cr_file	      (cr_filename.c_str());
  std::ifstream parameter_file(parameter_filename.c_str());
  std::ifstream structure_file(structure_filename.c_str());
  std::ifstream equations_file(equations_filename.c_str());
  std::ofstream cxxsim_file(cxxsim_filename.c_str());

  // announce transformation
  std::clog << "Creating " << cxxsim_filename << std::endl;
  
  // do transformations (abracadabra!)
  cxxsim::cr	    system_cr	    (system_name);
  cxxsim::parameter system_parameter(system_name);
  cxxsim::structure system_structure(system_name);
  cxxsim::equations system_equations(system_name);

  cr_file	 >> system_cr;
  parameter_file >> system_parameter;
  structure_file >> system_structure;
  equations_file >> system_equations;
  
  
  cxxsim_file << system_cr
	      << system_parameter
	      << system_structure 
	      << system_equations
	      << std::endl;

  cxxsim_file << std::endl
	      << "int"								<< std::endl
	      << "main(void){"							<< std::endl
	      << std::endl
	      << "  double mttdt = 0.1; // integration time step"		<< std::endl
	      << "  double mttt;"						<< std::endl
	      << "  int i;"							<< std::endl
	      << std::endl
	      << "  for (mttt = 0.0; mttt <= 10.0; mttt += mttdt){"		<< std::endl
	      << std::endl
	      << "    // get inputs and rates"					<< std::endl
	      << "    " << system_name << "_input(mttt,mttu,mttx,mtty);"	<< std::endl
	      << "    " << system_name << "_ode(mttt,mttu,mttx,mttdx,mtty);"	<< std::endl
	      << std::endl
	      << "    // integrate states (euler)"				<< std::endl
	      << "    for (i = 1; i < mttNx; i++){"				<< std::endl
	      << "      mttx[i] += mttdx[i] * mttdt;"				<< std::endl
	      << "    }"							<< std::endl
	      << std::endl
	      << "    // overwrite switch states"				<< std::endl
	      << "    " << system_name << "_logic(mttt,mttu,mttx,mttdx,mtty);"	<< std::endl
	      << std::endl
	      << "    // write: time outputs time states"			<< std::endl
	      << "    std::cout << mttt << '\\t';"				<< std::endl
	      << "    for (i = 1; i < mttNy; i++){"				<< std::endl
	      << "      std::cout << mtty[i] << ' ';"				<< std::endl 
	      << "    }"							<< std::endl
	      << "    std::cout << '\\t' << mttt;"				<< std::endl
	      << "    for (i = 1; i < mttNx; i++){"				<< std::endl
	      << "      std::cout << ' ' << mttx[i];"				<< std::endl 
	      << "    }"							<< std::endl
	      << "    std::cout << std::endl;"					<< std::endl
	      << "  }"								<< std::endl
	      << "  return 0;"							<< std::endl
	      << "}"								<< std::endl;

  // close files
  cr_file.close();
  parameter_file.close();
  structure_file.close();
  equations_file.close();
  cxxsim_file.close();

  return 0;
}

Modified mttroot/mtt/lib/rep/cxxsim_rep.make from [85ea44b9b2] to [6f40ee19ab].

1
2
3
4
5
6
7
8

9
10
11

12
13
14



15
16

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42



43
44
45
46
47
48
49
50

51
52
53
54


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*-makefile-*-
# $Id$
# $Log$
# Revision 1.1  2000/12/28 10:12:48  peterg
# Initial revision
#
# Revision 1.1  2000/08/31 14:31:28  peterg
# Initial revision

#
# Revision 1.3  2000/08/31 02:56:18  geraint
# Keep mkdir quiet if $(SYS)_Simulation_Dir exists

#
# Revision 1.2  2000/08/30 04:34:55  geraint
# *** empty log message ***



#


BIN=$(MTTPATH)/trans
INC=$(MTTPATH)/lib/cr/hh
SRC=$(MTTPATH)/trans/cc

CC=g++

INCLUDE=-I$(MTTPATH)/lib/cr/hh
OPTIMISE=-O3
WARNINGS=-Wall -ansi -pedantic

DIR=$(SYS)_Simulation_Dir
EXE=$(SYS)_cxxsim.exe
OBJ=$(SYS)_input.o $(SYS)_numpar.o $(SYS)_ode.o $(SYS)_init.o $(SYS)_cxxsim.o $(SYS)_switch.o

MTTFLAGS=-c -i euler -o -q

all: $(EXE)

$(SYS)_cxxsim.exe: $(OBJ) 
	echo Creating $(SYS)_cxxsim.exe
	$(CC) $(WARNINGS) $(OPTIMISE) $(INCLUDE) $(CFLAGS) $(OBJ) -o $(SYS)_cxxsim.exe
	mkdir -p ../$(DIR)
	cp *.cc ../$(DIR)
	cp *.hh ../$(DIR)
	cp *.o ../$(DIR)
	cp $(EXE) ../$(DIR)




$(SYS)_cxxsim.o: $(SYS)_cxxsim.cc $(SYS)_struc.hh
	echo Creating $(SYS)_cxxsim.o
	$(CC) $(WARNINGS) $(OPTIMISE) $(INCLUDE) $(CFLAGS) -c $(SYS)_cxxsim.cc

$(SYS)_cxxsim.cc: cxxsim.txt
	echo Creating $(SYS)_cxxsim.cc
	$(BIN)/cxxsim_txt2cc $(SYS) cxxsim < cxxsim.txt > $(SYS)_cxxsim.cc


cxxsim.txt:
	echo Creating cxxsim.txt:
	cp $(MTTPATH)/trans/cc/src/cxxsim.txt .



# list of constitutive relationships
$(SYS)_cr.hh: $(SYS)_cr.txt
	echo Creating $(SYS)_cr.hh
	$(BIN)/cr_txt2hh $(SYS) < $(SYS)_cr.txt > $(SYS)_cr.hh

$(SYS)_cr.txt:
	mtt $(MTTFLAGS) $(SYS) cr txt

# elementary system equations, Reduce format
$(SYS)_ese.r:
	echo Creating $(SYS)_ese.r
	mtt --version
	mtt $(MTTFLAGS) $(SYS) ese r

# state initialisation (MTT already has state.cc)
$(SYS)_init.o: $(SYS)_init.cc
	echo Creating $(SYS)_init.o
	$(CC) $(WARNINGS) $(OPTIMISE) $(INCLUDE) $(CFLAGS) -c $(SYS)_init.cc

$(SYS)_init.cc: $(SYS)_struc.txt
	echo Creating $(SYS)_init.cc
	$(BIN)/struc2state_txt2cc $(SYS) < $(SYS)_struc.txt > $(SYS)_init.cc

# system inputs
$(SYS)_input.o: $(SYS)_input.cc $(SYS)_cr.hh $(SYS)_numpar.hh
	echo Creating $(SYS)_input.o
	$(CC) $(WARNINGS) $(OPTIMISE) $(INCLUDE) $(CFLAGS) -c $(SYS)_input.cc

$(SYS)_input.cc: $(SYS)_struc.txt
	echo Creating $(SYS)_input.cc
	$(BIN)/struc2input_txt2cc $(SYS) < $(SYS)_struc.txt > $(SYS)_input.cc

# system numerical parameters
$(SYS)_numpar.o: $(SYS)_numpar.cc
	echo Creating $(SYS)_numpar.o
	$(CC) $(WARNINGS) $(OPTIMISE) $(INCLUDE) $(CFLAGS) -c $(SYS)_numpar.cc

$(SYS)_numpar.cc: $(SYS)_sympar.txt
	echo Creating $(SYS)_numpar.cc
	$(BIN)/sympar2numpar_txt2cc $(SYS) < $(SYS)_sympar.txt > $(SYS)_numpar.cc

$(SYS)_numpar.hh: $(SYS)_sympar.txt
	echo Creating $(SYS)_numpar.hh
	$(BIN)/sympar2numpar_txt2hh $(SYS) < $(SYS)_sympar.txt > $(SYS)_numpar.hh

$(SYS)_numpar.txt:
	echo Creating $(SYS)_numpar.txt
	mtt $(MTTFLAGS) $(SYS) numpar txt

# ordinary differential equations
$(SYS)_ode.o: $(SYS)_ode.cc $(SYS)_cr.hh $(SYS)_numpar.hh
	echo Creating $(SYS)_ode.o
	$(CC) $(WARNINGS) $(OPTIMISE) $(INCLUDE) $(CFLAGS) -c $(SYS)_ode.cc

$(SYS)_ode.cc: $(SYS)_ode.txt
	echo Creating $(SYS)_ode.cc
	$(BIN)/ode_txt2cc $(SYS) < $(SYS)_ode.txt > $(SYS)_ode.cc

$(SYS)_ode.txt: $(SYS)_ese.r
	echo Creating $(SYS)_ode.txt
	$(BIN)/stripComments '%' < $(SYS)_ese.r | $(BIN)/resolve $(SYS) > $(SYS)_ode.txt

# system structure, sizes of state-space matrices
$(SYS)_struc.hh: $(SYS)_struc.txt
	echo Creating $(SYS)_struc.hh
	$(BIN)/struc_txt2hh $(SYS) < $(SYS)_struc.txt > $(SYS)_struc.hh

$(SYS)_struc.txt:
	echo Creating $(SYS)_struc.txt
	mtt $(MTTFLAGS) $(SYS) struc txt

# logic control
$(SYS)_switch.o: $(SYS)_switch.cc $(SYS)_struc.txt $(SYS)_sympars.txt
	echo Creating $(SYS)_switch.o
	$(CC) $(WARNINGS) $(OPTIMISE) $(INCLUDE) $(CFLAGS) -c $(SYS)_switch.cc

$(SYS)_switch.cc: $(SYS)_struc.txt
	echo Creating $(SYS)_switch.cc
	$(BIN)/struc2switch_txt2cc $(SYS) < $(SYS)_struc.txt > $(SYS)_switch.cc

# list of symbolic parameters
$(SYS)_sympar.txt:
	echo Creating $(SYS)_sympar.txt
	mtt $(MTTFLAGS) $(SYS) sympar txt

# list of symbolic parameters and switch controls
$(SYS)_sympars.txt:
	echo Creating $(SYS)_sympars.txt
	mtt $(MTTFLAGS) $(SYS) sympars txt

<
<
<
<
|
<
<
>
|
<
|
>
|
<
<
>
>
>
|
|
>
|
<
<



|
|


|
|
<

<
|
<
|
|
|
|
<
<
<
<
<
>
>
>

|
|
|

|
|
<
>

|
|
<
>
>


<
<
<
<

|

|

<
<
|

<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

<
|

<
<
<
<
<
<
<
<
<


<
|

|
|
<
|
1




2


3
4

5
6
7


8
9
10
11
12
13
14


15
16
17
18
19
20
21
22
23

24

25

26
27
28
29





30
31
32
33
34
35
36
37
38
39

40
41
42
43

44
45
46
47




48
49
50
51
52


53
54









55











































56

57
58









59
60

61
62
63
64

65
# -*-makefile-*-







# usage: mtt <sys> cxxsim view


# example:
# mtt copy MotorGenerator && cd MotorGenerator && mtt MotorGenerator cxxsim view



# cxxsim creates a very simple C++ simulation without using Reduce
# it doesn't use MTT's normal .txt files so the resultant .cc must be edited 
# manually to alter parameter, input and logic values

# a more intelligent version would just do ese_r2cc to create <sys>_ese.cc
# this could then be embedded into MTT's normal code like <sys>_ode.cc
# maybe another day ...



CC=g++

INCLUDE=-I. -I${MTT_LIB}/cr/hh
OPTIMISE=-O0
WARNINGS=-Wall -ansi -pedantic


all: $(SYS)_cxxsim.$(LANG)



# view rule copied from gnuplot_rep.make

# need it here to prevent MTT using the default route (via dae)
$(SYS)_cxxsim.view: $(SYS)_gnuplot.wish $(SYS)_cxxsim.exe
	./$(SYS)_cxxsim.exe > $(SYS)_odes.dat2
	sh $(SYS)_gnuplot.wish			|\





		tee gnuplot_in.log		|\
		 gnuplot -geometry 400x300	\
		 > gnuplot_out.log 2> gnuplot_err.log &

$(SYS)_cxxsim.exe: $(SYS)_cxxsim.cc
	echo Creating $(SYS)_cxxsim.exe
	$(CC) -o $@ $^ $(OPTIMISE) $(WARNINGS) $(INCLUDE)

$(SYS)_cxxsim.cc: $(SYS)_cr.txt $(SYS)_ese.r $(SYS)_struc.txt $(SYS)_sympar.txt cxxsim
	./cxxsim $(SYS)

	cp $@ ..

cxxsim: ${MTT_LIB}/rep/cxxsim.cc
	echo creating $@

	echo Compiling $^
	$(CC) -o $@ $^ $(OPTIMISE) $(WARNINGS) $(INCLUDE)

# list of constitutive relationships




$(SYS)_cr.txt:
	mtt -q $(OPTS) $(SYS) cr txt

# elementary system equations
$(SYS)_ese.r:


	mtt -q $(OPTS) $(SYS) ese r










# system structure











































$(SYS)_struc.txt:

	mtt -q $(OPTS) $(SYS) struc txt










# list of symbolic parameters
$(SYS)_sympar.txt:

	mtt -q $(OPTS) $(SYS) sympar txt

# gnuplot script
$(SYS)_gnuplot.wish:

	mtt -q $(OPTS) $(SYS) gnuplot wish


MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]