rb-daspk 0.0.1-universal-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.
- data/lib/libdaspk.dylib +0 -0
- data/lib/rb-daspk.rb +205 -0
- metadata +65 -0
data/lib/libdaspk.dylib
ADDED
Binary file
|
data/lib/rb-daspk.rb
ADDED
@@ -0,0 +1,205 @@
|
|
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
|
+
|
29
|
+
require 'rubygems'
|
30
|
+
require 'ffi'
|
31
|
+
|
32
|
+
module FFI
|
33
|
+
class Pointer
|
34
|
+
def read_double
|
35
|
+
get_float64(0)
|
36
|
+
end
|
37
|
+
|
38
|
+
def write_double(obj)
|
39
|
+
put_float64(0, obj)
|
40
|
+
end
|
41
|
+
|
42
|
+
def read_array_of_double(length)
|
43
|
+
get_array_of_double(0, length)
|
44
|
+
end
|
45
|
+
|
46
|
+
def write_array_of_double(ary)
|
47
|
+
put_array_of_double(0, ary)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
module DASPK
|
54
|
+
extend FFI::Library
|
55
|
+
ffi_lib File.dirname(__FILE__) + '/libdaspk.dylib'
|
56
|
+
callback :jac, [:pointer,:pointer,:pointer,:pointer,:pointer,:pointer,:pointer,:pointer,:pointer], :void
|
57
|
+
callback :res, [:pointer,:pointer,:pointer,:pointer,:pointer,:pointer,:pointer,:pointer,:pointer], :void
|
58
|
+
attach_function 'ddaspk_', [:res, :pointer,:pointer,:pointer,:pointer,:pointer,:pointer,:pointer,:pointer,:pointer,
|
59
|
+
:pointer,:pointer,:pointer,:pointer,:pointer,:pointer,:jac,:pointer,:pointer,:pointer,
|
60
|
+
:pointer,:pointer,:pointer], :void
|
61
|
+
|
62
|
+
class Solver
|
63
|
+
|
64
|
+
include DASPK
|
65
|
+
|
66
|
+
attr_accessor :rtol,:atol,:ialgs,:jac,:maxstep
|
67
|
+
|
68
|
+
def initialize (neq, y, yprime, inter=nil, jac=nil, &res)
|
69
|
+
@lwr = 5000
|
70
|
+
@lwi = 5000
|
71
|
+
@plwr = FFI::MemoryPointer.new(:int)
|
72
|
+
@plwr.write_int(@lwr)
|
73
|
+
@plwi = FFI::MemoryPointer.new(:int)
|
74
|
+
@plwi.write_int(@lwi)
|
75
|
+
@work = [0]*@lwr
|
76
|
+
@pwork = FFI::MemoryPointer.new(:double,@lwr)
|
77
|
+
@piwork = FFI::MemoryPointer.new(:int,@lwi)
|
78
|
+
@info = [0]*30
|
79
|
+
@pinfo = FFI::MemoryPointer.new(:int,30)
|
80
|
+
@ialgs = [1]*neq
|
81
|
+
@res = res
|
82
|
+
@inter = inter
|
83
|
+
@rtol = 0.000001
|
84
|
+
@atol = 0.000001
|
85
|
+
@jac = jac
|
86
|
+
@maxstep =0
|
87
|
+
|
88
|
+
if (inter) then
|
89
|
+
@info[2]=1
|
90
|
+
end
|
91
|
+
|
92
|
+
@neq = neq
|
93
|
+
@pneq = FFI::MemoryPointer.new(:int)
|
94
|
+
@pneq.write_int(neq)
|
95
|
+
@y=y
|
96
|
+
@yprime=yprime
|
97
|
+
|
98
|
+
@py = FFI::MemoryPointer.new(:double,neq)
|
99
|
+
@py.write_array_of_double(y)
|
100
|
+
@pyprime = FFI::MemoryPointer.new(:double,neq)
|
101
|
+
@pyprime.write_array_of_double(yprime)
|
102
|
+
|
103
|
+
@prtol = FFI::MemoryPointer.new(:double)
|
104
|
+
@patol = FFI::MemoryPointer.new(:double)
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
def solve (time, tout)
|
109
|
+
|
110
|
+
@prtol.write_double(@rtol)
|
111
|
+
@patol.write_double(@atol)
|
112
|
+
|
113
|
+
#
|
114
|
+
# residual proc called from the Fortran. Let's pass it onto the user's residual procedure
|
115
|
+
# in a easier to use form
|
116
|
+
#
|
117
|
+
resproc = Proc.new do |p1,p2,p3,p4,p5,p6,p7,p8,p9|
|
118
|
+
delta = p5.read_array_of_double(@neq)
|
119
|
+
@res.call(p1.read_double,p2.read_array_of_double(@neq),p3.read_array_of_double(@neq),delta)
|
120
|
+
p5.write_array_of_double(delta)
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
if (@jac) then
|
125
|
+
@info[4]=1
|
126
|
+
else
|
127
|
+
@info[4]=0
|
128
|
+
end
|
129
|
+
#
|
130
|
+
# jacobian proc called from the Fortran. Let's pass it onto the user's jacboian procedure
|
131
|
+
# in a easier to use form
|
132
|
+
#
|
133
|
+
jacproc = Proc.new do |p1,p2,p3,p4,p5,p6,p7,p8,p9|
|
134
|
+
all = p4.read_array_of_double(@neq*@neq)
|
135
|
+
jacm = Array.new
|
136
|
+
for i in 0..(@neq-1) do
|
137
|
+
arow = Array.new
|
138
|
+
for j in 0..(@neq-1) do
|
139
|
+
arow << all[j*@neq+i] # Fortran stores in column major, though references in row major
|
140
|
+
end
|
141
|
+
jacm << arow
|
142
|
+
end
|
143
|
+
|
144
|
+
@jac.call(p1.read_double,p2.read_array_of_double(@neq),p3.read_array_of_double(@neq),jacm,p5.read_double)
|
145
|
+
|
146
|
+
for i in 0..(@neq-1) do
|
147
|
+
arow = jacm[i]
|
148
|
+
for j in 0..(@neq-1) do
|
149
|
+
all[j*@neq+i] = arow[j] # Fortran stores in column major, though references in row major
|
150
|
+
end
|
151
|
+
end
|
152
|
+
p4.write_array_of_double(all)
|
153
|
+
end
|
154
|
+
|
155
|
+
if (@maxstep!=0) then
|
156
|
+
@info[6]=1
|
157
|
+
@work[1]=@maxstep
|
158
|
+
else
|
159
|
+
@info[6]=0
|
160
|
+
@work[1]=@maxstep
|
161
|
+
end
|
162
|
+
|
163
|
+
ptime = FFI::MemoryPointer.new(:double)
|
164
|
+
ptime.write_double(time)
|
165
|
+
|
166
|
+
ptout = FFI::MemoryPointer.new(:double)
|
167
|
+
ptout.write_double(tout)
|
168
|
+
|
169
|
+
idid=1
|
170
|
+
pidid = FFI::MemoryPointer.new(:int)
|
171
|
+
pidid.write_int(idid)
|
172
|
+
|
173
|
+
@pwork.write_array_of_double(@work)
|
174
|
+
@pinfo.write_array_of_int(@info)
|
175
|
+
|
176
|
+
|
177
|
+
while (idid==1) do
|
178
|
+
ddaspk_(resproc,@pneq,ptime,@py,@pyprime,ptout,@pinfo,@prtol,@patol,pidid,@pwork,@plwr,@piwork,@plwi,nil,nil,jacproc,nil,nil,nil,nil,nil,nil)
|
179
|
+
idid = pidid.read_int
|
180
|
+
#
|
181
|
+
# report intermediate values
|
182
|
+
#
|
183
|
+
if ((idid==1)&&(@inter)) then
|
184
|
+
@inter.call(ptime.read_double,@py.read_array_of_double(@neq),@pyprime.read_array_of_double(@neq))
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
@work=@pwork.read_array_of_double(@lwr) # save the working ram
|
190
|
+
@info[0]=1
|
191
|
+
|
192
|
+
[ptime.read_double,@py.read_array_of_double(@neq),@pyprime.read_array_of_double(@neq),idid]
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
def reset
|
197
|
+
@info[0]=0
|
198
|
+
@py.write_array_of_double(@y)
|
199
|
+
@pyprime.write_array_of_double(@yprime)
|
200
|
+
end
|
201
|
+
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rb-daspk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: universal-darwin-10
|
6
|
+
authors:
|
7
|
+
- Eric Meyers
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-27 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ffi
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.5.3
|
24
|
+
version:
|
25
|
+
description: Allows users to solve differential algebraic equations in Ruby, using Ruby constructs. Interfaces with the DASPK Fortran library. DASPK solves DAE's of the form G(t,y,y',p) = 0
|
26
|
+
email: etm@ericmeyers.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/libdaspk.dylib
|
35
|
+
- lib/rb-daspk.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage:
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project: rb-daspk
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: DASPK Ruby Interface. Allows users to solve differential algebraic equations in Ruby, using Ruby constructs. Interfaces with the DASPK Fortran library. DASPK solves DAE's of the form G(t,y,y',p) = 0
|
64
|
+
test_files: []
|
65
|
+
|