Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/post/src/lib/mc.ini
3203 views
1
! freopen(stderr,"/dev/null","w");
2
3
function clear
4
{
5
"\e[H\e[2J";
6
! "\eH\eJ";
7
}
8
9
!clear
10
" MATC Matrix Language "
11
" (Version 0.9) "
12
" "
13
14
function tensor(x,y,oper)
15
{
16
if ((~exists("x")) | (~exists("y")))
17
{
18
"Usage: tensor(x,y[,oper])";
19
} else
20
{
21
if (~exists("oper")) oper = "*";
22
23
make = "tmp = x[i,j]" oper "y";
24
25
nx = size(x)[0]-1;
26
mx = size(x)[1]-1;
27
ny = size(y)[0];
28
my = size(y)[1];
29
k = 0:ny-1;
30
31
for(i=0:nx)
32
{
33
l = 0:my-1;
34
for(j=0:mx)
35
{
36
rubbish = @make;
37
_tensor[k,l] = tmp;
38
l = l + my;
39
}
40
k = k + ny;
41
}
42
}
43
}
44
45
function pointw(x,y,oper)
46
{
47
if ((~exists("x")) | (~exists("y")))
48
{
49
"Usage: pointw(x,y[,oper])";
50
} else
51
{
52
if (~exists("oper")) oper = "*";
53
54
make = "tmp = (x[i,j])" oper "(y[i,j])";
55
56
nx = size(x)[0]-1;
57
mx = size(x)[1]-1;
58
59
for(i=0:nx)
60
{
61
for(j=0:mx)
62
{
63
rubbish = @make;
64
_pointw[i,j] = tmp;
65
}
66
}
67
}
68
}
69
70
function vdot(x,y)
71
!
72
! Return dot product of two arrays of vectors x(0:2,n) and y(0:2,n)
73
!
74
{
75
n = size(x)[1]
76
n = 0:n-1
77
78
s = x*y
79
_vdot(n) = s(0,n) + s(1,n) + s(2,n)
80
}
81
82
function vcross(x,y)
83
!
84
! Return cross product of two arrays of vectors x(0:2,n) and y(0:2,n)
85
!
86
{
87
n = size(x)[1]
88
n = 0:n-1
89
90
_vcross(2,n) = x(0,n)*y(1,n) - x(1,n)*y(0,n);
91
_vcross(1,n) = x(2,n)*y(0,n) - x(0,n)*y(2,n);
92
_vcross(0,n) = x(1,n)*y(2,n) - x(2,n)*y(1,n);
93
}
94
95
function time(t,x)
96
!
97
! Return indexes for accessing given timestep of variables of current
98
! element model loaded in elmer-post.
99
!
100
! for example: t=velo(0,time(5)) gives x-component of velocity in sixth
101
! timestep (counting begins from zero). time("all") gives indexes for
102
! the whole timestep range.
103
!
104
import temp,nodes
105
{
106
if ( t=="all" )
107
{
108
if ( exists("x") ) {
109
_time = 0:size(x)[1]-1;
110
} else {
111
"Usage: time(\"all\", x);"
112
}
113
} else {
114
n = size(nodes)[1]
115
_time = t*n + (0:(n-1))
116
}
117
}
118
119
120
function dfdt(f,dt)
121
!
122
! compute time derivative of given scalar or vector function
123
!
124
import nodes
125
{
126
n = size(nodes)[1];
127
nf = size(f)[0];
128
nt = size(f)[1] / n;
129
130
if ( ~exists("dt") ) dt = 1
131
132
_dfdt = 0;
133
ddt = 1.0 / dt
134
135
if ( nt > 1 )
136
{
137
k0 = time(0);
138
k1 = time(1);
139
for( j=0:nf-1 )
140
{
141
_dfdt(j,k0) = ddt * ( f(j,k1) - f(j,k0) )
142
}
143
144
for( k=1:nt-2 )
145
{
146
k0 = time(k-1);
147
k1 = time(k);
148
k2 = time(k+1);
149
for( j=0:nf-1 )
150
{
151
_dfdt(j,k1) = 0.5 * ddt * ( f(j,k2) - f(j,k0) )
152
}
153
}
154
155
k0 = time(nt-2);
156
k1 = time(nt-1);
157
for( j=0:nf-1 )
158
{
159
_dfdt(j,k1) = ddt * ( f(j,k1) - f(j,k0) )
160
}
161
}
162
}
163
164
function norm(x,t)
165
export iter,eps
166
import eps,Inf
167
{
168
if (~exists("x"))
169
{
170
"Usage: norm(x[,t])";
171
} else
172
{
173
if (~exists("eps")) eps = 1.0e-16;
174
if (~exists("t")) t = 2;
175
176
rows = size(x)[0];
177
cols = size(x)[1];
178
179
if (rows > 1 & cols > 1)
180
{
181
if (t == 1)
182
{
183
_norm = max(sum(abs(x')));
184
} \
185
else if (t == Inf)
186
{
187
_norm = max(sum(abs(x)));
188
} \
189
else if (t == 2)
190
{
191
y = rows 1 % 1;
192
xt = x' * x;
193
k = 0;
194
l = 1/0;
195
l1 = 0;
196
while(abs(l-l1) >= eps & k < 100)
197
{
198
z = xt * y;
199
l1 = l;
200
l = (y'*z)/(y'*y);
201
m = 1/sqrt(sum(abs(z)^2));
202
y = m * z;
203
k = k + 1;
204
}
205
iter = k;
206
_norm = sqrt(l);
207
} \
208
else if (t == 2.1)
209
{
210
_norm = sqrt(max(eig(x'*x)[0:rows-1,0]));
211
} \
212
else if (t == "f")
213
{
214
_norm = sqrt(sum(diag(x'*x)));
215
}
216
} \
217
else
218
{
219
if (t == Inf)
220
{
221
_norm = max(abs(x));
222
} \
223
else if (t == -Inf)
224
{
225
_norm = min(abs(x));
226
} \
227
else if (t <> 0)
228
{
229
_norm = sum(abs(x)^t)^(1/t);
230
}
231
}
232
}
233
}
234
235
function jac(a, b)
236
import eps
237
export eps
238
{
239
if (~exists("a"))
240
{
241
"Usage: jac(a[,b])";
242
} \
243
else
244
{
245
n = size(a)[0];
246
if (~exists("b")) b = eye(size(a)[0]);
247
if (~exists("eps")) eps = 1.0e-16;
248
ev = jacob(a, b, eps);
249
_jac = eigv;
250
_jac[0:n-1,n] = ev';
251
}
252
}
253
254
function triu(a)
255
{
256
n = size(a)[0];
257
m = size(a)[1];
258
t = min(size(a));
259
_triu = n m % 0;
260
i = 0;
261
for(i=0:t-1)
262
{
263
_triu[i,i:m-1] = a[i,i:m-1];
264
}
265
}
266
267
function tril(a)
268
{
269
n = size(a)[0];
270
m = size(a)[1];
271
t = min(size(a));
272
_tril = n m % 0;
273
for(i=0:t-1)
274
{
275
_tril[i:n-1,i] = a[i:n-1,i];
276
}
277
}
278
279
function sort(x)
280
import Inf
281
{
282
n = size(x)[0] * size(x)[1];
283
_sort = x;
284
if (n > 1)
285
{
286
t = 1 n % x;
287
v = 0:n-1;
288
i = 0;
289
while(i < n)
290
{
291
m = min(t);
292
w = t[t==m];
293
j = size(w)[1];
294
y[i:i+j-1] = w;
295
t[t==m] = Inf;
296
i = i + j;
297
}
298
_sort = size(x) % y;
299
}
300
}
301
302
function plot(y, x)
303
! PLOT curves from argument Y as function of argument X
304
! if argument X is not present use Y:s column index for
305
! X axis.
306
!
307
! Curves plotted are rows of the argument matrix Y.
308
!
309
! Device can be selected by setting of variable gdevice,
310
! x- and y axis lablebs by gxlabel and gylabel respectively
311
! and plot title by gtitle.
312
!
313
import gdevice,gxlabel,gylabel,gtitle
314
export gdevice
315
{
316
ymax = max(max(y));
317
ymin = min(min(y));
318
319
np = size(y)[1];
320
ny = np - 1;
321
322
if (~exists("x"))
323
{
324
xmin = 1;
325
xmax = np;
326
x=1:xmax;
327
} else
328
{
329
xmin = min(x);
330
xmax = max(x);
331
}
332
333
if ( ~exists("gdevice") ) askgdev
334
gopen( gdevice );
335
336
gwindow(0 1 0 1 0 1); ! Set default window and viewport for axis drawing
337
gviewport(0 1 0 1);
338
339
gclear; ! Clear screen
340
341
gcolor(1); ! Use colorindex one
342
343
gmove(0 0 0); ! Plot frame around coordinate space
344
gdraw(1 0 0);
345
gdraw(1 1 0);
346
gdraw(0 1 0);
347
gdraw(0 0 0);
348
349
gmove(0.1 0.9 0); ! Axis vectors
350
gdraw(0.1 0.1 0);
351
gdraw(0.9 0.1 0);
352
353
i = 0.9; ! (X Axis) tick marks and values
354
t = xmax;
355
s = (xmax-xmin)/8;
356
while(i > 0.1)
357
{
358
gmove(i 0.1 0);
359
gdraw(i 0.075 0);
360
str=sprintf("%.3g",t);
361
gmove((i-size(str)[1]*0.013/2) 0.05 0);
362
gtext(0.013 0, str);
363
t=t-s;
364
i=i-0.1;
365
}
366
367
i = 0.9; ! (Y Axis) tick marks and values
368
t = ymax;
369
s = (ymax-ymin)/8;
370
while(i > 0.1)
371
{
372
gmove(0.1 i 0);
373
gdraw(0.075 i 0);
374
str=sprintf("%.3g",t);
375
gmove(0.05 (i-size(str)[1]*0.013/2) 0);
376
gtext(0.013 90, str);
377
t=t-s;
378
i=i-0.1;
379
}
380
381
if (exists("gxlabel")) {
382
gmove((0.5-size(gxlabel)[1]*0.019/2) 0.025 0); ! X Axis label
383
gtext(0.019 0, gxlabel);
384
}
385
386
if (exists("gylabel")) {
387
gmove(0.025 (0.5-size(gylabel)[1]*0.019/2) 0); ! Y Axis label
388
gtext(0.019 90, gylabel);
389
}
390
391
if (~exists("gtitle")) gtitle="MATC PLOT"; ! Title
392
gmove((0.5-size(gtitle)[1]*0.03/2) 0.925 0);
393
gtext(0.03 0, gtitle);
394
395
gwindow(xmin xmax ymin ymax 0 1); ! Set world coordinates
396
gviewport(0.1 0.9 0.1 0.9);
397
398
n = size(y)[0];
399
p[0:ny,0]=x;
400
p[0:ny,2]=0;
401
for(i=0:n-1) ! Plot curves one at a time; each
402
{ ! with a new color
403
p[0:ny,1]=y[i,0:ny];
404
gcolor(i+1);
405
gpolyline(np, p);
406
}
407
408
gclose;
409
}
410
411
function contour( mat, scale, vscale )
412
!
413
! Plot equal value color contours from matrix mat. Axis numbering scales
414
! can be given with second parameter.
415
!
416
! Device can be selected by setting of variable gdevice,
417
! x- and y axis lablebs by gxlabel and gylabel respectively
418
! and plot title by gtitle.
419
!
420
import gdevice,gxlabel,gylabel,gtitle
421
export gdevice
422
{
423
if (~exists("scale"))
424
{
425
ymin = 1;
426
ymax = size(mat)[0];
427
xmin = 1;
428
xmax = size(mat)[1];
429
} else
430
{
431
xmin = scale[0];
432
xmax = scale[1];
433
ymin = scale[2];
434
ymax = scale[3];
435
}
436
437
if ( ~exists("vscale") ) vscale=1
438
439
if (~exists("gdevice")) askgdev
440
gopen(gdevice);
441
442
gwindow(0 1 0 1 0 1); ! Set default window and viewport for axis drawing
443
gviewport(0 1 0 vscale);
444
445
gclear; ! Clear screen
446
447
gcolor(1); ! Use colorindex one
448
449
gmove(0 0 0); ! Plot frame around coordinate space
450
gdraw(1 0 0);
451
gdraw(1 1 0);
452
gdraw(0 1 0);
453
gdraw(0 0 0);
454
455
gmove(0.1 0.9 0); ! Axis vectors
456
gdraw(0.1 0.1 0);
457
gdraw(0.9 0.1 0);
458
459
i = 0.9; ! (X Axis) tick marks and values
460
t = xmax;
461
s = (xmax-xmin)/8;
462
while(i > 0.1)
463
{
464
gmove(i 0.1 0);
465
gdraw(i 0.075 0);
466
str=sprintf("%.3g",t);
467
gmove((i-size(str)[1]*0.013/2) 0.05 0);
468
gtext(0.013 0, str);
469
t=t-s;
470
i=i-0.1;
471
}
472
473
i = 0.9; ! (Y Axis) tick marks and values
474
t = ymax;
475
s = (ymax-ymin)/8;
476
while(i > 0.1)
477
{
478
gmove(0.1 i 0);
479
gdraw(0.075 i 0);
480
str=sprintf("%.3g",t);
481
gmove(0.05 (i-size(str)[1]*0.013/2) 0);
482
gtext(0.013 90, str);
483
t=t-s;
484
i=i-0.1;
485
}
486
487
if (exists("gxlabel")) {
488
gmove((0.5-size(gxlabel)[1]*0.019/2) 0.025 0); ! X Axis label
489
gtext(0.019 0, gxlabel);
490
}
491
492
if (exists("gylabel")) {
493
gmove(0.025 (0.5-size(gylabel)[1]*0.019/2) 0); ! Y Axis label
494
gtext(0.019 90, gylabel);
495
}
496
497
if (~exists("gtitle")) gtitle="MATC C3D/CONTOUR"; ! Title
498
gmove((0.5-size(gtitle)[1]*0.03/2) 0.925 0);
499
gtext(0.03 0, gtitle);
500
501
gviewport(0.1 0.9 0.1 (vscale-0.1) );
502
503
!
504
! array of pretty color selections
505
!
506
cols = 14 3 % 0 0 100 \
507
0 33 100 \
508
0 66 100 \
509
0 100 100 \
510
0 33 0 \
511
0 66 0 \
512
0 100 0 \
513
100 100 0 \
514
100 100 0 \
515
100 66 0 \
516
100 33 0 \
517
100 0 0 \
518
100 0 100 \
519
100 33 100 \
520
100 66 100;
521
522
! load colors
523
for(i=2:15)
524
{
525
gdefcolor(i, cols[i-2,0 1 2]/100.0);
526
}
527
528
gc3dlevels(13); ! number of color levels
529
gc3d(mat); ! do it
530
531
gclose;
532
}
533
534
function mesh(mat, vp)
535
!
536
! Plot 3D mesh from matrix mat. Viewpoint can be
537
! given with optional second parameter. Plot device is selected by
538
! imported variable gdevice, the title of the plot can be changed by
539
! variable gtitle.
540
!
541
import gdevice,gtitle
542
export gdevice
543
{
544
if (~exists("gdevice")) askgdev
545
gopen(gdevice);
546
547
gwindow(0 1 0 1 0 1); ! Set default window and viewport for axis drawing
548
gviewport(0 1 0 1);
549
550
gclear; ! Clear screen
551
552
gcolor(1); ! Use colorindex one
553
554
gmove(0 0 0); ! Plot frame around coordinate space
555
gdraw(1 0 0);
556
gdraw(1 1 0);
557
gdraw(0 1 0);
558
gdraw(0 0 0);
559
560
if (~exists("gtitle")) gtitle="MATC C3D/MESH"; ! Title
561
gmove((0.5-size(gtitle)[1]*0.03/2) 0.925 0);
562
gtext(0.03 0, gtitle);
563
564
gviewport(0.1 0.9 0.1 0.9);
565
566
!
567
! Set default or user given viewpoint
568
!
569
if (exists("vp"))
570
{
571
gviewpoint(vp);
572
} else
573
{
574
gviewpoint(1 3 % max(max(mat)));
575
}
576
577
gmove(0 0 0);
578
gdraw(0 0 1);
579
gmove(0 0 0);
580
gdraw(0 1 0);
581
gmove(0 0 0);
582
gdraw(1 0 0);
583
584
gc3dlevels(0); ! number of color levels
585
gc3d(mat); ! do it
586
587
gclose;
588
}
589
590
function cmesh(mat, vp)
591
!
592
! Plot 3D mesh and its equal value color contours from matrix mat.
593
! Viewpoint can be given with optional second parameter.
594
! Plot device is selected by imported variable gdevice, the
595
! title of the plot can be changed by setting of gtitle.
596
!
597
import gdevice,gtitle
598
export gdevice
599
{
600
if (~exists("gdevice")) askgdev;
601
gopen(gdevice);
602
603
gwindow(0 1 0 1 0 1); ! Set default window and viewport for axis drawing
604
gviewport(0 1 0 1);
605
606
gclear; ! Clear screen
607
608
gcolor(1); ! Use colorindex one
609
610
gmove(0 0 0);
611
gdraw(1 0 0);
612
gdraw(1 1 0);
613
gdraw(0 1 0);
614
gdraw(0 0 0);
615
616
if (~exists("gtitle")) gtitle="MATC C3D/CMESH"; ! Title
617
gmove((0.5-size(gtitle)[1]*0.03/2) 0.925 0);
618
gtext(0.03 0, gtitle);
619
620
gviewport(0.1 0.9 0.1 0.9);
621
622
!
623
! array of pretty color selections
624
!
625
cols = 14 3 % 0 0 100 \
626
0 33 100 \
627
0 66 100 \
628
0 100 100 \
629
0 33 0 \
630
0 66 0 \
631
0 100 0 \
632
100 100 0 \
633
100 100 0 \
634
100 66 0 \
635
100 33 0 \
636
100 0 0 \
637
100 0 100 \
638
100 66 100;
639
640
! load colors
641
for(i=2:15)
642
{
643
gdefcolor(i, cols[i-2,0 1 2]/100.0);
644
}
645
646
!
647
! Set default or user given viewpoint
648
!
649
if (exists("vp"))
650
{
651
gviewpoint(vp);
652
} else
653
{
654
gviewpoint(1 3 % max(max(mat)));
655
}
656
657
gc3dlevels(13); ! number of color levels
658
gc3d(mat); ! do it
659
660
gclose;
661
}
662
663
function printf(fmt,values)
664
{
665
if (exists("values"))
666
{
667
fprintf(stdout,fmt,values);
668
} else
669
{
670
fprintf(stdout,fmt);
671
}
672
}
673
674
function scanf(fmt)
675
{
676
_scanf = fscanf(stdin,fmt);
677
}
678
679
function gets(str)
680
{
681
_gets = fgets(stdin);
682
}
683
684
function puts(str)
685
{
686
fputs(stdout,str);
687
}
688
689
function tovector(a,b,c)
690
{
691
n=0:size(a)[1]-1;
692
_tovector(0,n)=a;
693
_tovector(1,n)=b;
694
if (exists("c"))
695
{
696
_tovector(2,n)=c;
697
} else
698
{
699
_tovector(2,n)=0;
700
}
701
}
702
703
NaN = ln(-1);
704
Inf = 1/0;
705
eps = 1.0e-15;
706
! pi = 4 * atan(1);
707
708
format(6,1)
709
gdevice = 4
710
711
! source("./.mcinit");
712
713
fclose(stderr);
714
715