/tags/R2007-03-28/octave-forge/main/odepkg/inst/odepkg_structure_check.m

# · MATLAB · 365 lines · 194 code · 31 blank · 140 comment · 44 complexity · 1c4c70ece57bc313ec52a82c9b67156d MD5 · raw file

  1. %# Copyright (C) 2006, Thomas Treichl <treichl@users.sourceforge.net>
  2. %# OdePkg - Package for solving ordinary differential equations with octave
  3. %#
  4. %# This program is free software; you can redistribute it and/or modify
  5. %# it under the terms of the GNU General Public License as published by
  6. %# the Free Software Foundation; either version 2 of the License, or
  7. %# (at your option) any later version.
  8. %#
  9. %# This program is distributed in the hope that it will be useful,
  10. %# but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. %# GNU General Public License for more details.
  13. %#
  14. %# You should have received a copy of the GNU General Public License
  15. %# along with this program; if not, write to the Free Software
  16. %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. %# -*- texinfo -*-
  18. %# @deftypefn {Function} {@var{odestruct} =} odepkg_structure_check (@var{odestruct})
  19. %# Checks the field names and the field values of the odepkg option structure @var{odestruct} and returns it again if it is valid. If an invalid field name or an invalid field value is detected then the function terminates with an error.
  20. %#
  21. %# Run
  22. %# @example
  23. %# demo odepkg_structure_check
  24. %# @end example
  25. %# to see an example.
  26. %# @end deftypefn
  27. %#
  28. %# @seealso{odepkg}
  29. %# Maintainer: Thomas Treichl
  30. %# Created: 20060809
  31. %# ChangeLog:
  32. function [vret] = odepkg_structure_check (vret)
  33. if (nargin == 0) %# Check number of input arguments
  34. vmsg = sprintf ('Number of input arguments must be greater than zero\n');
  35. help ('odepkg_structure_check'); error (vmsg);
  36. else
  37. vint.fld = fieldnames (vret); vint.len = length (vint.fld);
  38. end
  39. for vcntarg = 1:vint.len %# Run through the number of structure field names
  40. switch (vint.fld{vcntarg})
  41. case 'RelTol'
  42. if (isreal (vret.(vint.fld{vcntarg})) == true && ...
  43. (isvector (vret.(vint.fld{vcntarg})) == true || ...
  44. isscalar (vret.(vint.fld{vcntarg})) == true) && ...
  45. all (vret.(vint.fld{vcntarg}) > 0) == true) %# LabMat needs 'all'?!
  46. else
  47. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  48. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  49. error (vmsg);
  50. end
  51. case 'AbsTol'
  52. if (isreal (vret.(vint.fld{vcntarg})) == true && ...
  53. (isvector (vret.(vint.fld{vcntarg})) == true || ...
  54. isscalar (vret.(vint.fld{vcntarg})) == true) && ...
  55. all (vret.(vint.fld{vcntarg}) > 0) == true) %# LabMat needs 'all'?!
  56. else
  57. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  58. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  59. error (vmsg);
  60. end
  61. case 'NormControl'
  62. if (strcmp (vret.(vint.fld{vcntarg}), 'on') == true || ...
  63. strcmp (vret.(vint.fld{vcntarg}), 'off') == true)
  64. else
  65. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  66. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  67. error (vmsg);
  68. end
  69. case 'NonNegative'
  70. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  71. isvector (vret.(vint.fld{vcntarg})) == true)
  72. else
  73. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  74. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  75. error (vmsg);
  76. end
  77. case 'OutputFcn'
  78. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  79. isa (vret.(vint.fld{vcntarg}), 'function_handle') == true)
  80. else
  81. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  82. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  83. error (vmsg);
  84. end
  85. case 'OutputSel'
  86. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  87. isvector (vret.(vint.fld{vcntarg})) == true || ...
  88. isscalar (vret.(vint.fld{vcntarg})) == true)
  89. else
  90. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  91. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  92. error (vmsg);
  93. end
  94. case 'Refine'
  95. if (mod (vret.(vint.fld{vcntarg}), 1) == 0 && ...
  96. vret.(vint.fld{vcntarg}) >= 0 && ...
  97. vret.(vint.fld{vcntarg}) <= 5)
  98. else
  99. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  100. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  101. error (vmsg);
  102. end
  103. case 'Stats'
  104. if (strcmp (vret.(vint.fld{vcntarg}), 'on') == true || ...
  105. strcmp (vret.(vint.fld{vcntarg}), 'off') == true)
  106. else
  107. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  108. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  109. error (vmsg);
  110. end
  111. case 'InitialStep'
  112. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  113. (isreal (vret.(vint.fld{vcntarg})) == true && ...
  114. vret.(vint.fld{vcntarg}) > 0) )
  115. else
  116. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"', ...
  117. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  118. error (vmsg);
  119. end
  120. case 'MaxStep'
  121. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  122. (isscalar (vret.(vint.fld{vcntarg})) == true && vret.(vint.fld{vcntarg}) > 0) )
  123. else
  124. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  125. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  126. error (vmsg);
  127. end
  128. case 'Events'
  129. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  130. isa (vret.(vint.fld{vcntarg}), 'function_handle') == true)
  131. else
  132. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  133. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  134. error (vmsg);
  135. end
  136. case 'Jacobian'
  137. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  138. ismatrix (vret.(vint.fld{vcntarg})) == true || ...
  139. isa (vret.(vint.fld{vcntarg}), 'function_handle') == true)
  140. else
  141. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  142. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  143. error (vmsg);
  144. end
  145. case 'JPattern'
  146. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  147. issparse (vret.(vint.fld{vcntarg})) == true)
  148. else
  149. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  150. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  151. error (vmsg);
  152. end
  153. case 'Vectorized'
  154. if (strcmp (vret.(vint.fld{vcntarg}), 'on') == true || ...
  155. strcmp (vret.(vint.fld{vcntarg}), 'off') == true)
  156. else
  157. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  158. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  159. error (vmsg);
  160. end
  161. case 'Mass'
  162. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  163. ismatrix (vret.(vint.fld{vcntarg})) == true || ...
  164. isa (vret.(vint.fld{vcntarg}), 'function_handle') == true)
  165. else
  166. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  167. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  168. error (vmsg);
  169. end
  170. case 'MStateDependence'
  171. if (strcmp (vret.(vint.fld{vcntarg}), 'none') == true || ...
  172. strcmp (vret.(vint.fld{vcntarg}), 'weak') == true || ...
  173. strcmp (vret.(vint.fld{vcntarg}), 'strong') == true)
  174. else
  175. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  176. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  177. error (vmsg);
  178. end
  179. case 'MvPattern'
  180. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  181. issparse (vret.(vint.fld{vcntarg})) == true)
  182. else
  183. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  184. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  185. error (vmsg);
  186. end
  187. case 'MassSingular'
  188. if (strcmp (vret.(vint.fld{vcntarg}), 'yes') == true || ...
  189. strcmp (vret.(vint.fld{vcntarg}), 'no') == true || ...
  190. strcmp (vret.(vint.fld{vcntarg}), 'maybe') == true)
  191. else
  192. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  193. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  194. error (vmsg);
  195. end
  196. case 'InitialSlope'
  197. if (isempty (vret.(vint.fld{vcntarg})) == true || ...
  198. isvector (vret.(vint.fld{vcntarg})) == true)
  199. else
  200. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  201. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  202. error (vmsg);
  203. end
  204. case 'MaxOrder'
  205. if (mod (vret.(vint.fld{vcntarg}), 1) == 0 && ...
  206. vret.(vint.fld{vcntarg}) > 0 && vret.(vint.fld{vcntarg}) < 6)
  207. else
  208. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  209. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  210. error (vmsg);
  211. end
  212. case 'BDF'
  213. if (strcmp (vret.(vint.fld{vcntarg}), 'on') == true || ...
  214. strcmp (vret.(vint.fld{vcntarg}), 'off') == true)
  215. else
  216. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  217. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  218. error (vmsg);
  219. end
  220. otherwise
  221. vmsg = sprintf ('Unknown parameter "%s" or invalid parameter value "%s"\n', ...
  222. vint.fld{vcntarg}, char (vret.(vint.fld{vcntarg})));
  223. error (vmsg);
  224. end %# switch
  225. end %# for
  226. %# The following line can be uncommented for a even higher level error detection
  227. %# if (vint.len ~= 21)
  228. %# vmsg = sprintf ('Number of fields in structure must match 21');
  229. %# error (vmsg);
  230. %# end
  231. %!test A = odeset ('RelTol', 1e-4); %# odeset calls odepkg_structure_check
  232. %!test A = odeset ('RelTol', [1e-4, 1e-3]); %# after the options have been set
  233. %!error A = odeset ('RelTol', []);
  234. %!error A = odeset ('RelTol', '1e-4');
  235. %!test A = odeset ('AbsTol', 1e-4);
  236. %!test A = odeset ('AbsTol', [1e-4, 1e-3]);
  237. %!error A = odeset ('AbsTol', []);
  238. %!error A = odeset ('AbsTol', '1e-4');
  239. %!test A = odeset ('NormControl', 'on');
  240. %!test A = odeset ('NormControl', 'off');
  241. %!error A = odeset ('NormControl', []);
  242. %!error A = odeset ('NormControl', '12');
  243. %!test A = odeset ('NonNegative', 1);
  244. %!test A = odeset ('NonNegative', [1, 2, 3]);
  245. %!error A = odeset ('NonNegative', []);
  246. %!error A = odeset ('NonNegative', '12');
  247. %!test A = odeset ('OutputFcn', @odeprint);
  248. %!test A = odeset ('OutputFcn', @odeplot);
  249. %!error A = odeset ('OutputFcn', []);
  250. %!error A = odeset ('OutputFcn', 'odeprint');
  251. %!test A = odeset ('OutputSel', 1);
  252. %!test A = odeset ('OutputSel', [1, 2, 3]);
  253. %!error A = odeset ('OutputSel', []);
  254. %!error A = odeset ('OutputSel', '12');
  255. %!test A = odeset ('Refine', 3);
  256. %!error A = odeset ('Refine', [1, 2, 3]);
  257. %!error A = odeset ('Refine', []);
  258. %!error A = odeset ('Refine', 6);
  259. %!test A = odeset ('Stats', 'on');
  260. %!test A = odeset ('Stats', 'off');
  261. %!error A = odeset ('Stats', []);
  262. %!error A = odeset ('Stats', '12');
  263. %!test A = odeset ('InitialStep', 3);
  264. %!error A = odeset ('InitialStep', [1, 2, 3]);
  265. %!error A = odeset ('InitialStep', []);
  266. %!error A = odeset ('InitialStep', 6);
  267. %!test A = odeset ('MaxStep', 3);
  268. %!error A = odeset ('MaxStep', [1, 2, 3]);
  269. %!error A = odeset ('MaxStep', []);
  270. %!error A = odeset ('MaxStep', 6);
  271. %!test A = odeset ('Events', @demo);
  272. %!error A = odeset ('Events', 'off');
  273. %!error A = odeset ('Events', []);
  274. %!error A = odeset ('Events', '12');
  275. %!test A = odeset ('Jacobian', @demo);
  276. %!test A = odeset ('Jacobian', [1, 2; 3, 4]);
  277. %!error A = odeset ('Jacobian', []);
  278. %!error A = odeset ('Jacobian', '12');
  279. %#!test A = odeset ('JPattern', );
  280. %#!test A = odeset ('JPattern', );
  281. %#!error A = odeset ('JPattern', );
  282. %#!error A = odeset ('JPattern', );
  283. %!test A = odeset ('Vectorized', 'on');
  284. %!test A = odeset ('Vectorized', 'off');
  285. %!error A = odeset ('Vectorized', []);
  286. %!error A = odeset ('Vectorized', '12');
  287. %!test A = odeset ('Mass', @demo);
  288. %!test A = odeset ('Mass', [1, 2; 3, 4]);
  289. %!error A = odeset ('Mass', []);
  290. %!error A = odeset ('Mass', '12');
  291. %!test A = odeset ('MStateDependence', 'none');
  292. %!test A = odeset ('MStateDependence', 'weak');
  293. %!test A = odeset ('MStateDependence', 'strong');
  294. %!error A = odeset ('MStateDependence', [1, 2; 3, 4]);
  295. %!error A = odeset ('MStateDependence', []);
  296. %!error A = odeset ('MStateDependence', '12');
  297. %#!test A = odeset ('MvPattern', );
  298. %#!test A = odeset ('MvPattern', );
  299. %#!error A = odeset ('MvPattern', );
  300. %#!error A = odeset ('MvPattern', );
  301. %!test A = odeset ('MassSingular', 'yes');
  302. %!test A = odeset ('MassSingular', 'no');
  303. %!test A = odeset ('MassSingular', 'maybe');
  304. %!error A = odeset ('MassSingular', [1, 2; 3, 4]);
  305. %!error A = odeset ('MassSingular', []);
  306. %!error A = odeset ('MassSingular', '12');
  307. %!test A = odeset ('InitialSlope', [1, 2, 3]);
  308. %!error A = odeset ('InitialSlope', 1);
  309. %!error A = odeset ('InitialSlope', []);
  310. %!error A = odeset ('InitialSlope', '12');
  311. %!test A = odeset ('MaxOrder', 3);
  312. %!error A = odeset ('MaxOrder', 3.5);
  313. %!error A = odeset ('MaxOrder', [1, 2; 3, 4]);
  314. %!error A = odeset ('MaxOrder', []);
  315. %!test A = odeset ('BDF', 'on');
  316. %!test A = odeset ('BDF', 'off');
  317. %!error A = odeset ('BDF', [1, 2; 3, 4]);
  318. %!error A = odeset ('BDF', []);
  319. %!demo
  320. %!
  321. %! odepkg_structure_check (odeset);
  322. %!
  323. %! %----------------------------------------------------------------
  324. %! % Returns the checked odepkg options structure created by odeset.
  325. %!demo
  326. %!
  327. %! A = odeset (); odepkg_structure_check (A);
  328. %!
  329. %! %----------------------------------------------------------------
  330. %! % Create the odepkg options structure A with odeset and check
  331. %! % it with odepkg_structure_check.
  332. %# Local Variables: ***
  333. %# mode: octave ***
  334. %# End: ***