function tolerance=testsgettolerance(md,sequence,field),
%TESTSGETTOLERANCE - give the tolerance for the nightly run 
%
%   This routine is used by nightly runs (runme.m and updatearchive.m).
%   It gives the accepted tolerance, given the package, the analysis_type and the field check.
%
%   Usage:
%      tolerance=testsgettolerance2(md,package,analysis_type,field),
%
%   Example:
%      tolerance=testsgettolerance2(md,'cielo','diagnostic','vel'),
%
%   See also: TESTSGETANALYSIS, TESTGETPACKAGE

%recover what is being requested.
analysis_type=sequence{1};
sub_analysis_type=sequence{2};
qmu=sequence{3};
control=sequence{4};
control_fit=sequence{5};
parallel=sequence{6};

if ~parallel,
	tolerance=10^-12;

else
	if strcmpi(analysis_type,'prognostic'),
		tolerance=10^-14;

	elseif strcmpi(analysis_type,'transient'),
		if strcmpi(md.type,'2d'),
			tolerance=10^-11;
		else
			if md.isstokes,
				tolerance=10^-4;
			elseif any(md.gridonpattyn),
				tolerance=10^-6;
			else,
				tolerance=10^-12;
			end
		end

	elseif strcmpi(analysis_type,'diagnostic'),
		if md.isstokes,
			tolerance=10^-4;
		elseif any(md.gridonpattyn),
			tolerance=10^-6;
		elseif ~isnans(md.rifts),
			tolerance=10^-10;
		else,
			tolerance=10^-13;
		end

	elseif strcmpi(analysis_type,'thermal'),
		if strncmpi(fliplr(field),fliplr('melting'),7),
			tolerance=10^-6;
		elseif strncmpi(fliplr(field),fliplr('temperature'),11),
			tolerance=10^-11;
		end

	elseif (strcmpi(analysis_type,'steadystate')),
			if md.isstokes,
				tolerance=5*10^-5;
			elseif any(md.gridonpattyn),
				tolerance=2*10^-8;
			else,
				tolerance=10^-10;
			end

	else
		error(['testsgettolerance2 error message: analysis_type '  analysis_type ' not supported yet with package cielo_parallel']);
	end
end
