92 lines
3.0 KiB
Matlab

function obj = run(obj)
%RUN Run PricklyPear model simulation
% run(PP)
% Variables
cdir = pwd();
% Read the base hoc file
fname = fullfile(obj.modeldir(), sprintf('msomodel_%s.hoc', obj.model_name));
fid = fopen(fname);
txt = textscan(fid, '%s', 'delimiter', '\n');
txt = txt{1};
fclose(fid);
% Replace the main placeholder
phLine = find(strcmp('//%%%TEMPLATE%%%', txt));
props = properties(obj);
phNew = cell(numel(props), 1);
for ii = 1:numel(props)
if any(strcmp(obj.template_exclude, props{ii}))
continue;
end
phNew{ii} = sprintf('%s = %g', props{ii}, obj.(props{ii}));
end
phNew = [{'// PricklyPear parameters'};...
phNew;...
{'// If you see this file, something went wrong during pricklypear.run(). Debug and remove this file'}];
txt = [txt(1:phLine-1); phNew; txt(phLine+1:end)];
% Replace the ending placeholder
phLine = strcmp('//%%%TEMPLATE_ENDING%%%', txt);
txt{phLine} = 'quit()';
% Write the temporary hoc file
fname = fullfile(obj.modeldir(), 'msomodel__temp.hoc');
fid = fopen(fname, 'w');
fprintf(fid, '%s\n', txt{:});
fclose(fid);
% Write the input event times
fname = fullfile(obj.modeldir(), 'eventtimes_contra.txt');
fid = fopen(fname, 'w');
fprintf(fid, '%i\r\n', obj.eventtimes_contra);
fclose(fid);
fname = fullfile(obj.modeldir(), 'eventtimes_ipsi.txt');
fid = fopen(fname, 'w');
fprintf(fid, '%i\n', obj.eventtimes_ipsi);
fclose(fid);
% Remove existing data files
if exist(obj, 'directMode', true)
isDeleted = delete(obj, 'directMode', true);
end
parobj = parent(obj);
if exist(child(parobj, 0))
isDeleted = delete(child(parobj, 0));
end
% Execute
cd(obj.modeldir());
system('neuron -nogui msomodel__temp.hoc');
cd(cdir);
% Wait for simulation to finish
while ~exist(obj, 'directMode', true, 'full', true)
pause(4);
end
% Move data
if ~isempty(obj.sim_experiment)
[unused unused] = mkdir(obj.datadir());
for ii = 1:numel(obj.sim_channels)
movefile(fullfile(obj.directdir(), sprintf('run_%05i__vm_%s.txt', obj.iter, obj.sim_channels{ii})),...
fullfile(obj.datadir(), sprintf('run_unsaved__vm_%s.txt', obj.sim_channels{ii})));
end
end
copyfile(fullfile(obj.modeldir(), sprintf('eventtimes_contra.txt')),...
fullfile(obj.datadir(), sprintf('run_unsaved__evt_contra.txt')));
copyfile(fullfile(obj.modeldir(), sprintf('eventtimes_ipsi.txt')),...
fullfile(obj.datadir(), sprintf('run_unsaved__evt_ipsi.txt')));
props = struct(obj);
save(fullfile(obj.datadir(), 'run_unsaved__properties.ppbin'), '-struct', 'props');
% Clean up
delete(fullfile(obj.modeldir(), 'msomodel__temp.hoc'));
delete(fullfile(obj.modeldir(), 'eventtimes_contra.txt'));
delete(fullfile(obj.modeldir(), 'eventtimes_ipsi.txt'));
end