rb-daspk 0.0.3-x86-darwin-10 → 0.0.4-x86-darwin-10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/examples/springmass.rb +130 -0
  2. data/lib/rb-daspk.rb +1 -2
  3. metadata +2 -2
  4. data/lib/test.rb +0 -132
@@ -0,0 +1,130 @@
1
+ # Copyright (c) 2009 Eric Todd Meyers
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person
4
+ # obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without
6
+ # restriction, including without limitation the rights to use,
7
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the
9
+ # Software is furnished to do so, subject to the following
10
+ # conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ # OTHER DEALINGS IN THE SOFTWARE.
23
+ #
24
+ #
25
+ # More information, source code and gems @ http://rubyforge.org/projects/rb-daspk/
26
+ #
27
+
28
+ require 'rubygems'
29
+ require 'rb-daspk'
30
+ include DASPK
31
+
32
+ #
33
+ # classic spring mass problem
34
+ #
35
+ # d2x/dt + 2*l*w0*dxdt + w0*w0*x = 0
36
+ #
37
+ # let v = dx/dt, then
38
+ #
39
+ # 1. dxdt - v = 0
40
+ # 2. dvdt + 2*l*w0*v + w0*w0*x = 0
41
+ #
42
+ #
43
+ # let's start with x at 1, dxdt at 0 (v=0)
44
+ # we'll ask DASPK for the state derivatives
45
+ #
46
+ y = [1.0,0.0]
47
+ yprime= [0.0,0.0] # these are gusses in this case. we will call solveForInitialValues to get them
48
+
49
+
50
+ w0 = 1.0
51
+ l = 0.0 # undamped
52
+
53
+ #
54
+ # in case we want to see details during the integration. in this case were just dumping values
55
+ # not required (can pass nil to Solver.new if you want)
56
+ #
57
+ inter = Proc.new do |time,y,yprime|
58
+ print " time = #{time} x = #{y[0]}\n"
59
+ end
60
+
61
+ jac = Proc.new do |time,y,yprime,jacm,cj|
62
+
63
+ jacm[0][0] = cj
64
+ jacm[0][1] = -1.0
65
+ jacm[1][0] = w0**2.0
66
+ jacm[1][1] = (2*l*w0 + cj)
67
+
68
+ end
69
+
70
+ s=Solver.new(2,y,yprime,inter,jac) do |time,y,yprime,delta|
71
+ #s=Solver.new(2,y,yprime) do |time,y,yprime,delta|
72
+ delta[0] = yprime[0] - y[1]
73
+ delta[1] = yprime[1] + 2.0*l*w0*y[1] + w0*w0*y[0]
74
+ end
75
+
76
+
77
+ #
78
+ # solve the initial value problem
79
+ # note, this isnt required if the y and yprimes are already consistent
80
+ #
81
+ # return code from solveFoeInitialValues
82
+ # 4 success (see DDASPK for all possible values)
83
+ #
84
+ idid = s.solveForInitialValues(0,y,yprime,[0,0])
85
+ print " ----------------------------------------\n"
86
+ print " initial conditions\n"
87
+ print " x = #{y[0]}\n"
88
+ print " vel = #{y[1]}\n"
89
+ print " accelaration = #{yprime[1]}\n"
90
+ print " ----------------------------------------\n"
91
+
92
+ #
93
+ # integrate to PI seconds
94
+ #
95
+ #
96
+ ans = s.solve(0.0,Math::PI)
97
+ #
98
+ # answer is an array of arrays
99
+ # ans[0] = end time
100
+ # ans[1] = end y array
101
+ # ans[2] = end yprime array
102
+ # ans[3] = DASPK return code (see DDASPK.F for meaning)
103
+ #
104
+ print " ----------------------------------------\n"
105
+ print " final conditions\n"
106
+ print " time = #{ans[0]}\n"
107
+ print " x = #{ans[1][0]}\n"
108
+ print " vel = #{ans[1][1]}\n"
109
+ print " accelaration = #{ans[2][1]}\n"
110
+ print " ----------------------------------------\n"
111
+
112
+ #
113
+ # continue to integrate to 2PI seconds
114
+ #
115
+ #
116
+ ans = s.solve(0.0,2*Math::PI)
117
+ #
118
+ # answer is an array of arrays
119
+ # ans[0] = end time
120
+ # ans[1] = end y array
121
+ # ans[2] = end yprime array
122
+ # ans[3] = DASPK return code (see DDASPK.F for meaning)
123
+ #
124
+ print " ----------------------------------------\n"
125
+ print " final conditions\n"
126
+ print " time = #{ans[0]}\n"
127
+ print " x = #{ans[1][0]}\n"
128
+ print " vel = #{ans[1][1]}\n"
129
+ print " accelaration = #{ans[2][1]}\n"
130
+ print " ----------------------------------------\n"
data/lib/rb-daspk.rb CHANGED
@@ -24,7 +24,6 @@
24
24
  #
25
25
  # More information, source code and gems @ http://rubyforge.org/projects/rb-daspk/
26
26
  #
27
- #:include:test.rb
28
27
 
29
28
  require 'rubygems'
30
29
  require 'ffi'
@@ -74,7 +73,7 @@ module DASPK
74
73
 
75
74
  include DASPK
76
75
 
