-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodewriter.m
187 lines (156 loc) · 4.17 KB
/
odewriter.m
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
function odewriter(src, destfilename, options)
arguments
src
destfilename=[]
options.sortmethod='none'
options.do_ranges=false
end
% read ode write new ode (with options for sorting pars/vars/fixed/etc)
%parser already re-orders fixed quantities so they are always defined
%before use.
% sortmethod='alpha';
% sortmethod='none'; %xpp is sensitive to order of equations...
% input checking
if nargin==0 || isempty(src)
[name,path]=uigetfile('.ode','Select an ODE file');
src=fullfile(path,name);
end
% Extract info from the ODE file
if isstruct(src)
xppdata=src;
else
xppdata=parseODEfile(src);
end
%Build the destination filename
if isempty(destfilename)
destfilename=uiputfile('*.ode','New file name');
end
output_file=BuildOutputFile(xppdata,options.sortmethod, options.do_ranges);
lineCount=length(output_file);
%delete a previous version - silently destroys any previous version!
fullPath=fullfile(pwd,destfilename+".ode");
if exist(fullPath,'file')==2
delete(fullPath)
end
% write to file
fidw=fopen(fullPath,'w','n','UTF-8');
if fidw > -1
for line=1:lineCount
fprintf(fidw, '%s\n', output_file{line});
end
status=fclose(fidw);
else
fprintf(' Problem opening file %s for writing. Cannot continue\n',fullPath);
return
end
%check to make sure file is written
ready=false;
while ~ready
status=exist(fullPath,'file');
if status
ready=true;
end
end
success = true;
return
end
function output_file=BuildOutputFile(xppdata,sortmethod, do_ranges)
% build the output as a cell array containing each line.
par=xppdata.par;
num=xppdata.num;
var=xppdata.var;
fixed=xppdata.fixed;
func=xppdata.func;
aux=xppdata.aux;
opt=xppdata.opt.allopt;
output_file={};
output_file{end+1}="# "+xppdata.name;
output_file{end+1}='';
%constants
if xppdata.nNum>0
output_file{end+1}='#Constants';
num=sortNames(num,sortmethod);
for i=1:xppdata.nNum
output_file{end+1}=['num ' num(i).name '=' num2str(num(i).value)];
end
end
%parameters
if xppdata.nPar>0
output_file{end+1}='';
output_file{end+1}='#Parameters';
par=sortNames(par,sortmethod);
for i=1:xppdata.nPar
thisline = ['par ' par(i).name '=' num2str(par(i).value)];
if do_ranges
thisline=[thisline,'[',num2str(par(i).lb),',',num2str(par(i).ub),']'];
end
output_file{end+1}=thisline;
end
end
%Functions
if xppdata.nFunc>0
output_file{end+1}='';
output_file{end+1}='#Functions';
func=sortNames(func,sortmethod);
for i=1:xppdata.nFunc
output_file{end+1}=[func(i).name '(' func(i).full_arglist ')=' func(i).formula];
end
end
%Expressions
if xppdata.nFixed>0
output_file{end+1}='';
output_file{end+1}='#Fixed expressions';
fixed=sortNames(fixed,sortmethod);
for i=1:xppdata.nFixed
output_file{end+1}=[fixed(i).name '=' fixed(i).formula];
end
end
if xppdata.nVar>0
var=sortNames(var,sortmethod);
%Initial conditions
output_file{end+1}='';
output_file{end+1}='#Initial conditions';
for i=1:xppdata.nVar
thisline = ['init ' var(i).name '=' num2str(var(i).value)];
if do_ranges
thisline=[thisline,'[',num2str(var(i).lb),',',num2str(var(i).ub),']'];
end
output_file{end+1}=thisline;
end
%State Variables
output_file{end+1}='';
output_file{end+1}='#Differential equations';
for i=1:xppdata.nVar
output_file{end+1}=[var(i).name '''=' var(i).formula];
end
end
%User Functions
if xppdata.nAux>0
output_file{end+1}='';
output_file{end+1}='#User functions';
aux=sortNames(aux,sortmethod);
for i=1:xppdata.nAux
output_file{end+1}=['aux ' aux(i).name '=' aux(i).formula];
end
end
%options
if xppdata.nOpt>0
output_file{end+1}='';
output_file{end+1}='#Options';
opt=sortNames(opt,sortmethod);
for i=1:xppdata.nOpt
output_file{end+1}=['@ ' opt(i).name '=' opt(i).value];
end
end
output_file{end+1}='';
output_file{end+1}='done';
end
function mystruct=sortNames(mystruct,sortmethod)
switch sortmethod
case 'alpha'
[~,ixs]=natsort({mystruct(:).name});
otherwise
ixs=1:numel(mystruct);
end
mystruct=mystruct(ixs);
end