Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TQpLinSolverBase.cxx
Go to the documentation of this file.
1 // @(#)root/quadp:$Id$
2 // Author: Eddy Offermann May 2004
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /*************************************************************************
13  * Parts of this file are copied from the OOQP distribution and *
14  * are subject to the following license: *
15  * *
16  * COPYRIGHT 2001 UNIVERSITY OF CHICAGO *
17  * *
18  * The copyright holder hereby grants you royalty-free rights to use, *
19  * reproduce, prepare derivative works, and to redistribute this software*
20  * to others, provided that any changes are clearly documented. This *
21  * software was authored by: *
22  * *
23  * E. MICHAEL GERTZ gertz@mcs.anl.gov *
24  * Mathematics and Computer Science Division *
25  * Argonne National Laboratory *
26  * 9700 S. Cass Avenue *
27  * Argonne, IL 60439-4844 *
28  * *
29  * STEPHEN J. WRIGHT swright@cs.wisc.edu *
30  * Computer Sciences Department *
31  * University of Wisconsin *
32  * 1210 West Dayton Street *
33  * Madison, WI 53706 FAX: (608)262-9777 *
34  * *
35  * Any questions or comments may be directed to one of the authors. *
36  * *
37  * ARGONNE NATIONAL LABORATORY (ANL), WITH FACILITIES IN THE STATES OF *
38  * ILLINOIS AND IDAHO, IS OWNED BY THE UNITED STATES GOVERNMENT, AND *
39  * OPERATED BY THE UNIVERSITY OF CHICAGO UNDER PROVISION OF A CONTRACT *
40  * WITH THE DEPARTMENT OF ENERGY. *
41  *************************************************************************/
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 ///
45 /// \class TQpLinSolverBase
46 ///
47 /// Implementation of main solver for linear systems
48 ///
49 ////////////////////////////////////////////////////////////////////////////////
50 
51 #include "Riostream.h"
52 #include "TQpLinSolverBase.h"
53 #include "TMatrixD.h"
54 
55 ClassImp(TQpLinSolverBase);
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Default constructor
59 
60 TQpLinSolverBase::TQpLinSolverBase()
61 {
62  fNx = 0;
63  fMy = 0;
64  fMz = 0;
65  fNxup = 0;
66  fNxlo = 0;
67  fMcup = 0;
68  fMclo = 0;
69  fFactory = 0;
70 }
71 
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// Constructor
75 
76 TQpLinSolverBase::TQpLinSolverBase(TQpProbBase *factory,TQpDataBase *data)
77 {
78  fFactory = factory;
79 
80  fNx = data->fNx;
81  fMy = data->fMy;
82  fMz = data->fMz;
83 
84  fXloIndex.ResizeTo(data->fXloIndex); fXloIndex = data->fXloIndex;
85  fXupIndex.ResizeTo(data->fXupIndex); fXupIndex = data->fXupIndex;
86  fCloIndex.ResizeTo(data->fCloIndex); fCloIndex = data->fCloIndex;
87  fCupIndex.ResizeTo(data->fCupIndex); fCupIndex = data->fCupIndex;
88 
89  fNxlo = fXloIndex.NonZeros();
90  fNxup = fXupIndex.NonZeros();
91  fMclo = fCloIndex.NonZeros();
92  fMcup = fCupIndex.NonZeros();
93 
94  if (fNxup+fNxlo > 0) {
95  fDd.ResizeTo(fNx);
96  fDq.ResizeTo(fNx);
97  data->GetDiagonalOfQ(fDq);
98  }
99  fNomegaInv.ResizeTo(fMz);
100  fRhs .ResizeTo(fNx+fMy+fMz);
101 }
102 
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// Copy constructor
106 
107 TQpLinSolverBase::TQpLinSolverBase(const TQpLinSolverBase &another) : TObject(another),
108  fFactory(another.fFactory)
109 {
110  *this = another;
111 }
112 
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// Sets up the matrix for the main linear system in "augmented system" form. The
116 /// actual factorization is performed by a routine specific to either the sparse
117 /// or dense case
118 
119 void TQpLinSolverBase::Factor(TQpDataBase * /* prob */,TQpVar *vars)
120 {
121  R__ASSERT(vars->ValidNonZeroPattern());
122 
123  if (fNxlo+fNxup > 0) {
124  fDd.ResizeTo(fDq);
125  fDd = fDq;
126  }
127  this->ComputeDiagonals(fDd,fNomegaInv,
128  vars->fT,vars->fLambda,vars->fU,vars->fPi,
129  vars->fV,vars->fGamma,vars->fW,vars->fPhi);
130  if (fNxlo+fNxup > 0) this->PutXDiagonal(fDd);
131 
132  fNomegaInv.Invert();
133  fNomegaInv *= -1.;
134 
135  if (fMclo+fMcup > 0) this->PutZDiagonal(fNomegaInv);
136 }
137 
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Computes the diagonal matrices in the augmented system from the current set of variables
141 
142 void TQpLinSolverBase::ComputeDiagonals(TVectorD &dd,TVectorD &omega,
143  TVectorD &t, TVectorD &lambda,
144  TVectorD &u, TVectorD &pi,
145  TVectorD &v, TVectorD &gamma,
146  TVectorD &w, TVectorD &phi)
147 {
148  if (fNxup+fNxlo > 0) {
149  if (fNxlo > 0) AddElemDiv(dd,1.0,gamma,v,fXloIndex);
150  if (fNxup > 0) AddElemDiv(dd,1.0,phi ,w,fXupIndex);
151  }
152  omega.Zero();
153  if (fMclo > 0) AddElemDiv(omega,1.0,lambda,t,fCloIndex);
154  if (fMcup > 0) AddElemDiv(omega,1.0,pi, u,fCupIndex);
155 }
156 
157 
158 ////////////////////////////////////////////////////////////////////////////////
159 /// Solves the system for a given set of residuals. Assembles the right-hand side appropriate
160 /// to the matrix factored in factor, solves the system using the factorization produced there,
161 /// partitions the solution vector into step components, then recovers the step components
162 /// eliminated during the block elimination that produced the augmented system form .
163 
164 void TQpLinSolverBase::Solve(TQpDataBase *prob,TQpVar *vars,TQpResidual *res,TQpVar *step)
165 {
166  R__ASSERT(vars->ValidNonZeroPattern());
167  R__ASSERT(res ->ValidNonZeroPattern());
168 
169  (step->fX).ResizeTo(res->fRQ); step->fX = res->fRQ;
170  if (fNxlo > 0) {
171  TVectorD &vInvGamma = step->fV;
172  vInvGamma.ResizeTo(vars->fGamma); vInvGamma = vars->fGamma;
173  ElementDiv(vInvGamma,vars->fV,fXloIndex);
174 
175  AddElemMult(step->fX,1.0,vInvGamma,res->fRv);
176  AddElemDiv (step->fX,1.0,res->fRgamma,vars->fV,fXloIndex);
177  }
178 
179  if (fNxup > 0) {
180  TVectorD &wInvPhi = step->fW;
181  wInvPhi.ResizeTo(vars->fPhi); wInvPhi = vars->fPhi;
182  ElementDiv(wInvPhi,vars->fW,fXupIndex);
183 
184  AddElemMult(step->fX,1.0,wInvPhi,res->fRw);
185  AddElemDiv (step->fX,-1.0,res->fRphi,vars->fW,fXupIndex);
186  }
187 
188  // start by partially computing step->fS
189  (step->fS).ResizeTo(res->fRz); step->fS = res->fRz;
190  if (fMclo > 0) {
191  TVectorD &tInvLambda = step->fT;
192  tInvLambda.ResizeTo(vars->fLambda); tInvLambda = vars->fLambda;
193  ElementDiv(tInvLambda,vars->fT,fCloIndex);
194 
195  AddElemMult(step->fS,1.0,tInvLambda,res->fRt);
196  AddElemDiv (step->fS,1.0,res->fRlambda,vars->fT,fCloIndex);
197  }
198 
199  if (fMcup > 0) {
200  TVectorD &uInvPi = step->fU;
201 
202  uInvPi.ResizeTo(vars->fPi); uInvPi = vars->fPi;
203  ElementDiv(uInvPi,vars->fU,fCupIndex);
204 
205  AddElemMult(step->fS,1.0,uInvPi,res->fRu);
206  AddElemDiv (step->fS,-1.0,res->fRpi,vars->fU,fCupIndex);
207  }
208 
209  (step->fY).ResizeTo(res->fRA); step->fY = res->fRA;
210  (step->fZ).ResizeTo(res->fRC); step->fZ = res->fRC;
211 
212  if (fMclo > 0)
213  this->SolveXYZS(step->fX,step->fY,step->fZ,step->fS,step->fLambda,prob);
214  else
215  this->SolveXYZS(step->fX,step->fY,step->fZ,step->fS,step->fPi,prob);
216 
217  if (fMclo > 0) {
218  (step->fT).ResizeTo(step->fS); step->fT = step->fS;
219  Add(step->fT,-1.0,res->fRt);
220  (step->fT).SelectNonZeros(fCloIndex);
221 
222  (step->fLambda).ResizeTo(res->fRlambda); step->fLambda = res->fRlambda;
223  AddElemMult(step->fLambda,-1.0,vars->fLambda,step->fT);
224  ElementDiv(step->fLambda,vars->fT,fCloIndex);
225  }
226 
227  if (fMcup > 0) {
228  (step->fU).ResizeTo(res->fRu); step->fU = res->fRu;
229  Add(step->fU,-1.0,step->fS);
230  (step->fU).SelectNonZeros(fCupIndex);
231 
232  (step->fPi).ResizeTo(res->fRpi); step->fPi = res->fRpi;
233  AddElemMult(step->fPi,-1.0,vars->fPi,step->fU);
234  ElementDiv(step->fPi,vars->fU,fCupIndex);
235  }
236 
237  if (fNxlo > 0) {
238  (step->fV).ResizeTo(step->fX); step->fV = step->fX;
239  Add(step->fV,-1.0,res->fRv);
240  (step->fV).SelectNonZeros(fXloIndex);
241 
242  (step->fGamma).ResizeTo(res->fRgamma); step->fGamma = res->fRgamma;
243  AddElemMult(step->fGamma,-1.0,vars->fGamma,step->fV);
244  ElementDiv(step->fGamma,vars->fV,fXloIndex);
245  }
246 
247  if (fNxup > 0) {
248  (step->fW).ResizeTo(res->fRw); step->fW = res->fRw;
249  Add(step->fW,-1.0,step->fX);
250  (step->fW).SelectNonZeros(fXupIndex);
251 
252  (step->fPhi).ResizeTo(res->fRphi); step->fPhi = res->fRphi;
253  AddElemMult(step->fPhi,-1.0,vars->fPhi,step->fW);
254  ElementDiv(step->fPhi,vars->fW,fXupIndex);
255  }
256  R__ASSERT(step->ValidNonZeroPattern());
257 }
258 
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// Assemble right-hand side of augmented system and call SolveCompressed to solve it
262 
263 void TQpLinSolverBase::SolveXYZS(TVectorD &stepx,TVectorD &stepy,
264  TVectorD &stepz,TVectorD &steps,
265  TVectorD & /* ztemp */, TQpDataBase * /* prob */ )
266 {
267  AddElemMult(stepz,-1.0,fNomegaInv,steps);
268  this->JoinRHS(fRhs,stepx,stepy,stepz);
269 
270  this->SolveCompressed(fRhs);
271 
272  this->SeparateVars(stepx,stepy,stepz,fRhs);
273 
274  stepy *= -1.;
275  stepz *= -1.;
276 
277  Add(steps,-1.0,stepz);
278  ElementMult(steps,fNomegaInv);
279  steps *= -1.;
280 }
281 
282 
283 ////////////////////////////////////////////////////////////////////////////////
284 /// Assembles a single vector object from three given vectors .
285 /// rhs_out (output) final joined vector
286 /// rhs1_in (input) first part of rhs
287 /// rhs2_in (input) middle part of rhs
288 /// rhs3_in (input) last part of rhs .
289 
290 void TQpLinSolverBase::JoinRHS(TVectorD &rhs_out, TVectorD &rhs1_in,
291  TVectorD &rhs2_in,TVectorD &rhs3_in)
292 {
293  fFactory->JoinRHS(rhs_out,rhs1_in,rhs2_in,rhs3_in);
294 }
295 
296 
297 ////////////////////////////////////////////////////////////////////////////////
298 /// Extracts three component vectors from a given aggregated vector.
299 /// vars_in (input) aggregated vector
300 /// x_in (output) first part of vars
301 /// y_in (output) middle part of vars
302 /// z_in (output) last part of vars
303 
304 void TQpLinSolverBase::SeparateVars(TVectorD &x_in,TVectorD &y_in,
305  TVectorD &z_in,TVectorD &vars_in)
306 {
307  fFactory->SeparateVars(x_in,y_in,z_in,vars_in);
308 }
309 
310 
311 ////////////////////////////////////////////////////////////////////////////////
312 /// Assignment opeartor
313 
314 TQpLinSolverBase &TQpLinSolverBase::operator=(const TQpLinSolverBase &source)
315 {
316  if (this != &source) {
317  TObject::operator=(source);
318 
319  fNx = source.fNx;
320  fMy = source.fMy;
321  fMz = source.fMz;
322  fNxup = source.fNxup;
323  fNxlo = source.fNxlo;
324  fMcup = source.fMcup;
325  fMclo = source.fMclo;
326 
327  fNomegaInv.ResizeTo(source.fNomegaInv); fNomegaInv = source.fNomegaInv;
328  fRhs .ResizeTo(source.fRhs); fRhs = source.fRhs;
329 
330  fDd .ResizeTo(source.fDd); fDd = source.fDd;
331  fDq .ResizeTo(source.fDq); fDq = source.fDq;
332 
333  fXupIndex .ResizeTo(source.fXupIndex); fXupIndex = source.fXupIndex;
334  fCupIndex .ResizeTo(source.fCupIndex); fCupIndex = source.fCupIndex;
335  fXloIndex .ResizeTo(source.fXloIndex); fXloIndex = source.fXloIndex;
336  fCloIndex .ResizeTo(source.fCloIndex); fCloIndex = source.fCloIndex;
337 
338  // LM : copy also pointer data member
339  fFactory = source.fFactory;
340  }
341  return *this;
342 }