Index: /issm/trunk/src/m/solutions/AdjointCorePointerFromSolutionEnum.m
===================================================================
--- /issm/trunk/src/m/solutions/AdjointCorePointerFromSolutionEnum.m	(revision 5377)
+++ /issm/trunk/src/m/solutions/AdjointCorePointerFromSolutionEnum.m	(revision 5377)
@@ -0,0 +1,14 @@
+function [adjointcore]=AdjointCorePointerFromSolutionEnum(solutiontype),
+%COREPOINTERFROMSOLUTIONENUM - returns solution_core function
+%
+%   Usage:
+%      [adjointcore]=AdjointCorePointerFromSolutionEnum(solutiontype);
+
+switch solutiontype,
+
+	case DiagnosticSolutionEnum,         adjointcore='adjointdiagnostic_core';
+	case SteadystateSolutionEnum,        adjointcore='adjointdiagnostic_core';
+	case BalancedthicknessSolutionEnum,  adjointcore='adjointbalancedthickness_core';
+	otherwise error('%s%s%s',' solution type: ',EnumToString(solutiontype),' not supported yet!');
+
+end
Index: /issm/trunk/src/m/solutions/SetCurrentConfiguration.m
===================================================================
--- /issm/trunk/src/m/solutions/SetCurrentConfiguration.m	(revision 5376)
+++ /issm/trunk/src/m/solutions/SetCurrentConfiguration.m	(revision 5377)
@@ -34,5 +34,5 @@
 
 	if found==-1,
-		error('SetCurrentConfiguration error message: could not find analysis_type in list of FemModel analyses');
+		error(['SetCurrentConfiguration error message: could not find analysis_type ' EnumToString(configuration_enum) ' in list of FemModel analyses']);
 	end
 	