77
- attr_accessor :rtol,:atol,:init,:jac,:maxstep,:mxnit,:mxnj,:mxnh,:lsoff,:stptol,:epinit
76
+ attr_accessor :rtol,:atol,:inter,:jac,:maxstep,:mxnit,:mxnj,:mxnh,:lsoff,:stptol,:epinit
78
77
  #
79
78
  #
80
79
  # * neq = the number of equations. this is always the sum of the number of states and algebraics
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb-daspk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: x86-darwin-10
6
6
  authors:
7
7
  - Eric Meyers
@@ -33,7 +33,7 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - lib/libdaspk.dylib
35
35
  - lib/rb-daspk.rb
36
- - lib/test.rb
36
+ - examples/springmass.rb
37
37
  has_rdoc: true
38
38
  homepage: http://rb-daspk.rubyforge.org
39
39
  licenses: []
data/lib/test.rb DELETED
@@ -1,132 +0,0 @@
1
- # Copyright (c) 2009 Eric Todd Meyers
2
- #
3
- # Permission is hereby granted, free of charge, to any person
4
- # obtaining a copy of this software and associated documentation
5
- # files (the "Software"), to deal in the Software without
6
- # restriction, including without limitation the rights to use,
7
- # copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- # copies of the Software, and to permit persons to whom the
9
- # Software is furnished to do so, subject to the following
10
- # conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
- # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
- # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
- # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
- # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
- # OTHER DEALINGS IN THE SOFTWARE.
23
- #
24
- #
25
- # More information, source code and gems @ http://rubyforge.org/projects/rb-daspk/
26
- #
27
-
28
- require 'rubygems'
29
- require 'rb-daspk'
30
- include DASPK
31
-
32
- y = [1.0,0.5]
33
- yprime = [0.0,0.0]
34
-
35
- #
36
- # in case the user wants to see details during the integration. in this case were just dumping values
37
- # not required
38
- #
39
- inter = Proc.new do |time,y,yprime|
40
- print "time = #{time} y0=#{y[0]} y1=#{y[1]}\n"
41
- end
42
-
43
-
44
- #
45
- # let's solve and easy one
46
- #
47
- # dy1/dy = y1
48
- # dy2/dt = 2*y2
49
- #
50
- # we all know the answer is (for the starting y's above)
51
- # y1 = e**t
52
- # y2 = e**(2t+ln(0.5))
53
- #
54
- # at 2 seconds we should get y1 = 7.389056098930649, y2 = 27.29907501657209
55
- #
56
- #
57
-
58
- #
59
- # explicit modified jacobian
60
- # not required, but used if passed to solver
61
- #
62
- jac = Proc.new do |time,y,yprime,pd,cj|
63
- pd[0][0] = 1.0 - cj # first equation with respect to first var
64
- pd[0][1] = 0.0 # first equation with respect to second var
65
- pd[1][0] = 0.0 # second equation with respect to first var
66
- pd[1][1] = 2.0 - cj # second equation with respect to second var
67
- end
68
-
69
- s=Solver.new(2,y,yprime,inter, jac) do |time,yy,yyprime,delta|
70
- #s=Solver.new(2,y,yprime,inter) do |time,yy,yyprime,delta|
71
- delta[0] = yy[0] - yyprime[0]
72
- delta[1] = 2.0*yy[1] - yyprime[1]
73
- end
74
-
75
-
76
- #
77
- # solve the initial value problem
78
- # this is only required if the Y's and Y'primes are not
79
- # already consistent
80
- #
81
- # y and yprime will be modified
82
- #
83
- #
84
- #
85
- #
86
- #
87
- #
88
- ans = s.solveForInitialValues(0,y,yprime,[0,0],1)
89
- print " y0=#{y[0]} y1=#{y[1]}\n"
90
- print " yp0=#{yprime[0]} yp1=#{yprime[1]}\n"
91
- print "idid = #{ans}\n"
92
-
93
- #
94
- # integrate to 1 second
95
- #
96
- ans = s.solve(0.0,1.0)
97
-
98
-
99
- #print final values
100
- time = ans[0]
101
- ya = ans[1]
102
- ypa = ans[2]
103
- print "time = #{time} y0=#{ya[0]} y1=#{ya[1]}\n"
104
- print "time = #{time} yp0=#{ypa[0]} yp1=#{ypa[1]}\n"
105
-
106
- #
107
- # lets keep going. should start at the last time we left off regardless of the sart time
108
- # integrate to 2 seconds
109
- #
110
- ans = s.solve(0.0,2.0)
111
- #print final values
112
- time = ans[0]
113
- ya = ans[1]
114
- ypa = ans[2]
115
- print "time = #{time} y0=#{ya[0]} y1=#{ya[1]}\n"
116
- print "time = #{time} yp0=#{ypa[0]} yp1=#{ypa[1]}\n"
117
-
118
- #
119
- # reset integration. should go back to tzero
120
- # integrate to 2 seconds
121
- #
122
- s.reset
123
-
124
- ans = s.solve(0.0,2.0)
125
- #print final values
126
- time = ans[0]
127
- ypa = ans[2]
128
- ya = ans[1]
129
- print "time = #{time} y0=#{ya[0]} y1=#{ya[1]}\n"
130
- print "time = #{time} yp0=#{ypa[0]} yp1=#{ypa[1]}\n"
131
-
132
-