Index: /issm/trunk/src/m/solutions/dakota/qmu.m
===================================================================
--- /issm/trunk/src/m/solutions/dakota/qmu.m	(revision 449)
+++ /issm/trunk/src/m/solutions/dakota/qmu.m	(revision 450)
@@ -7,5 +7,5 @@
 % qmudir =['qmu_' datestr(now,'yyyymmdd_HHMMSS')];
 qmudir ='qmu';
-%  qmufile can not be changed unless cielo_ice_script.sh is
+%  qmufile can not be changed unless cielo_ice_script.sh is also changed
 qmufile='qmu';
 ivar   =1;
@@ -13,4 +13,5 @@
 imethod=1;
 iparams=1;
+runmpi =false;
 
 %  process any extra input arguments
@@ -30,4 +31,15 @@
         case 'iparams'
             iparams=varargin{i+1};
+        case 'overwrite'
+            overwrite=varargin{i+1};
+        case 'outfiles'
+            outfiles =varargin{i+1};
+        case 'rstfile'
+            rstfile  =varargin{i+1};
+        case 'rundakota'
+            rundakota=varargin{i+1};
+        case 'runmpi'
+            runmpi =varargin{i+1};
+            disp(sprintf('runmpi=%d',runmpi));
     end
 end
@@ -35,9 +47,11 @@
 %first create temporary directory in which we will work
 if exist(qmudir,'dir')
-    overwrite=input(['Overwrite existing ''' qmudir ''' directory? Y/N [N]: '], 's');
+    if ~exist('overwrite','var')
+        overwrite=input(['Overwrite existing ''' qmudir ''' directory? Y/N [N]: '], 's');
+    end
     if strncmpi(overwrite,'y',1)
         system(['rm -rf ' qmudir]);
-    else
-        error('Existing ''%s'' directory not overwritten.',qmudir);
+%    else
+%        error('Existing ''%s'' directory not overwritten.',qmudir);
     end
 end
@@ -56,15 +70,67 @@
     md.qmu_params(iparams).analysis_driver=[ISSM_DIR '/src/m/solutions/dakota/cielo_ice_script.sh'];
 end
+dakota_in_data(md.qmu_method(imethod),md.variables(ivar),md.responses(iresp),md.qmu_params(iparams),qmufile,package,md);
 
-dakota_in_data(md.qmu_method(imethod),md.variables(ivar),md.responses(iresp),md.qmu_params(iparams),qmufile,package,md);
-rundak=input(['Run Dakota analysis ''' qmufile '''? Y/N [N]: '], 's');
-if ~strncmpi(rundak,'y',1)
+%  check for existence of results.out files to use
+if exist('results.out.1','file') || exist('results.out.zip','file')
+    if ~exist('outfiles','var')
+        outfiles=input(['Use existing ''results.out'' files? Y/N [N]: '], 's');
+    end
+    if ~strncmpi(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 (~exist('outfiles','var') || ~strncmpi(outfiles,'y',1)) && ...
+    exist('dakota.rst','file')
+    if ~exist('rstfile','var')
+        rstfiles=input(['Use existing ''dakota.rst'' file? Y/N [N]: '], 's');
+    end
+    if strncmpi(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 ~exist('rundakota','var')
+    rundakota=input(['Run Dakota analysis ''' qmufile '''? Y/N [N]: '], 's');
+end
+if ~strncmpi(rundakota,'y',1)
     cd ..
     return
 end
 
-%call dakota
-system(['dakota -i ' qmufile '.in -o ' qmufile '.out -e ' qmufile '.err']);
-% system(['export MPIRUN_NPROCS=8;mpirun -np 4 dakota -i ' qmufile '.in -o ' qmufile '.out -e ' qmufile '.err']);
+if ~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
+
+%  check to see if dakota returned errors in the err file
+fide=fopen([qmufile '.err'],'r');
+fline=fgetl(fide);
+if ischar(fline)
+    while ischar(fline)
+        disp(sprintf('%s',fline));
+        fline=fgetl(fide);
+    end
+    status=fclose(fide);
+    cd ../
+    error(['Dakota returned error in ''' qmufile '.err'' file.  ''' qmudir ''' directory retained.'])
+end
+status=fclose(fide);
 
 %parse inputs and results from dakota
@@ -93,4 +159,5 @@
 %end
 
+%  move all the individual function evalutations into zip files
 system('zip -mq params.in.zip params.in.[1-9]*');
 system('zip -mq results.out.zip results.out.[1-9]*');
