-
Notifications
You must be signed in to change notification settings - Fork 0
/
series.pl
120 lines (112 loc) · 2.92 KB
/
series.pl
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
# Legendre second order method for linear advection
# Copyright (C) 2012 Borja Latorre - [email protected]
#
# For the latest updates, please visit:
# https://github.com/B0RJA/legendre-1D
#
# 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 3 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, see:
# http://www.gnu.org/licenses/
# TODO: Optimize the maxima API by keeping the command open
# or compute all the integrals at the same time.
# Program configuration
$cell_count = 100;
$domain_size = 1.0;
$scalar = 'exp( - 50 * ( x - 1 / 2 ) ^ 2 )';
$velocity = '1 / ( 1 + cos( 2 * 3.141592653589793 * x ) / 5 )';
$order = 2;
# Change depending on your OS
$program = 'maxima'; # UNIX
#$program = 'maxima.exe'; # Windows
$dx = $domain_size / $cell_count;
# Check maxima installation
$int = maxima('romberg(legendre_p(0,x),x,-1,1)');
$int = sprintf("%.7f", $int);
if($int ne '2.0000000')
{
print "Maxima 5.21.1 or greater is required.\n";
print "http://maxima.sourceforge.net\n";
print "If already installed, check variable \$program in the script.\n";
exit;
}
# Integrate scalar
open F, '>'.'initial/m_'.$cell_count.'.txt';
for( $i=0 ; $i<$cell_count ; $i++ )
{
$x1 = $i * $dx;
$x2 = ( $i + 1 ) * $dx;
$f = $scalar;
$f =~ s/exp/eyp/g;
$f =~ s/x/\($i\+\(x\+1\)\/2\)\*$domain_size\/$cell_count/g;
$f =~ s/eyp/exp/g;
for( $l=1 ; $l<=$order ; $l++ )
{
$int = maxima('(2*'.$l.'-1)/2*romberg(legendre_p('.$l.'-1,x)*'.$f.',x,-1,1)');
if($int eq ''){
$int = 0.0;
}
$int = sprintf("%.13le", $int);
print F $int."\n";
}
}
close F;
# Integrate velocity
open F, '>'.'initial/v_'.$cell_count.'.txt';
for( $i=0 ; $i<$cell_count ; $i++ )
{
$x1 = $i * $dx;
$x2 = ( $i + 1 ) * $dx;
$f = $velocity;
$f =~ s/exp/eyp/g;
$f =~ s/x/\($i\+\(x\+1\)\/2\)\*$domain_size\/$cell_count/g;
$f =~ s/eyp/exp/g;
for( $l=1 ; $l<=$order ; $l++ )
{
$int = maxima('(2*'.$l.'-1)/2*romberg(legendre_p('.$l.'-1,x)*'.$f.',x,-1,1)');
if($int eq ''){
$int = 0.0;
}
$int = sprintf("%.13le", $int);
print F $int."\n";
}
}
close F;
exit;
# Maxima API
sub maxima
{
my $start = $program . ' --batch-string="display2d:false;';
my $end = ';" -q';
my $cmd = $start . 'float('.$_[0].')' . $end;
open(PS_F, "$cmd|");
my $save = 0;
my $line = '';
while (<PS_F>)
{
my $x = $_;
if( $x =~ /\(\%o2\)/ )
{
$save = 1;
$x =~ s/\(\%o2\)//g;
}
if( $save == 1 )
{
$line = $line . $x;
}
}
close(PS_F);
$line =~ s/[\r\n\s]+//g;
return $line;
}