A Cardiovascular Simulator for Research 1.0.0

File: <base>/src/rcvsim.m (10,394 bytes)
% The function rcvsim.m is a wrapper function which enables simulate.m
% -- a human cardiovascular simulator  -- to be executed at the Linux 
% prompt.  This function reads in an input file containing the parameter
% values which characterize the model and its execution and outputs
% waveforms and annotations in MIT format.
%
% Note: This function does not permit the execution of all of the
%       models of simulate.m.  Rather, only the models that have
%       been fully documented can be executed.  Type rcvsim -h at
%	the Linux prompt to find out which models may be implemented.
%	In order to execute the non-documented models, simulate.m 
%	must be run in the MATLAB environment.
%

function rcvsim(parameterfile,outputfile)

if (nargin < 1)

   display(['Usage: rcvsim parameterfile outputfile']);
   display(['       rcvsim -h for help']);

elseif (nargin == 1 & feval('strcmp',parameterfile,'-h') == 1)

   display(['The function rcvsim executes a computational model of the']);
   display(['cardiovascular system.  The model includes the following']);
   display(['components:']);
   display(['1) lumped parameter, pulsatile heart and circulation (intact,']);
   display(['   heart-lung unit, or systemic circulation)']);
   display(['2) short-term regulatory system']);
   display(['   a) arterial baroreflex system']);
   display(['   b) cardiopulmonary baroreflex system']);
   display(['   c) direct neural coupling mechanism between respiration']);
   display(['      and heart rate']);
   display(['3) resting physiologic perturbations']);
   display(['   a) respiration']);
   display(['   b) autoregulation of local vascular beds']);
   display(['      (bandlimited disturbance to systemic arterial resistance)']);
   display(['   c) 1/f disturbance to heart rate']);
   display([' ']);
   display(['Command line arguments:']);
   display([' ']);
   display(['rcvsim parameterfile outputfile']);
   display([' ']);
   display(['where']);
   display([' ']);
   display(['parameterfile - name of working file which contains the current parameter']);
   display(['                values characterizing the model (must be in current directory)']);
   display(['   outputfile - prefix of the output files generated by the model']);
   display([' ']);
   display(['or']);
   display([' ']);
   display(['rcvsim -h for help']);
   display(' '); 
   display(['Output files:']);
   display([' ']);
   display(['   outputfile.dat - binary file (MIT format -- shorts) containing all']);
   display(['                    generated waveforms']);
   display(['   outputfile.qrs - qrs annotations file (MIT format)']);
   display(['   outputfile.hea - header file (MIT format) describing the contents']);
   display(['                    of outputfile.dat']);
   display(['   outputfile.txt - ascii, multi-column file representing either']);
   display(['                    cardiac function or venous return curves']);
   display(['parameterfile.num - parameter file characterizing execution;']);
   display(['                    created after each parameter update beginning with']);
   display(['                    the initial choice of values (num = 0);']);
   display(['                    see aux information in outputfile.qrs for']);
   display(['                    for time in which this file is created']);
   display([' ']);
   display(['During on-line viewing,']);
   display([' ']);
   display(['Press p and RETURN at standard input to pause']);
   display([' ']);
   display(['Once paused, the following actions may be carried out:']);
   display(['1) scrolling backwards with arrow buttons']);
   display(['2) plotting waveforms against each other by clicking on File']);
   display(['   (with the right mouse), then, Analyze..., and then VCG']);
   display(['   (the first two waveforms in Signal List will be plotted']);
   display(['   against each other)']);
   display(['3) update parameters in parameterfile and save parameterfile']);
   display([' ']);
   display(['Press r and RETURN at standard input to resume']);

elseif (nargin < 2)

   display(['Usage: rcvsim parameterfile outputfile']);
   display(['       rcvsim -h for help']);

elseif (nargin > 3)

   display(['Usage: rcvsim parameterfile outputfile']);
   display(['       rcvsim -h for help']);

