File: MasterContactSolver.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 88,820 kB
  • ctags: 27,300
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 68
file content (829 lines) | stat: -rw-r--r-- 24,267 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/
#include <sofa/component/mastersolver/MasterContactSolver.h>

#include <sofa/simulation/common/AnimateVisitor.h>
#include <sofa/simulation/common/BehaviorUpdatePositionVisitor.h>
#include <sofa/simulation/common/MechanicalVisitor.h>
#include <sofa/simulation/common/SolveVisitor.h>

#include <sofa/helper/LCPcalc.h>

#include <sofa/core/ObjectFactory.h>

#include <sofa/helper/system/thread/CTime.h>
#include <math.h>
#include <iostream>




namespace sofa
{

namespace component
{

namespace odesolver
{


LCP::LCP(unsigned int mxC) : maxConst(mxC), tol(0.00001), numItMax(1000), useInitialF(true), mu(0.0), dim(0), lok(false)
{
	W.resize(maxConst,maxConst);
    dFree.resize(maxConst);
    f.resize(2*maxConst+1);
}

LCP::~LCP()
{
}

void LCP::reset(void)
{
	W.clear();
	W.clear();
	dFree.clear();
}



using namespace sofa::defaulttype;
using namespace helper::system::thread;
using namespace core::componentmodel::behavior;




#define MAX_NUM_CONSTRAINTS 1024
//#define DISPLAY_TIME

MasterContactSolver::MasterContactSolver()
:displayTime(initData(&displayTime, false, "displayTime","Display time for each important step of MasterContactSolver."))
,initial_guess(initData(&initial_guess, true, "initial_guess","activate LCP results history to improve its resolution performances."))
,build_lcp(initData(&build_lcp, true, "build_lcp", "LCP is not fully built to increase performance in some case."))
,tol( initData(&tol, 0.001, "tolerance", ""))
,maxIt( initData(&maxIt, 1000, "maxIt", ""))
,mu( initData(&mu, 0.6, "mu", ""))
, constraintGroups( initData(&constraintGroups, "group", "list of ID of groups of constraints to be handled by this solver.") )
,_mu(0.6)
, lcp1(MAX_NUM_CONSTRAINTS)
, lcp2(MAX_NUM_CONSTRAINTS)
, _W(&lcp1.W)
, lcp(&lcp1)
, _dFree(&lcp1.dFree)
, _result(&lcp1.f)
{
	_numConstraints = 0;
	_mu = 0.0;
	constraintGroups.beginEdit()->insert(0);
	constraintGroups.endEdit();

	_numPreviousContact=0;
	_PreviousContactList = (contactBuf *)malloc(MAX_NUM_CONSTRAINTS * sizeof(contactBuf));
	_cont_id_list = (long *)malloc(MAX_NUM_CONSTRAINTS * sizeof(long));
	
	_Wdiag = new SparseMatrix<double>();
}

void MasterContactSolver::init()
{
    getContext()->get<core::componentmodel::behavior::BaseConstraintCorrection>(&constraintCorrections, core::objectmodel::BaseContext::SearchDown);
}

void MasterContactSolver::build_LCP()
{
	_numConstraints = 0;
//sout<<" accumulateConstraint "  <<sendl;

	// mechanical action executed from root node to propagate the constraints
	simulation::MechanicalResetConstraintVisitor().execute(context);
	simulation::MechanicalAccumulateConstraint(_numConstraints).execute(context);
	_mu = mu.getValue();


	//sout<<" accumulateConstraint_done "  <<sendl;

	if (_numConstraints > MAX_NUM_CONSTRAINTS)
	{
		serr<<sendl<<"Error in MasterContactSolver, maximum number of contacts exceeded, "<< _numConstraints/3 <<" contacts detected"<<sendl;
		exit(-1);
	}

	lcp->getMu() = _mu;

    _dFree->resize(_numConstraints);
    _W->resize(_numConstraints,_numConstraints);

	MechanicalGetConstraintValueVisitor(_dFree).execute(context);
//	simulation::MechanicalComputeComplianceVisitor(_W).execute(context);

    if (this->f_printLog.getValue()) sout<<"MasterContactSolver: "<<_numConstraints<<" constraints, mu = "<<_mu<<sendl;

	//sout<<" computeCompliance in "  << constraintCorrections.size()<< " constraintCorrections" <<sendl;

    for (unsigned int i=0;i<constraintCorrections.size();i++)
    {
		core::componentmodel::behavior::BaseConstraintCorrection* cc = constraintCorrections[i];
		cc->getCompliance(_W);
	}
	//sout<<" computeCompliance_done "  <<sendl;

	if (initial_guess.getValue())
	{
		MechanicalGetContactIDVisitor(_cont_id_list).execute(context);
		computeInitialGuess();
	}
}

/// build_problem_info
/// When the LCP or the NLCP is not fully built, the  diagonal blocks of the matrix are still needed for the resolution
/// This function ask to the constraintCorrection classes to build this diagonal blocks
void MasterContactSolver::build_problem_info()
{

	// debug
	//std::cout<<" accumulateConstraint "  <<std::endl;
	_numConstraints = 0;
	// mechanical action executed from root node to propagate the constraints
	simulation::MechanicalResetConstraintVisitor().execute(context);
	simulation::MechanicalAccumulateConstraint(_numConstraints).execute(context);
	_mu = mu.getValue();
	
	// debug
	//std::cout<<" accumulateConstraint_done "  <<std::endl;

	// necessary ///////
	if (_numConstraints > MAX_NUM_CONSTRAINTS)
	{
		serr<<sendl<<"Error in MasterContactSolver, maximum number of contacts exceeded, "<< _numConstraints/3 <<" contacts detected"<<sendl;
		exit(-1);
	}

	lcp->getMu() = _mu;

    _dFree->resize(_numConstraints);
	// as _Wdiag is a sparse matrix resize do not allocate memory
	_Wdiag->resize(_numConstraints,_numConstraints); 
	_result->resize(_numConstraints);

	// debug
	//std::cout<<" resize done "  <<std::endl;
		
	MechanicalGetConstraintValueVisitor(_dFree).execute(context);

    if (this->f_printLog.getValue()) sout<<"MasterContactSolver: "<<_numConstraints<<" constraints, mu = "<<_mu<<sendl;

	//debug
	//std::cout<<" computeCompliance in "  << constraintCorrections.size()<< " constraintCorrections" <<std::endl;

   
	if (initial_guess.getValue())
	{
		MechanicalGetContactIDVisitor(_cont_id_list).execute(context);
		computeInitialGuess();
	}	
		
	

	
	

}



void MasterContactSolver::computeInitialGuess()
{
	int numContact = (_mu > 0.0) ? _numConstraints/3 : _numConstraints;

	for (int c=0; c<numContact; c++)
	{
		if (_mu>0.0){
			(*_result)[3*c  ] = 0.0;
			(*_result)[3*c+1] = 0.0;
			(*_result)[3*c+2] = 0.0;
		}
		else
		{
			(*_result)[c] =  0.0;
			(*_result)[c+numContact] =  0.0;
		}
	}


	for (int c=0; c<numContact; c++)
	{
		for (unsigned int pc=0; pc<_numPreviousContact; pc++)
		{
			if (_cont_id_list[c] == _PreviousContactList[pc].id)
			{
				if (_mu>0.0){
					(*_result)[3*c  ] = _PreviousContactList[pc].F.x();
					(*_result)[3*c+1] = _PreviousContactList[pc].F.y();
					(*_result)[3*c+2] = _PreviousContactList[pc].F.z();
				}
				else
				{
					(*_result)[c+numContact] =  _PreviousContactList[pc].F.x();
				}
			}
		}
	}
}

void MasterContactSolver::keepContactForcesValue()
{
	_numPreviousContact=0;

	int numContact = (_mu > 0.0) ? _numConstraints/3 : _numConstraints;

	for (int c=0; c<numContact; c++)
	{
		if (_mu>0.0)
		{
			if ((*_result)[3*c]>0.0)//((_result[3*c]>0.0)||(_result[3*c+1]>0.0)||(_result[3*c+2]>0.0))
			{
				_PreviousContactList[_numPreviousContact].id = (_cont_id_list[c] >= 0) ? _cont_id_list[c] : -_cont_id_list[c];
				_PreviousContactList[_numPreviousContact].F.x() = (*_result)[3*c];
				_PreviousContactList[_numPreviousContact].F.y() = (*_result)[3*c+1];
				_PreviousContactList[_numPreviousContact].F.z() = (*_result)[3*c+2];
				_numPreviousContact++;
			}
		}
		else
		{
			if ((*_result)[c]>0.0)
			{
				_PreviousContactList[_numPreviousContact].id = (_cont_id_list[c] >= 0) ? _cont_id_list[c] : -_cont_id_list[c];
				_PreviousContactList[_numPreviousContact].F.x() = (*_result)[c];
				_numPreviousContact++;
			}

		}
	}
}

void MasterContactSolver::step(double dt)
{

	context = dynamic_cast<simulation::Node *>(this->getContext()); // access to current node
	CTime *timer;
	CTime *timerTotal;
	double time = 0.0;
	double timeTotal=0.0;
	double timeScale = 1000.0 / (double)CTime::getRefTicksPerSec();
	if ( displayTime.getValue() )
	{
		timer = new CTime();
		timerTotal = new CTime();
		time = (double) timer->getTime();
		timeTotal = (double) timerTotal->getTime();
		sout<<"********* Start Iteration : " << _numConstraints << " constraints *********" <<sendl;
	}

	// Update the BehaviorModels
	// Required to allow the RayPickInteractor interaction
	
	if( f_printLog.getValue())
		serr<<"updatePos called"<<sendl;

	simulation::BehaviorUpdatePositionVisitor updatePos(dt);
	context->execute(&updatePos);
	
	if( f_printLog.getValue())
		serr<<"updatePos performed - beginVisitor called"<<sendl;


    simulation::MechanicalBeginIntegrationVisitor beginVisitor(dt);
    context->execute(&beginVisitor);

	if( f_printLog.getValue())
		serr<<"beginVisitor performed - SolveVisitor for freeMotion is called"<<sendl;	
	
	// Free Motion
    simulation::SolveVisitor freeMotion(dt, true);
    context->execute(&freeMotion);
	simulation::MechanicalPropagateFreePositionVisitor().execute(context);
	
	if( f_printLog.getValue())
		serr<<" SolveVisitor for freeMotion performed - propagate DX called"<<sendl;	

	core::componentmodel::behavior::BaseMechanicalState::VecId dx_id = core::componentmodel::behavior::BaseMechanicalState::VecId::dx();
	simulation::MechanicalVOpVisitor(dx_id).execute( context);
	simulation::MechanicalPropagateDxVisitor(dx_id).execute( context);
	simulation::MechanicalVOpVisitor(dx_id).execute( context);

	if ( displayTime.getValue() )
	{
		sout << " >>>>> Begin display MasterContactSolver time" << sendl;
		sout<<" Free Motion " << ( (double) timer->getTime() - time)*timeScale <<" ms" <<sendl;

		time = (double) timer->getTime();
	}
	
	if( f_printLog.getValue())
		serr<<" propagate DXn performed - collision called"<<sendl;		

	// Collision detection and response creation
	computeCollision();

	if ( displayTime.getValue() )
	{
		sout<<" computeCollision " << ( (double) timer->getTime() - time)*timeScale <<" ms" <<sendl;
		time = (double) timer->getTime();
	}
//	MechanicalResetContactForceVisitor().execute(context);

	sout<<"constraintCorrections is called"<<sendl;
	
    for (unsigned int i=0;i<constraintCorrections.size();i++)
    {
		core::componentmodel::behavior::BaseConstraintCorrection* cc = constraintCorrections[i];
		cc->resetContactForce();
	}
	
	sout<<"constraintCorrections is finished"<<sendl;
	if(build_lcp.getValue())
	{
		sout<<"build_LCP is called"<<sendl;
		build_LCP();
		
		
		if ( displayTime.getValue() )
		{
			sout<<" build_LCP " << ( (double) timer->getTime() - time)*timeScale<<" ms" <<sendl;
			time = (double) timer->getTime();
		}
		
		double _tol = tol.getValue();
		int _maxIt = maxIt.getValue();
		
		if (_mu > 0.0)
		{
			
			lcp->setNbConst(_numConstraints);
			lcp->setTol(_tol);
			helper::nlcp_gaussseidel(_numConstraints, _dFree->ptr(), _W->lptr(), _result->ptr(), _mu, _tol, _maxIt, initial_guess.getValue());
			if (this->f_printLog.getValue()) helper::afficheLCP(_dFree->ptr(), _W->lptr(), _result->ptr(),_numConstraints);
		}
		else
		{
			// warning _A has been being suppr... need to be allocated
			//
			//		helper::lcp_lexicolemke(_numConstraints, _dFree->ptr(), _W->lptr(), _A.lptr(), _result->ptr());
			helper::gaussSeidelLCP1(_numConstraints, _dFree->ptr(), _W->lptr(), _result->ptr(), _tol, _maxIt);
			if (this->f_printLog.getValue()) helper::afficheLCP(_dFree->ptr(), _W->lptr(), _result->ptr(),_numConstraints);
		}
	}
	else
	{

		
			
		build_problem_info();
		//std::cout<<"build_problem_info is finished"<<std::endl;
		if ( displayTime.getValue() )
		{
			sout<<" build_problem " << ( (double) timer->getTime() - time)*timeScale<<" ms" <<sendl;
			time = (double) timer->getTime();
		}		
		
		//std::cout<<"gaussseidel_unbuilt"<<std::endl;
		//std::cout<<"_result-before :"<<_result<<std::endl;
		gaussseidel_unbuilt(_dFree->ptr(), _result->ptr());
		//std::cout<<"\n_result unbuilt:"<<(*_result)<<std::endl;

	   /////// debug
	  /* 
	   _result->resize(_numConstraints);
	   
	   double _tol = tol.getValue();
	   int _maxIt = maxIt.getValue();
	   
	   build_LCP();
	   helper::nlcp_gaussseidel(_numConstraints, _dFree->ptr(), _W->lptr(), _result->ptr(), _mu, _tol, _maxIt, initial_guess.getValue());
		std::cout<<"\n_result nlcp :"<<(*_result)<<std::endl;
		*/	
	   //std::cout<<"LCP:"<<std::endl;
	   //helper::afficheLCP(_dFree->ptr(), _W->lptr(), _result->ptr(),_numConstraints);
		//std::cout<<"build_problem_info is called"<<std::endl;
		
	   ////////			
		
	
	}

	if ( displayTime.getValue() )
	{
		sout<<" TOTAL solve_LCP" <<( (double) timer->getTime() - time)*timeScale<<" ms" <<sendl;
		time = (double) timer->getTime();
	}

	if (initial_guess.getValue())
		keepContactForcesValue();

	if(this->f_printLog.getValue())
	{
		serr<<"keepContactForces done"<<sendl;

	}

//	MechanicalApplyContactForceVisitor(_result).execute(context);
    for (unsigned int i=0;i<constraintCorrections.size();i++)
    {
		core::componentmodel::behavior::BaseConstraintCorrection* cc = constraintCorrections[i];
		cc->applyContactForce(_result);
	}
	if(this->f_printLog.getValue())
	{
		serr<<"applyContactForce in constraintCorrection done"<<sendl;

	}

	simulation::MechanicalPropagateAndAddDxVisitor().execute( context);

	//simulation::MechanicalPropagatePositionAndVelocityVisitor().execute(context);

	//simulation::MechanicalPropagateAndAddDxVisitor().execute( context);

	if(this->f_printLog.getValue())
	{
		serr<<"propagate corrective motion done"<<sendl;

	}

//	MechanicalResetContactForceVisitor().execute(context);
    for (unsigned int i=0;i<constraintCorrections.size();i++)
    {
		core::componentmodel::behavior::BaseConstraintCorrection* cc = constraintCorrections[i];
		cc->resetContactForce();
	}
	if ( displayTime.getValue() )
	{
		sout<<" contactCorrections" <<( (double) timer->getTime() - time)*timeScale <<" s" <<sendl;
		sout << "<<<<<< End display MasterContactSolver time." << sendl;
	}


	if(build_lcp.getValue())
	{
		lcp = (lcp == &lcp1) ? &lcp2 : &lcp1;
		_W = &lcp->W;
		_dFree = &lcp->dFree;
		_result = &lcp->f;
	}


	//lcp1.unlock();
	//lcp2.unlock();
	//sout << "new lcp : " << lcp << endl;

	//struct timespec ts;
	//ts.tv_sec = 0;
    //ts.tv_nsec = 60 *1000 *1000;
//	nanosleep(&ts, NULL);

    simulation::MechanicalEndIntegrationVisitor endVisitor(dt);
    context->execute(&endVisitor);


	if (displayTime.getValue())
	{
		sout<<" TotalTime" <<( (double) timerTotal->getTime() - timeTotal)*timeScale <<" ms" <<sendl;
	}

}


int MasterContactSolver::gaussseidel_unbuilt(double *dfree, double *f)
{

	CTime *timer;
	double time = 0.0;
	double timeScale = 1000.0 / (double)CTime::getRefTicksPerSec();
	if ( displayTime.getValue() )
	{
		timer = new CTime();
		time = (double) timer->getTime();
	}


	if(_mu==0.0)
	{	
		serr<<"WARNING: frictionless case with unbuilt lcp is not implemented"<<sendl;
	}
	
	/////// test: numContacts = _numConstraints/3 (must be dividable by 3)
	double test = _numConstraints/3;
	int numContacts =  (int) floor(test);
	test = _numConstraints/3 - numContacts;

	if (test>0.01){
		serr<<" WARNING dim should be dividable by 3 in nlcp_gaussseidel"<<sendl;
		return 0;
	}
	//////////////////////////////////////////////
	// iterators
	int it,c1;
	
	//////////////////////////////////////////////
	// data for iterative procedure
	double _tol = tol.getValue();
	int _maxIt = maxIt.getValue();
	double _mu = mu.getValue();
	
	//debug
	//std::cout<<"data are set"<<std::endl;				
	
	//////// Important component if the LCP is not build : 
	// for each contact, the pair of constraint correction that is involved with the contact is memorized
	_cclist_elem1.clear();
	_cclist_elem2.clear();
	for (c1=0; c1<numContacts; c1++){
		bool elem1 = false;
		bool elem2 = false;	
		for (unsigned int i=0;i<constraintCorrections.size();i++)
		{

			core::componentmodel::behavior::BaseConstraintCorrection* cc = constraintCorrections[i];
			if(cc->hasConstraintNumber(3*c1))
			{
				if(elem1){
					_cclist_elem2.push_back(cc);
					elem2=true;
				}
				else
				{
					_cclist_elem1.push_back(cc);
					elem1=true;
				}
				
			}
		}
		if(!elem1)
				serr<<"WARNING: no constraintCorrection found for contact"<<c1<<sendl;
		if(!elem2)
				_cclist_elem2.push_back(NULL);
				
	}
	
	//debug
	//std::cout<<"_cclist_elem1 _cclist_elem2 are set"<<std::endl;		
	
		
	
	// memory allocation of vector d
	double *d;
	d = (double*)malloc(_numConstraints*sizeof(double));	
	


	//////////////
	// Beginning of iterative computations
	//////////////
	
	/// each constraintCorrection has an internal force vector that is set to "0"
	
	// if necessary: modify the sequence of contact
	std::list<int> contact_sequence;
	
	for (unsigned int i=0;i<constraintCorrections.size();i++)
	{
		core::componentmodel::behavior::BaseConstraintCorrection* cc = constraintCorrections[i];	
		cc->resetForUnbuiltResolution(f, contact_sequence); 
	}
	
	bool change_contact_sequence = false;
	
	if(contact_sequence.size() ==_numConstraints)
		change_contact_sequence=true;
	
	
	
	/////////// the 3x3 diagonal block matrix is built:
	/////////// for each contact, the pair of constraintcorrection is called to add the contribution
	for (c1=0; c1<numContacts; c1++)
	{
		//debug
		//std::cout<<"contact "<<c1<<" cclist_elem1 : "<<_cclist_elem1[c1]->getName()<<std::endl;
		// compliance of object1
		_cclist_elem1[c1]->getBlockDiagonalCompliance(_Wdiag, 3*c1, 3*c1+2);			
		// compliance of object2 (if object2 exists)
		if(_cclist_elem2[c1] != NULL){
			_cclist_elem2[c1]->getBlockDiagonalCompliance(_Wdiag, 3*c1, 3*c1+2);
			// debug
			//std::cout<<"_cclist_elem2[c1]"<<std::endl;
		}
	}		
	
					

	// allocation of the inverted system 3x3 
	// TODO: evaluate the cost of this step : it can be avoied by directly feeding W33 in constraint correction 
	helper::LocalBlock33 **W33;
	W33 = (helper::LocalBlock33 **) malloc (_numConstraints*sizeof(helper::LocalBlock33));
	for (c1=0; c1<numContacts; c1++){
		W33[c1] = new helper::LocalBlock33();
		double w[6];
		w[0] = _Wdiag->element(3*c1  , 3*c1  );
		w[1] = _Wdiag->element(3*c1  , 3*c1+1);
		w[2] = _Wdiag->element(3*c1  , 3*c1+2);
		w[3] = _Wdiag->element(3*c1+1, 3*c1+1);
		w[4] = _Wdiag->element(3*c1+1, 3*c1+2);
		w[5] = _Wdiag->element(3*c1+2, 3*c1+2);
		W33[c1]->compute(w[0], w[1] , w[2], w[3], w[4] , w[5]);
	}			
	 
	// debug
	// std::cout<<"getBlockDiagonalCompliance  Wdiag = "<<(* _Wdiag)<<std::endl;
	// return 1;
	if ( displayTime.getValue() )
	{
		sout<<" build_constraints_and_diagonal " << ( (double) timer->getTime() - time)*timeScale<<" ms" <<sendl;
		time = (double) timer->getTime();
	}
	
	double error = 0;
	double dn, dt, ds, fn, ft, fs;

	for (it=0; it<_maxIt; it++)
	{
		std::list<int>::iterator it_c = contact_sequence.begin();
		error =0;
		for (int c=0; c<numContacts; c++)
		{
			if(change_contact_sequence)
			{
				int constraint = *it_c;
				c1 = constraint/3;
				it_c++; it_c++; it_c++;
				
			}
			else
				c1=c;
		
			//std::cout<<"it"<<it << " - c1 :"<<c1<<std::endl;
			
			// compute the current violation :
			
			// violation when no contact force
			d[3*c1]=dfree[3*c1]; d[3*c1+1]=dfree[3*c1+1]; d[3*c1+2]=dfree[3*c1+2];
			
			// debug		
			//if(c1<2)
			//	std::cout<<"free displacement for contact : dn_free = "<< d[3*c1] <<"  dt_free = "<<d[3*c1+1]<<"  ds_free = "<<d[3*c1+2]<<std::endl;
			
			
			// displacement of object1 due to contact force
			_cclist_elem1[c1]->addConstraintDisplacement(d, 3*c1, 3*c1+2);
			
			// displacement of object2 due to contact force (if object2 exists)
			if(_cclist_elem2[c1] != NULL)
				_cclist_elem2[c1]->addConstraintDisplacement(d, 3*c1, 3*c1+2);
			
				
		// set current force in fn, ft, fs
			fn=f[3*c1]; ft=f[3*c1+1]; fs=f[3*c1+2];
			
			// set displacement in dn, dt, ds
			dn=d[3*c1]; dt=d[3*c1+1]; ds=d[3*c1+2];												
			
			
			// debug
			//if(c1<2)
			//	std::cout<<"New_GS_State called : dn = "<<dn<<"  dt = "<<dt<<"  ds = "<<ds<<"  fn = "<<fn<<"  ft = "<<ft<<"  fs = "<<fs<<std::endl;
		
		
		

		
		// compute a new state for stick/slip 		
			/// ATTENTION  NOUVEAU GS_STATE : maintenant dn, dt et ds inclue les forces fn, ft, fs
			W33[c1]->New_GS_State(_mu,dn,dt,ds,fn,ft,fs);
			// debug
			//if(c1<2)
			//	std::cout<<"New_GS_State solved for contact "<<c1<<" : dn = "<<dn<<"  dt = "<<dt<<"  ds = "<<ds<<"  fn = "<<fn<<"  ft = "<<ft<<"  fs = "<<fs<<std::endl;
			
		// evaluate an error (based on displacement)
			error += helper::absError(dn,dt,ds,d[3*c1],d[3*c1+1],d[3*c1+2]);
			
			
			
			
			bool update;
			if (f[3*c1] == 0.0 && fn == 0.0)
				update=false;
			else
				update=true;

		// set the new force :
			// compute the Delta of contact forces:
			f[3*c1  ] = fn - f[3*c1  ];
			f[3*c1+1] = ft - f[3*c1+1];
			f[3*c1+2] = fs - f[3*c1+2];
			
			//std::cout<<"fn = "<< fn<<" -  ft = "<< ft<<" -  fs = "<<fs<<std::endl;
			///////// verifier si Delta force vaut 0 => pas la peine d'ajouter la force
			
			// set Delta force on object 1 for evaluating the followings displacement

			if(update)
			{
			_cclist_elem1[c1]->setConstraintDForce(f, 3*c1, 3*c1+2, update);  

			// set Delta force on object2 (if object2 exists)
			if(_cclist_elem2[c1] != NULL)
				_cclist_elem2[c1]->setConstraintDForce(f, 3*c1, 3*c1+2, update);
			}	
			
			
			///// debug : verifie si on retrouve le même dn
			/*
			d[3*c1]=dfree[3*c1]; d[3*c1+1]=dfree[3*c1+1]; d[3*c1+2]=dfree[3*c1+2];
			_cclist_elem1[c1]->addConstraintDisplacement(d, 3*c1, 3*c1+2);
			if(fabs(dn-d[3*c1]) > 0.000000001*fabs(dn) && dn> 0.1*_tol)
				std::cerr<<"WARNING debug : dn ="<<dn<<" d["<<3*c1<<"]= "<< d[3*c1]<<" dfree= "<<dfree[3*c1]<<"  - update :"<<update<<" with fn ="<<fn<<" and f["<<3*c1<<"]= "<< fn-f[3*c1  ]<<std::endl;
			*/
						
			// set force on the contact force vector
			helper::set3Dof(f,c1,fn,ft,fs);	

		}

		if (error < _tol*(numContacts+1)){
			free(d);
			if ( displayTime.getValue() )
			{
				sout<<"convergence after "<<it<<" iterations - error"<<error<<sendl;
			}
			//debug
			//std::cout<<" f : ["<<std::endl;
			for (int i = 0; i < numContacts; i++){
			//	std::cout<<f[3*i]<<"\n"<<f[3*i+1] <<"\n"<<f[3*i+2] <<std::endl;
				delete W33[i];
			}
			//std::cout<<"];"<<std::endl;
			free(W33);
			if ( displayTime.getValue() )
			{
				sout<<" GAUSS_SEIDEL iterations  " << ( (double) timer->getTime() - time)*timeScale<<" ms" <<sendl;

			}			
			
			
			return 1;
		}
	}
	free(d);
	for (int i = 0; i < numContacts; i++)
		delete W33[i];
	free(W33);
	if ( displayTime.getValue() )
	{
		sout<<" GAUSS_SEIDEL iterations  " << ( (double) timer->getTime() - time)*timeScale<<" ms" <<sendl;
	}
	
	std::cerr<<"\n No convergence in  unbuilt gaussseidel function : error ="<<error <<" after"<< it<<" iterations"<<std::endl;
	//afficheLCP(dfree,W,f,dim);
	return 0;
	
				


}


SOFA_DECL_CLASS(MasterContactSolver)

int MasterContactSolverClass = core::RegisterObject("Constraint solver")
.add< MasterContactSolver >()
;

} // namespace odesolver

} // namespace component

} // namespace sofa