int new_linearisation(struct jacobian *jac,double hinvGak,int neq,ErrorMsg error_message){ double luparity, *Ax; int i,j,*Ap,*Ai,funcreturn; if(jac->use_sparse==1){ Ap = jac->spJ->Ap; Ai = jac->spJ->Ai; Ax = jac->spJ->Ax; /* Construct jac->spJ->Ax from jac->xjac, the jacobian:*/ for(j=0;j<neq;j++){ for(i=Ap[j];i<Ap[j+1];i++){ if(Ai[i]==j){ /* I'm at the diagonal */ Ax[i] = 1.0-hinvGak*jac->xjac[i]; } else{ Ax[i] = -hinvGak*jac->xjac[i]; } } } /* Matrix constructed... */ if(jac->new_jacobian==_TRUE_){ /*I have a new pattern, and I have not done a LU decomposition since the last jacobian calculation, so I need to do a full sparse LU-decomposition: */ /* Find the sparsity pattern C = J + J':*/ calc_C(jac); /* Calculate the optimal ordering: */ sp_amd(jac->Cp, jac->Ci, neq, jac->cnzmax, jac->Numerical->q,jac->Numerical->wamd); /* if the next line is uncomented, the code uses natural ordering instead of AMD ordering */ /*jac->Numerical->q = NULL;*/ funcreturn = sp_ludcmp(jac->Numerical, jac->spJ, 1e-3); class_test(funcreturn == _FAILURE_,error_message, "Failure in sp_ludcmp. Possibly singular matrix!"); jac->new_jacobian = _FALSE_; } else{ /* I have a repeated pattern, so I can just refactor:*/ sp_refactor(jac->Numerical, jac->spJ); } } else{ /* Normal calculation: */ for(i=1;i<=neq;i++){ for(j=1;j<=neq;j++){ jac->LU[i][j] = - hinvGak * jac->dfdy[i][j]; if(i==j) jac->LU[i][j] +=1.0; } } /*Dense LU decomposition: */ funcreturn = ludcmp(jac->LU,neq,jac->luidx,&luparity,jac->LUw); class_test(funcreturn == _FAILURE_,error_message, "Failure in ludcmp. Possibly singular matrix!"); } return _SUCCESS_; }
MatrixXcf Circuit_Handler::calc_S(){ MatrixXcf S(num_of_nodes,num_of_nodes); MatrixXcf I = MatrixXcf::Identity(num_of_nodes,num_of_nodes); MatrixXcf X = calc_X(); MatrixXcf C = calc_C(); num_of_ports = ports.size(); MatrixXcf temp2(num_of_nodes,num_of_nodes); MatrixXcf temp(num_of_nodes,num_of_nodes); temp = C*X; temp = I - temp; temp2 = temp.inverse(); S = X*temp2; return S; }