else

	% Checking for existence of parameter file.  If file does not
	% exist, then exit program. 
	fidp = fopen(parameterfile,'r');
	y = (fidp == -1);	
	mbintscalar(y);
	if (y)
	   display(['Invalid parameter file name.']);
	else   

	       % Reading initial choice of parameters from working
	       % parameter file.
	       [th,signals] = read_param(fidp);
	       fclose(fidp);

	       % Writing initial choice of parameters to file named
	       % parameterfile.0.
	       write_param(parameterfile,'0');

	       % Checking that all parameter values are properly
	       % assigned to th parameter vector.  If not, then
	       % exit the program.
	       if (length(th) == 1)
		  display(['All parameter values in the file ' parameterfile ' not properly assigned.']);
	       else	

			% Assigning status parameters.  
			preparation = th(107);
			breathing = th(108);
			dncm = th(109);
			baro = th(110);
			dra = th(111);
			if (dra == 1)
			   dra = 2;
			end
			df = th(112);
			flag = [preparation; breathing; dncm; baro; dra; 0; df; 0];

			% Writing header file.  Note that the checksum
			% values are not assigned properly.
			fid1 = fopen([outputfile '.hea'],'w');
			fprintf(fid1,'%s %d %8.2f %d\n',outputfile,29,th(113),th(106));
			
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/mmHg',16,0,0,0,0,'Pl');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/mmHg',16,0,0,0,0,'Pa');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/mmHg',16,0,0,0,0,'Pv');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/mmHg',16,0,0,0,0,'Pr');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/mmHg',16,0,0,0,0,'Ppa');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/mmHg',16,0,0,0,0,'Ppv');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/mmHg',16,0,0,0,0,'Pth');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/mmHg',16,0,0,0,0,'Palv');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/mmHg',16,0,0,0,0,'Pra');

			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml',16,0,0,0,0,'Ql');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml',16,0,0,0,0,'Qa');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml',16,0,0,0,0,'Qv');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml',16,0,0,0,0,'Qr');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml',16,0,0,0,0,'Qpa');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml',16,0,0,0,0,'Qpv');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,1,'/ml',16,0,0,0,0,'Qlu');

			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml/s',16,0,0,0,0,'qpv');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml/s',16,0,0,0,0,'ql');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml/s',16,0,0,0,0,'qa');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml/s',16,0,0,0,0,'qv');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml/s',16,0,0,0,0,'qr');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml/s',16,0,0,0,0,'qpa');

			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,1000,'/ml/mmHg',16,0,0,0,0,'Cls');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,1000,'/ml/mmHg',16,0,0,0,0,'Crs');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,10,'/ml',16,0,0,0,0,'Qvo');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,1000,'/mmHg-s/ml',16,0,0,0,0,'Ra');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,100,'/bpm',16,0,0,0,0,'F');

			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,100,'/mmHg/ml',16,0,0,0,0,'El');
			fprintf(fid1,'%s %d %8.2f%s %d %d %d %d %d %s\n',[outputfile '.dat'],16,100,'/mmHg/ml',16,0,0,0,0,'Er');
			fclose(fid1);

			% Generating the waveforms by calling simulate.m.
			[P,v,q,ap,ve,qrs,t,num] = simulate(flag,th,outputfile,parameterfile,signals);

			% Writing waveforms and annotations to MIT format files
			% if they are not being viewed on-line.
		        if (strcmp(signals,'-1') ~= 0)

			   ap = ap(1:5,:);
			   X = [P; v(1:6,:); v(7,:)*0.1; q; ap(1:2,:)*100; ap(3,:); ap(4,:)*100; ap(5,:)*600; ve*10]*10;
			   fid = fopen([outputfile '.dat'],'w');
			   fwrite(fid,X,'short');
			   fclose(fid);
			   fid2 = fopen([outputfile '.qrs'],'wa');
			   fwrite(fid2,1,'bit10');
			   fwrite(fid2,1,'ubit6');
			   fwrite(fid2,-1,'bit10');
			   fwrite(fid2,62,'ubit6');
			   Nbytes = length(parameterfile)+2;
			   fwrite(fid2,Nbytes,'bit10');
			   fwrite(fid2,63,'ubit6');
			   fwrite(fid2,[parameterfile '.' num2str(0)],[num2str(Nbytes) '*uchar']);

			   qrso = diff(qrs');
			   for i = 1:length(qrso)
			       fwrite(fid2,qrso(i),'bit10');
			       fwrite(fid2,1,'ubit6');
			   end
			   fwrite(fid2,0,'bit10');
			   fwrite(fid2,0,'ubit6');
			   fclose(fid2);

			end
			
			% Writing cardiac function or venous return curve
			% numerics to ascii multi-column file, if necessary.
			if (preparation == 1 | preparation == 2)

			   fid3 = fopen([outputfile '.txt'],'w');
			   if (preparation == 2)
			      fprintf(fid3,'%8s %8s\n','mPra', 'mqv');
			      fprintf(fid3,'%8.2f %8.2f\n',[num(1,:); num(2,:)*(60/1000)]);
			   elseif (preparation == 1)
			      fprintf(fid3,'%8s %8s %8s\n','mPra','mql','mPa');
			      fprintf(fid3,'%8.2f %8.2f %8.2f\n',[num(1,:); num(3,:)*(60/1000); num(2,:)]);
			   end
			   fclose(fid3);
			   if (th(68) ~= -1)
			      %#function plot_cfvr
			      plot_cfvr(outputfile,th(68));
			   end
	          end    

	       end

	end

end