e5c2d8fa87069bdb5319941ce9c0c46ab8969bcc
[occt.git] / src / FEmTool / FEmTool_LinearTension.cxx
1 // Created on: 1998-11-06
2 // Created by: Igor FEOKTISTOV
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <FEmTool_ElementsOfRefMatrix.hxx>
19 #include <FEmTool_LinearTension.hxx>
20 #include <math.hxx>
21 #include <math_GaussSetIntegration.hxx>
22 #include <math_IntegerVector.hxx>
23 #include <math_Matrix.hxx>
24 #include <math_Vector.hxx>
25 #include <PLib.hxx>
26 #include <PLib_HermitJacobi.hxx>
27 #include <PLib_JacobiPolynomial.hxx>
28 #include <Standard_ConstructionError.hxx>
29 #include <Standard_DomainError.hxx>
30 #include <Standard_NotImplemented.hxx>
31 #include <Standard_Type.hxx>
32 #include <TColStd_HArray2OfInteger.hxx>
33 #include <TColStd_HArray2OfReal.hxx>
34
35 FEmTool_LinearTension::FEmTool_LinearTension(const Standard_Integer WorkDegree,
36                                              const GeomAbs_Shape ConstraintOrder):
37        RefMatrix(0,WorkDegree,0,WorkDegree)
38        
39 {
40   static Standard_Integer Order = -333, WDeg = 14;
41   static math_Vector MatrixElemts(0, ((WDeg+2)*(WDeg+1))/2 -1 );
42
43   myOrder = PLib::NivConstr(ConstraintOrder);
44
45   if (myOrder != Order) {
46     //Calculating RefMatrix
47     if (WorkDegree > WDeg) Standard_ConstructionError::Raise("Degree too high");
48     Order = myOrder;
49     Standard_Integer DerOrder = 1;
50     Handle(PLib_HermitJacobi) theBase = new PLib_HermitJacobi(WDeg, ConstraintOrder);
51     FEmTool_ElementsOfRefMatrix Elem = FEmTool_ElementsOfRefMatrix(theBase, DerOrder);
52     
53     Standard_Integer maxDegree = WDeg+1;
54     math_IntegerVector Order(1,1,Min(4*(maxDegree/2+1),math::GaussPointsMax()));
55     math_Vector Lower(1,1,-1.), Upper(1,1,1.); 
56     
57     math_GaussSetIntegration anInt(Elem, Lower, Upper, Order);
58     MatrixElemts = anInt.Value();
59   }
60
61   Standard_Integer i, j, ii, jj;
62   for(ii = i = 0; i <= WorkDegree; i++) {
63     RefMatrix(i, i) =  MatrixElemts(ii);
64     for(j = i+1, jj = ii+1; j <= WorkDegree; j++, jj++) {
65       RefMatrix(j, i) = RefMatrix(i, j) =  MatrixElemts(jj);
66     }
67     ii += WDeg+1-i;
68   }
69 }
70
71 Handle(TColStd_HArray2OfInteger) FEmTool_LinearTension::DependenceTable() const
72 {
73   if(myCoeff.IsNull()) Standard_DomainError::Raise("FEmTool_LinearTension::DependenceTable");
74
75   Handle(TColStd_HArray2OfInteger) DepTab = 
76     new TColStd_HArray2OfInteger(myCoeff->LowerCol(), myCoeff->UpperCol(),
77                                  myCoeff->LowerCol(), myCoeff->UpperCol(),0);
78   Standard_Integer i;
79   for(i=1; i<=myCoeff->RowLength(); i++) DepTab->SetValue(i,i,1);
80   
81   return DepTab;
82 }
83
84 Standard_Real FEmTool_LinearTension::Value() 
85 {
86   Standard_Integer deg = Min(myCoeff->ColLength() - 1, RefMatrix.UpperRow()), 
87                    i, j, j0 = myCoeff->LowerRow(), degH = Min(2*myOrder+1, deg),
88                    NbDim = myCoeff->RowLength(), dim;
89
90   TColStd_Array2OfReal NewCoeff( 1, NbDim, 0, deg);
91
92   Standard_Real coeff = (myLast - myFirst)/2., cteh3 = 2./coeff, 
93                 mfact, Jline;
94
95   Standard_Integer k1;
96
97
98   Standard_Real J = 0.;
99
100   for(i = 0; i <= degH; i++) {
101     k1 = (i <= myOrder)? i : i - myOrder - 1;
102     mfact = Pow(coeff,k1);
103     for(dim = 1; dim <= NbDim; dim++) 
104       NewCoeff(dim, i) = myCoeff->Value(j0 + i, dim) * mfact;
105   }
106
107   for(i = degH + 1; i <= deg; i++) {
108     for(dim = 1; dim <= NbDim; dim++) 
109       NewCoeff(dim, i) = myCoeff->Value(j0 + i, dim);
110   }
111
112   for(dim = 1; dim <= NbDim; dim++) {
113   
114     for(i = 0; i <= deg; i++) {
115
116       Jline = 0.5 * RefMatrix(i, i) * NewCoeff(dim, i);
117
118       for(j = 0; j < i; j++) 
119         Jline += RefMatrix(i, j) * NewCoeff(dim, j);
120       
121       J += Jline * NewCoeff(dim, i);
122
123     }    
124
125   }
126
127   return cteh3*J;
128
129
130 }
131
132 void FEmTool_LinearTension::Hessian(const Standard_Integer Dimension1,
133                                     const Standard_Integer Dimension2, math_Matrix& H) 
134 {
135  
136   Handle(TColStd_HArray2OfInteger) DepTab = DependenceTable();
137
138   if(Dimension1 < DepTab->LowerRow() || Dimension1 > DepTab->UpperRow() || 
139      Dimension2 < DepTab->LowerCol() || Dimension2 > DepTab->UpperCol()) 
140     Standard_OutOfRange::Raise("FEmTool_LinearTension::Hessian");
141
142   if(DepTab->Value(Dimension1,Dimension2) == 0) 
143     Standard_DomainError::Raise("FEmTool_LinearTension::Hessian");
144
145   Standard_Integer deg = Min(RefMatrix.UpperRow(), H.RowNumber() - 1), degH = Min(2*myOrder+1, deg);
146
147   Standard_Real coeff = (myLast - myFirst)/2., cteh3 = 2./coeff, mfact;
148   Standard_Integer k1, k2, i, j, i0 = H.LowerRow(), j0 = H.LowerCol(), i1, j1;
149
150   H.Init(0.);
151
152   i1 = i0;
153   for(i = 0; i <= degH; i++) {
154     k1 = (i <= myOrder)? i : i - myOrder - 1;
155     mfact = Pow(coeff,k1)*cteh3;
156     // Hermite*Hermite part of matrix
157     j1 = j0 + i;
158     for(j = i; j <= degH; j++) {
159       k2 = (j <= myOrder)? j : j - myOrder - 1;
160       H(i1, j1) = mfact*Pow(coeff, k2)*RefMatrix(i, j);
161       if (i != j) H(j1, i1) = H(i1, j1);
162       j1++;
163     }
164     // Hermite*Jacobi part of matrix
165     j1 = j0 + degH + 1;
166     for(j = degH + 1; j <= deg; j++) {
167       H(i1, j1) = mfact*RefMatrix(i, j);
168       H(j1, i1) = H(i1, j1);
169       j1++;
170     }
171     i1++;
172   }
173
174
175   // Jacoby*Jacobi part of matrix
176   i1 = i0 + degH + 1;
177   for(i = degH+1; i <= deg; i++) {
178     j1 = j0 + i;
179     for(j = i; j <= deg; j++) {
180       H(i1, j1) = cteh3*RefMatrix(i, j);
181       if (i != j) H(j1, i1) = H(i1, j1);
182       j1++;
183     }
184     i1++;
185   }
186
187 }
188
189  void FEmTool_LinearTension::Gradient(const Standard_Integer Dimension, math_Vector& G) 
190 {
191   if(Dimension < myCoeff->LowerCol() || Dimension > myCoeff->UpperCol()) 
192     Standard_OutOfRange::Raise("FEmTool_LinearTension::Gradient");
193
194   Standard_Integer deg = Min(G.Length() - 1, myCoeff->ColLength() - 1);
195
196   math_Vector X(0,deg);
197   Standard_Integer i, i1 = myCoeff->LowerRow();
198   for(i = 0; i <= deg; i++) X(i) = myCoeff->Value(i1+i, Dimension);
199
200   math_Matrix H(0,deg,0,deg);
201   Hessian(Dimension, Dimension, H);
202   
203   G.Multiply(H, X);
204
205
206 }