Index: /issm/trunk/src/m/solutions/dakota/process_qmu_options.m
===================================================================
--- /issm/trunk/src/m/solutions/dakota/process_qmu_options.m	(revision 780)
+++ /issm/trunk/src/m/solutions/dakota/process_qmu_options.m	(revision 780)
@@ -0,0 +1,135 @@
+function outoptions=process_qmu_options(options)
+%PROCESS_QMU_OPTIONS - set up default options for qmu phase
+%
+%   Usage:
+%      options=process_qmu_options(options)
+%
+%   See also: QMU,RECOVER_QMU_OPTIONS
+
+%analysis_type: check on this option, error out otherwise
+found=0;
+for i=1:size(options,1),
+	if strcmpi(options{i,1},'analysis_type'),
+		analysis_type=options{i,2};
+		found=1;
+	end
+end
+if ~found,
+	error('recover_qmu_options error message: no ''analysis_type'' was provided');
+end
+
+%package: is there one? default to ''cielo''
+found=0;
+for i=1:size(options,1),
+	if strcmpi(options{i,1},'package'),
+		package=options{i,2};
+		found=1;
+	end
+end
+if ~found,
+	disp('recover_qmu_options info message: no ''package'' was provided, defaulting to ''cielo''');
+	options(end+1,:)={'package' 'cielo'};
+	package='cielo';
+end
+
+if ~ischar(package), 
+	error(['process_qmu_options error message: package ' package ' not supported yet']);
+end
+
+%sub_analysis_type: check on it, not mandatory
+found=0;
+for i=1:size(options,1),
+	if strcmpi(options{i,1},'sub_analysis_type'),
+		sub_analysis_type=options{i,2};
+		found=1;
+	end
+end
+if ~found
+	if ~strcmpi(analysis_type,'thermal'),
+		sub_analysis_type='none';
+	else
+		disp('recover_qmu_options info message: no ''sub_analysis_type'' was provided, defaulting to ''steady''');
+		sub_analysis_type='steady';
+	end
+end
+
+%check solution type is supported
+if ~(strcmpi(analysis_type,'control') |  ...
+		strcmpi(analysis_type,'diagnostic') |  ...
+		strcmpi(analysis_type,'prognostic') |  ...
+		strcmpi(analysis_type,'thermal') |  ...
+		strcmpi(analysis_type,'parameters') |  ...
+		strcmpi(analysis_type,'mesh') |  ...
+		strcmpi(analysis_type,'mesh2grid') |  ...
+		strcmpi(analysis_type,'transient') ),
+	error(['process_qmu_options error message: analysis_type ' analysis_type ' not supported yet!']);
+end
+if ~(strcmpi(sub_analysis_type,'none') |  ...
+		strcmpi(sub_analysis_type,'steady') |  ...
+		strcmpi(sub_analysis_type,'horiz') |  ...
+		strcmpi(sub_analysis_type,'vert') |  ...
+		strcmpi(sub_analysis_type,'') |  ...
+		strcmpi(sub_analysis_type,'transient') ),
+	error(['process_qmu_options error message: sub_analysis_type ' sub_analysis_type ' not supported yet!']);
+end
+if ~(strcmpi(package,'cielo') |  ...
+		strcmpi(package,'ice') |  ...
+		strcmpi(package,'macayeal') ),
+	error(['process_qmu_options error message: package ' package ' not supported yet!']);
+end
+
+
+
+
+%  process qmu arguments
+
+%first, the defaults
+qmudir ='qmu';% qmudir =['qmu_' datestr(now,'yyyymmdd_HHMMSS')];
+qmufile='qmu';%  qmufile can not be changed unless cielo_ice_script.sh is also changed
+ivar   =1;
+iresp  =1;
+imethod=1;
+iparams=1;
+runmpi =false;
+
+for i=1:size(options,1),
+	switch options{i,1},
+	case 'qmudir'
+		qmudir=options{i,2};
+	case 'qmufile'
+		qmufile=options{i,2};
+	case 'ivar'
+		ivar=options{i,2};
+	case 'iresp'
+		iresp=options{i,2};
+	case 'imethod'
+		imethod=options{i,2};
+	case 'iparams'
+		iparams=options{i,2};
+	case 'overwrite'
+		outoptions.overwrite=options{i,2};
+	case 'outfiles'
+		outoptions.outfiles=options{i,2};
+	case 'rstfile'
+		outoptions.rstfile=options{i,2}; 
+	case 'rundakota'
+		outoptions.rundakota=options{i,2};
+	case 'runmpi'
+		runmpi=options{i,2};
+	otherwise
+		%nothing
+	end
+end
+
+%setup final options structure
+outoptions.analysis_type=analysis_type;
+outoptions.package=package;
+outoptions.sub_analysis_type=sub_analysis_type;
+outoptions.qmudir=qmudir;
+outoptions.qmufile=qmufile;
+outoptions.ivar=ivar;
+outoptions.iresp=iresp;
+outoptions.imethod=imethod;
+outoptions.iparams=iparams;
+outoptions.runmpi=runmpi;
+
Index: /issm/trunk/src/m/solutions/dakota/qmu.m
===================================================================
--- /issm/trunk/src/m/solutions/dakota/qmu.m	(revision 780)
+++ /issm/trunk/src/m/solutions/dakota/qmu.m	(revision 780)
@@ -0,0 +1,204 @@
+function md=qmu(md,varargin)
+%QMU - apply Quantification of Margins and Uncertainties techniques 
+%      to a solution sequence (like diagnostic.m, progonstic.m, etc ...), 
+%      using the Dakota software from Sandia.
+%   Usage:
+%      md=qmu(md,varargin)
+%      where varargin is a lit of paired arguments. 
+%      arguments can be: 'analysis_type': 'diagnostic','thermal','prognostic','transient'
+%      arguments can be: 'sub_analysis_type': 'transient','steady','' (default if empty = 'steady')
+%      arguments can be: 'package': 'macayeal','ice','cielo' (default if not specified = 'cielo')
+%
+%
+%   Examples:
+%      md=qmu(md,'analysis_type','diagnostic','package','cielo');
+%      md=qmu(md,'analysis_type','control','package','ice');
+%      md=qmu(md,'analysis_type','thermal','sub_analysis_type','transient','package','ice');
+%      md=qmu(md,'analysis_type','thermal','sub_analysis_type','steady','package','cielo');
+%      md=qmu(md,'analysis_type','thermal','package','cielo');
+%
+%   On top of these solution arguments (found also in solve.m), user can specify QMU 
+%   specific arguments: 
+%
+%       qmudir:  any directory where to run the qmu analysis
+%       qmufile: input file for Dakota
+%       ivar: selection number for variables input (if several are specified in variables)
+%       iresp: same thing for response functions
+%       imethod: same thing for methods
+%       iparams: same thing for params
+%       overwrite: overwrite qmudir
+%       outfiles: (John?)
+%       rstfile: backup file name
+%       rundakota: (John?)
+%       runmpi: (John?)
+
+%some checks on list of arguments
+global ISSM_DIR;
+
+%recover options
+options=recover_qmu_options(md,varargin{:});
+
+%add default options
+options=process_qmu_options(options);
+
+%recover some fields
+md.analysis_type=options.analysis_type;
+md.sub_analysis_type=options.sub_analysis_type;
+package=options.package;
+
+%Use package to set solution namespace
+usenamespace(package);
+
+if ~ismodelselfconsistent(md,package),
+	error(' '); %previous error messages should explain what is going on.
+end
+
+displaystring(md.debug,'\n%s\n','launching solution sequence');
+
+
+%first create temporary directory in which we will work
+if exist(options.qmudir,'dir')
+    if ~isfield(options,'overwrite')
+        options.overwrite=input(['Overwrite existing ''' options.qmudir ''' directory? Y/N [N]: '], 's');
+    end
+    if strncmpi(options.overwrite,'y',1)
+        system(['rm -rf ' options.qmudir]);
+%    else
+%        error('Existing ''%s'' directory not overwritten.',qmudir);
+    end
+end
+mkdir(options.qmudir)
+cd(options.qmudir)
+
+%some data needs to be copied in qmudir when not running in library mode
+if ~md.dakotaplugin,
+	system('cp $ISSM_DIR/startup.m .');
+
+	%save our model in qmu so that it can be repeatedly used by Dakota.
+	save('Qmu.model','md')
+end
+
+%when running in library mode, the in file needs to be called md.name.qmu.in
+if md.dakotaplugin,
+	options.qmufile=[md.name ];
+end
+
+%create m and in files for dakota
+if (~isfield(md.qmu_params(options.iparams),'direct') || ...
+    ~md.qmu_params(options.iparams).direct) && ...
+   (~isfield(md.qmu_params(options.iparams),'analysis_driver') || ...
+    isempty(md.qmu_params(options.iparams).analysis_driver))
+    md.qmu_params(options.iparams).analysis_driver=[ISSM_DIR '/src/m/solutions/dakota/cielo_ice_script.sh'];
+end
+
+%create m and in files for dakota
+dakota_in_data(md.qmu_method(options.imethod),md.variables(options.ivar),md.responses(options.iresp),md.qmu_params(options.iparams),options.qmufile,package,md);
+
+
+%in library mode, we only need the dakota in file
+if md.dakotaplugin,
+	system(['rm -rf ' md.name '.m']);
+end
+
+%Dakota can be run in library mode in parallel, or using the 
+%standard Dakota (scripted or direct) interface. Those two 
+%modes of running are very different, and call for different 
+%solution sequences. We branch here. 
+
+if md.dakotaplugin,
+
+	%Check we are running in parallel.
+	if strcmpi(md.cluster,'none'),
+		error('qmu error message: cannot run in library mode serially! set dakotaplugin to 0');
+	end
+
+	%Get cluster.rc location
+	cluster_rc_location=which('cluster.rc');
+
+	%let solution sequences know that we are running in qmu mode
+	md.qmu_analysis=1;
+
+	%Figure out parameters for this particular cluster
+	[codepath,executionpath]=ClusterParameters(md.cluster,cluster_rc_location);
+
+	%Marshall model data into a binary file.
+	marshall(md);
+
+	%add qmu fields to binary file
+	qmumarshall(md,md.variables(options.ivar),md.responses(options.iresp));
+
+	%Now, we need to build the queuing script, used by the cluster to launch the job.
+	BuildQueueingScript(md,executionpath,codepath);
+
+	%Now, launch the queueing script
+	LaunchQueueJob(md,executionpath);
+
+	%Do we return, or just wait for results?
+	if md.waitonlock,
+		%we wait for the done file
+		waitonlock([executionpath '/' md.name '.lock']);
+		%load results
+		md=loadresultsfromcluster(md);
+	else
+		return;
+	end
+	
+else
+%  check for existence of results.out files to use
+	if exist('results.out.1','file') || exist('results.out.zip','file')
+		if ~isfield(options,'outfiles')
+			options.outfiles=input(['Use existing ''results.out'' files? Y/N [N]: '], 's');
+		end
+		if ~strncmpi(options.outfiles,'y',1)
+			system('rm -f results.out.[1-9]*');
+		else
+			if exist('results.out.zip','file') && ~exist('results.out.1','file')
+				display('Inflating ''results.out.zip'' file.');
+				system('unzip -q results.out.zip');
+			end
+		end
+	end
+
+	%  check for existence of dakota.rst file to use
+	rstflag='';
+	if (~isfield(options,'outfiles') || ~strncmpi(options.outfiles,'y',1)) && ...
+		exist('dakota.rst','file')
+		if ~isfield(options,'rstfile')
+			options.rstfiles=input(['Use existing ''dakota.rst'' file? Y/N [N]: '], 's');
+		end
+		if strncmpi(options.rstfiles,'y',1)
+			system('rm -f results.out.[1-9]*');
+			system('dakota_restart_util print dakota.rst | grep completed');
+			rstflag=' -read_restart dakota.rst';
+		end
+	end
+
+	%call dakota
+	if ~isfield(options,'rundakota')
+		options.rundakota=input(['Run Dakota analysis ''' qmufile '''? Y/N [N]: '], 's');
+	end
+	if ~strncmpi(options.rundakota,'y',1)
+		cd ..
+		return
+	end
+
+	if ~options.runmpi
+		system(['dakota -i ' qmufile '.in -o ' qmufile '.out -e ' qmufile '.err' rstflag]);
+	else
+	%  use 'mpd --ncpus=8 &' to initialize mpi and 'mpdringtest' to verify.
+	%  exporting MPIRUN_NPROCS sets mpi in dakota.
+	%    system('mpd --ncpus=8 &');
+	%    system('mpdringtest');
+		system(['export MPIRUN_NPROCS=8;mpirun -np 4 dakota -i ' qmufile '.in -o ' qmufile '.out -e ' qmufile '.err' rstflag]);
+	end
+
+end
+
+
+
+%Now load the results from dakota output files.
+qmuoutput(md,options.qmufile,options.qmudir);
+
+%Get out of qmu directory
+cd ..
+
Index: /issm/trunk/src/m/solutions/dakota/qmumarshall.m
===================================================================
--- /issm/trunk/src/m/solutions/dakota/qmumarshall.m	(revision 780)
+++ /issm/trunk/src/m/solutions/dakota/qmumarshall.m	(revision 780)
@@ -0,0 +1,85 @@
+function qmumarshall(md,variables,responses)
+%QMUMARSHALL - output ISSM compatible binary file with qmu fields. This is 
+%   in addition to the marshall routine for regular solve routines.
+%   Usage:
+%      qmumarshall(md,variables,responses)
+% 
+%   where variables and responses are the Dakota variables and responses found in the model @md.
+
+%some checks on list of arguments
+if ((nargin~=3) & (nargout~=0))
+	qmumarshallusage;
+	error('marshall error message');
+end
+
+disp(['qmu marshalling file ' md.name '.bin']);
+
+%open file for binary adding 
+fid=fopen([ md.name '.bin'],'ab');
+if fid==-1,
+	error(['qmumarshall error message: could not open ' [md.name '.bin'],' file for binary adding']);
+end
+
+%count how many variables we have
+numvariables=0;
+variable_fieldnames=fieldnames(variables);
+for i=1:length(variable_fieldnames),
+	field_name=variable_fieldnames{i};
+	fieldvariables=variables.(field_name);
+	numvariables=numvariables+numel(variables.(field_name));
+end
+%write number of variables to disk
+WriteData(fid,numvariables,'Integer','numberofvariables');
+
+%now, for each variable, write descriptor
+count=0;
+for i=1:length(variable_fieldnames),
+	field_name=variable_fieldnames{i};
+	fieldvariables=variables.(field_name);
+	for j=1:length(fieldvariables),
+		descriptor=fieldvariables(j).descriptor;
+		WriteData(fid,descriptor,'String',['variabledescriptor' num2str(count)]);
+		count=count+1;
+	end
+end
+
+%deal with responses
+
+%count how many responses we have
+numresponses=0;
+response_fieldnames=fieldnames(responses);
+for i=1:length(response_fieldnames),
+	field_name=response_fieldnames{i};
+	fieldresponses=responses.(field_name);
+	numresponses=numresponses+numel(responses.(field_name));
+end
+%write number of responses to disk
+WriteData(fid,numresponses,'Integer','numberofresponses');
+
+%now, for each response, write descriptor
+count=0;
+for i=1:length(response_fieldnames),
+	field_name=response_fieldnames{i};
+	fieldresponses=responses.(field_name);
+	for j=1:length(fieldresponses),
+		descriptor=fieldresponses(j).descriptor;
+		WriteData(fid,descriptor,'String',['responsedescriptor' num2str(count)]);
+		count=count+1;
+	end
+end
+
+%write npart to disk
+WriteData(fid,md.npart,'Integer','npart');
+	
+%close file
+st=fclose(fid);
+if st==-1,
+	error(['qmumarshall error message: could not close file ' [md.name '.bin']]);
+end
+
+end
+
+function qmumarshallusage();
+disp(' ');
+disp('function qmumarshall(md,variables,responses)');
+end
Index: /issm/trunk/src/m/solutions/dakota/qmuserial.m
===================================================================
--- /issm/trunk/src/m/solutions/dakota/qmuserial.m	(revision 779)
+++ /issm/trunk/src/m/solutions/dakota/qmuserial.m	(revision 780)
@@ -14,4 +14,5 @@
 iparams=1;
 runmpi =false;
+
 
 %  process any extra input arguments
Index: /issm/trunk/src/m/solutions/dakota/recover_qmu_options.m
===================================================================
--- /issm/trunk/src/m/solutions/dakota/recover_qmu_options.m	(revision 780)
+++ /issm/trunk/src/m/solutions/dakota/recover_qmu_options.m	(revision 780)
@@ -0,0 +1,22 @@
+function options=recover_qmu_options(md,varargin)
+%RECOVER_SOLVE_OPTIONS - recover solution options for qmu runs.
+%
+%   Usage:
+%      options=recover_qmu_options(md,varargin);
+%
+%   See also: SOLVE
+
+%initialize options.
+options=cell(0,2);
+
+%make sure length(varargin) is even, ie options come in pairs.
+if mod(length(varargin),2),
+	error('recover_qmu_options error message: an even number of options is necessary');
+end
+
+%go through varargin, extract options 
+for i=1:length(varargin)/2,
+
+	options(end+1,:)={varargin{2*i-1} varargin{2*i}};
+
+end