Index: sm/trunk/src/m/solutions/adjoint_core.m
===================================================================
--- /issm/trunk/src/m/solutions/adjoint_core.m	(revision 5376)
+++ 	(revision )
@@ -1,46 +1,0 @@
-function femmodel=adjoint_core(femmodel),
-%ADJOINT_CORE - compute inverse method adjoint state
-%
-%   Usage:
-%      femmodel=adjoint_core(femmodel)
-
-	%recover parameters common to all solutions
-	verbose=femmodel.parameters.Verbose;
-	isstokes=femmodel.parameters.IsStokes;
-	dim=femmodel.parameters.Dim;
-	control_analysis=femmodel.parameters.ControlAnalysis;
-	solution_type=femmodel.parameters.SolutionType;
-	conserve_loads=true;
-
-	%set analysis type to compute velocity:
-	if (solution_type==DiagnosticSolutionEnum | solution_type==SteadystateSolutionEnum)
-		displaystring('\n%s',['      computing velocities']);
-		if(isstokes), femmodel=SetCurrentConfiguration(femmodel,DiagnosticStokesAnalysisEnum);
-		else femmodel=SetCurrentConfiguration(femmodel,DiagnosticHorizAnalysisEnum); end
-		femmodel=solver_diagnostic_nonlinear(femmodel,conserve_loads);
-	elseif (solution_type==BalancedthicknessSolutionEnum)
-		femmodel=SetCurrentConfiguration(femmodel,BalancedthicknessAnalysisEnum);
-		femmodel=solver_linear(femmodel);
-	else
-		error('solution not implemented yet');
-	end
-
-	displaystring('\n%s',['      computing adjoint']);
-	if (solution_type==DiagnosticSolutionEnum | solution_type==SteadystateSolutionEnum)
-		if(isstokes), femmodel=SetCurrentConfiguration(femmodel,DiagnosticStokesAnalysisEnum,AdjointStokesAnalysisEnum);
-		else femmodel=SetCurrentConfiguration(femmodel,DiagnosticHorizAnalysisEnum,AdjointHorizAnalysisEnum); end
-	elseif (solution_type==BalancedthicknessSolutionEnum),
-		femmodel=SetCurrentConfiguration(femmodel,BalancedthicknessAnalysisEnum,AdjointBalancedthicknessAnalysisEnum);
-	end
-	femmodel=solver_adjoint_linear(femmodel);
-
-	if(solution_type==AdjointSolutionEnum & ~control_analysis)
-		displaystring(verbose,'\n%s',['      saving results...']);
-		femmodel.elements=InputToResult(femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters,AdjointxEnum);
-		femmodel.elements=InputToResult(femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters,AdjointyEnum);
-		if(isstokes),
-			femmodel.elements=InputToResult(femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters,AdjointzEnum);
-			femmodel.elements=InputToResult(femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters,AdjointpEnum);
-		end
-	end
-
Index: /issm/trunk/src/m/solutions/adjointbalancedthickness_core.m
===================================================================
--- /issm/trunk/src/m/solutions/adjointbalancedthickness_core.m	(revision 5377)
+++ /issm/trunk/src/m/solutions/adjointbalancedthickness_core.m	(revision 5377)
@@ -0,0 +1,24 @@
+function femmodel=adjointbalancedthickness_core(femmodel),
+%ADJOINT_CORE - compute inverse method adjoint state
+%
+%   Usage:
+%      femmodel=adjointbalancedthickness_core(femmodel)
+
+	%recover parameters common to all solutions
+	verbose=femmodel.parameters.Verbose;
+	control_analysis=femmodel.parameters.ControlAnalysis;
+	solution_type=femmodel.parameters.SolutionType;
+
+	%set analysis type to compute velocity:
+	displaystring('\n%s',['      computing thickness']);
+	femmodel=SetCurrentConfiguration(femmodel,BalancedthicknessAnalysisEnum);
+	femmodel=solver_linear(femmodel);
+
+	displaystring('\n%s',['      computing adjoint']);
+	femmodel=SetCurrentConfiguration(femmodel,BalancedthicknessAnalysisEnum,AdjointBalancedthicknessAnalysisEnum);
+	femmodel=solver_adjoint_linear(femmodel);
+
+	if(solution_type==AdjointSolutionEnum & ~control_analysis)
+		displaystring(verbose,'\n%s',['      saving results...']);
+		femmodel.elements=InputToResult(femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters,AdjointEnum);
+	end
Index: /issm/trunk/src/m/solutions/adjointdiagnostic_core.m
===================================================================
--- /issm/trunk/src/m/solutions/adjointdiagnostic_core.m	(revision 5377)
+++ /issm/trunk/src/m/solutions/adjointdiagnostic_core.m	(revision 5377)
@@ -0,0 +1,34 @@
+function femmodel=adjointdiagnostic_core(femmodel),
+%ADJOINT_CORE - compute inverse method adjoint state
+%
+%   Usage:
+%      femmodel=adjointdiagnostic_core(femmodel)
+
+	%recover parameters common to all solutions
+	verbose=femmodel.parameters.Verbose;
+	isstokes=femmodel.parameters.IsStokes;
+	dim=femmodel.parameters.Dim;
+	control_analysis=femmodel.parameters.ControlAnalysis;
+	solution_type=femmodel.parameters.SolutionType;
+	conserve_loads=true;
+
+	%set analysis type to compute velocity:
+	displaystring('\n%s',['      computing velocities']);
+	if(isstokes), femmodel=SetCurrentConfiguration(femmodel,DiagnosticStokesAnalysisEnum);
+	else femmodel=SetCurrentConfiguration(femmodel,DiagnosticHorizAnalysisEnum); end
+	femmodel=solver_diagnostic_nonlinear(femmodel,conserve_loads);
+
+	displaystring('\n%s',['      computing adjoint']);
+	if(isstokes), femmodel=SetCurrentConfiguration(femmodel,DiagnosticStokesAnalysisEnum,AdjointStokesAnalysisEnum);
+	else femmodel=SetCurrentConfiguration(femmodel,DiagnosticHorizAnalysisEnum,AdjointHorizAnalysisEnum); end
+	femmodel=solver_adjoint_linear(femmodel);
+
+	if(solution_type==AdjointSolutionEnum & ~control_analysis)
+		displaystring(verbose,'\n%s',['      saving results...']);
+		femmodel.elements=InputToResult(femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters,AdjointxEnum);
+		femmodel.elements=InputToResult(femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters,AdjointyEnum);
+		if(isstokes),
+			femmodel.elements=InputToResult(femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters,AdjointzEnum);
+			femmodel.elements=InputToResult(femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters,AdjointpEnum);
+		end
+	end
Index: /issm/trunk/src/m/solutions/control_core.m
===================================================================
--- /issm/trunk/src/m/solutions/control_core.m	(revision 5376)
+++ /issm/trunk/src/m/solutions/control_core.m	(revision 5377)
@@ -29,4 +29,5 @@
 	%Get core from solution type
 	solutioncore=CorePointerFromSolutionEnum(solution_type);
+	adjointcore=AdjointCorePointerFromSolutionEnum(solution_type);
 
 	%Preprocess models
@@ -44,4 +45,7 @@
 			femmodel=steadystate_core(femmodel);
 		end
+
+		displaystring(verbose,'\n%s',['      compute adjoint state...']);
+		eval(['femmodel=' adjointcore '(femmodel);']);
 
 		displaystring(verbose,'\n%s',['      computing gradJ...']);
Index: /issm/trunk/src/m/solutions/gradient_core.m
===================================================================
--- /issm/trunk/src/m/solutions/gradient_core.m	(revision 5376)
+++ /issm/trunk/src/m/solutions/gradient_core.m	(revision 5377)
@@ -24,7 +24,4 @@
 	control_type=femmodel.parameters.ControlType;
 	control_steady=femmodel.parameters.ControlSteady;
-
-	displaystring(verbose,'\n%s',['      compute adjoint state...']);
-	femmodel=adjoint_core(femmodel);
 
 	displaystring(verbose,'\n%s',['      compute gradient...']);
