#% LaheTrussDFI.m
function AlgParm=LaheTrussDFI(baasi0,Ntoerkts,sSolmF,tsolm,tSiire,krdn,selem)
##
## 
##disp('==================================================================')
##disp(' LaheBeamDFI.m     ')
##disp(' The compiler of a basic-, compatibility-, joint equilibrium-, ')
##disp(' side conditions- and support restrtictions sparse equations.  ')
##disp(' Solving system of sparse equations. Find the initial parmeter vectors ')
##disp(' for elements displacements, forces and the support reactions.  ')
##disp(' OUTPUT: AlgParm -- the initial parmeter vector for elements. ')
## 
## GNU octave version >= 3.0.x
##=========================================================================
## This Program is written by Andres Lahe,   2010-07-16
##                    e-mail: andres.lahe@ttu.ee
## LAST MODIFIED: Andres Lahe,   2013-07-21
## Copyright (c)  2010 by Tallinn University of Technology 
##                Department of Mechanics
##                http://www.ttu.ee/
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.

## This program 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 General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
## http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
##=========================================================================
##
##
## baasi0 - scaling multiplier for the displacements,
## Ntoerkt - the number of the support reactions,
## sSolmF - the node forces in global  coordinates
##          sSolmF(2,1,SolmedeArv),
## tsolm - the restrtictions on the support displacements 
## tSiire - the support shift 
##          tSiire(2,1,SolmedeArv)
## krdn - the nodal coordinates
## selem - the topology of elements 
##         selem(ElementideArv,1:16)
##
#if nargin != 7
if ~(nargin==7)
error(' function LaheBeamDFI have wrong number of input arguments!') 
end
#
 global spA
 global siireVardaA
 global SiireJoud
 global X  
# global SpTUv
# global SpTWv
#disp('---  ') 
#%selem=[selemjl(:,1:23)];
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%==============
%%jaotT=L/4;
%==============
EARV=size(selem);
NEARV=EARV(1,1);
SARV=size(krdn);
NSARV=SARV(1,1);
NNK=8*NEARV+Ntoerkts;
#========== 
#==========
TSARV=size(tsolm);
NTSARV=TSARV(1,1);
#==========
##%%%%%%%%%%%
ToeSidemeteArv=0;
for i=1:NTSARV
sJrN(i)=i;
  for j=2:3
     if (tsolm(i,j) == 1)
       ToeSidemeteArv=ToeSidemeteArv+1;
     endif 
  endfor
endfor
#
#%%%%%%%%%%%%%%%%%%%%%%
selemjl=selem;
#1
selem;
lvarras=VardaPikkusTr(NSARV,NEARV,krdn,selem);
#lvarras
disp('   ')
%%NNK=8*NEARV+Ntoerkts;
#%%%%%%%%%%%
#
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#
#  No;  daf''s at the end:  u, w, fi, N; Node number; moment hinge.
#disp(' No;  daf''s at the end:  u, w, fi, N; Node number; moment hinge ')
#
for i=1:NEARV
selemlopp(i,1)=i;
selemlopp(i,2:5)=selemjl(i,1:4);
selemlopp(i,6)=[selemjl(i,9)];
#selemlopp(i,7:8)=[selemjl(i,13:14)];
endfor
#
#selemlopp
#%%%%%%%%%%%
# No;  daf''s at the beginning:  u, w, fi, N; Node number; moment hinge.
#disp(' No;  daf''s at the beginning:  u, w, fi, N; Node number; moment hinge. ')
#
for i=1:NEARV
selemaluses(i,1)=i;
selemaluses(i,2:5)=selemjl(i,5:8);
selemaluses(i,6)=[selemjl(i,10)];
#selemaluses(i,7:8)=[selemjl(i,15:16)];
endfor
#
%selemaluses
#%%%
%elemvabsolm
for i=1:NEARV
elemvabsolm(i,1:6)=selemlopp(i,1:6);
endfor
#
for i=1:NEARV
elemvabsolm(NEARV+i,1:6)=selemaluses(i,1:6);
endfor
#%%%
elemvabsolm;
AB=sortrows(elemvabsolm(:,6));
#AB;
#
##[~,idx] = sortrows(elemvabsolm(:,6));
[tmp,idx] = sortrows(elemvabsolm(:,6));
ABB=elemvabsolm(idx,:);
disp('============================================================= ')
disp('Element number; DaF numbers; Node number')
disp('------------------------------------------------------------- ')
disp('Element    u     w    fi     N    Node       ')
disp('------------------------------------------------------------- ')
ABB=ABB
#--------- Define a matrix spA to be sparse -----------
#--------- Define a matrix spA to be sparse -----------
disp('-------- Sparse matrix instantiation --------  ')
disp('  spA=sparse(NNK,NNK)  ')
spA=sparse(NNK,NNK) % nullistan võrrandisüsteemi kordajate maatriksi
#--------- Define a vector B ------------
disp('-------- Right-hand side of the equations (RHS). --------  ')
disp('  B=zeros(NNK,1);  ')
B=zeros(NNK,1);   % nullistan võrrandisüsteemi vabaliikmed
disp('   ')
%%%%%%%%%%%%%%%%%%%%%%%%%

#------------ The basic equations of frame ----------------
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
disp('----- Writing basic equations of frame with the transfer matrix ----  ')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
#  http://digi.lib.ttu.ee/opik_eme/Ehitusmehaanika.pdf#page=397   page=693
# W. D. Pilkey, W. Wunderlich. Mechanics of Structures. Variational and 
# Computational Methods. CRC Press, Boca Raton, Ann Arbor, London, Tokyo, 1994.
# 4.3.1 Transfer Matrix (the basic equations) p. 174
#
IIv=0;
IJv=0;
#%%%%%%%%%%%%%%%
krda=0;
for i=1:NEARV
krda=krda+1;
%
EA=selem(i,11);    ## MIS SEE ON
Li=lvarras(i,1);
spvFs=yspSRmhvI(baasi0,Li,EA);
vBs=zeros(4,1); % varda koormus puudub
%%vB=vBs;
IIv=krda*4-3;
IJv=krda*8-7;
spA=spInsertBtoA(spA,IIv,IJv,spvFs);
B=InsertBtoA(B,NNK,1,IIv,1,vBs,4,1);
%
endfor
#
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%spA
vorrandeidB=size(spA);
rows_of_basic_equations=vorrandeidB(1,1);
cols_of_basic_equations=vorrandeidB(1,2);
non_zero_elements_in_the_basic_equations=nnz(spA);

spRidu=vorrandeidB(1,1);
pohivorrandiXe=vorrandeidB(1,2);
disp('  ')
disp('--------The basic equations are inserted into spA  --------  ')
spA_rows=sprintf('rows_of_basic_equations: %d', spRidu)
spA_col=sprintf('cols_of_basic_equations: %d', pohivorrandiXe)
spA_nnz=sprintf('non_zero_elements_in_the_basic_equations: %d', nnz(spA))
nnzB=spA_nnz;
disp('  ')
Nr1=spRidu+1;
#
#disp('  ')
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#
  VarrasteArv=NEARV;
  SolmedeArv=NSARV;
#%%%%
    Vord='=';
    Kom=',' ;  
    Semi=';';
   #Tyhk='  ';     
 Arv1=num2str(1);
 Arv2=num2str(2);
 Arv3=num2str(3);    
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
disp(' Compatibility equations of displacements at nodes ')
From_row=sprintf('Compatibility equations begin from row: %d', Nr1)
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
#
for i=1:NSARV
#disp('---------------------------------------------------------')
Solm=i;
Node=Solm
AVS=VardadSolmesTr(NSARV,NEARV,Solm,AB,ABB);
#AVS
#
#disp('---------------------------------------------------------') 
#AVSMN
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
NEARV2=NEARV*2;
#
SpTM2x2=sparse(2,2);
SpTM2x2m=sparse(2,2);
#SpTM2x2xz=sparse(2,2);
#
AVSdim=size(AVS) ;
AVSMN=AVSdim(1,1);
#if AVSdim(1,1) > 1

    for j=2:AVSMN
    
       Varras1=AVS(1,1);   
       VarrasN=AVS(j,1);
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#
   str1=num2str(Nr1) ;
#   AVS(1,6);
#   AVS(j,6);
#   AVSsum=AVS(1,6)+ AVS(j,6);
#
      SpTM2x2=SpTeisendusMaatriksTr2x2(NSARV,NEARV,Varras1,krdn,selem);
#      Varras1
#      SpTM2x2
      str1=num2str(Nr1);    
      TAVS2=eval(sprintf('%3i',AVS(1,2)));  
      TVS2=int2str(TAVS2);
      str2=TVS2;    
      str=['spA=spInsertBtoA(spA',Kom, str1,Kom, str2,Kom, 'SpTM2x2',')',Semi];
      cmd = sprintf(str)
      eval(cmd); 
      SpTM2x2=SpTeisendusMaatriksTr2x2(NSARV,NEARV,VarrasN,krdn,selem);
#      VarrasN
      SpTM2x2m=-SpTM2x2; 
      TAVS2=eval(sprintf('%3i',AVS(j,2)));  
      TVS2=int2str(TAVS2);
      str2=TVS2;
      str=['spA=spInsertBtoA(spA',Kom, str1,Kom, str2,Kom, 'SpTM2x2m',')',Semi];
      cmd = sprintf(str)
      eval(cmd);        
      Nr1=Nr1+2; 
# 
  endfor  #% for j=2:AVSMN                  
# endif #%if AVSdim(1,1) > 1                                       
endfor  #% for i=1:NSARV
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('----- ')
vorrandeidD=size(spA);
#
disp(' ')
spA_rows=vorrandeidD(1,1)
spA_rowsD=spA_rows;
spA_cols=vorrandeidD(1,2)
#
nnzD=nnz(spA);
spA_nnz=sprintf('non_zero_elements_in_spA: %d', nnz(spA))
disp('  ')
disp('-----Compatibility equations of displacements are inserted into spA  ----  ')
disp('  ')
compatibility_equations_rows=spA_rowsD-rows_of_basic_equations
non_zero_elements_in_compatibility_equations= nnzD-non_zero_elements_in_the_basic_equations
#nnzB=nnz(spA)
disp('  ')
#%
#%pohivorrandiXe
#
toemuutuja=pohivorrandiXe+1;
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
disp(' Joint equilibrium equations at nodes ')
From_rows=sprintf('Joint equilibrium equations begin from row: %d', Nr1)
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')

str1=num2str(Nr1); 
strSolm=num2str(Solm);
koolonV=':';
#
tsolmLaiend=zeros(NSARV,4);
  for i=1:NSARV
      tsolmLaiend(i,1)=i; 
    for j=1:NTSARV;
     if (tsolmLaiend(i,1)-tsolm(j,1)) == 0
      tsolmLaiend(i,2:4)=tsolm(j,2:4);
     endif 
    endfor
 endfor
# tsolm
# tsolmLaiend
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:NSARV  % sõlmede arv
#     uvfiLaiend(i)=tsolmLaiend(i,2)+tsolmLaiend(i,3)+tsolmLaiend(i,4);
  disp('---------------------------------------------------------')
  Solm=i;
  Node=Solm
  AVS=VardadSolmesTr(NSARV,NEARV,Solm,AB,ABB);
  uvfiLaiend(Node,1)=tsolmLaiend(Node,2)+tsolmLaiend(Node,3)+tsolmLaiend(Node,4);
#
disp('---------------------------------------------------------') 
  vorrandeid=size(spA);
  spRidu=vorrandeid(1,1); 
  Nr1=spRidu+1;
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  NEARV2=NEARV*2;
SpTM2x2=sparse(2,2);
SpTM2x2m=sparse(2,2);
SpTM2x2xz=sparse(2,2);
uvfi=0;
#  uvfi=tsolm(k,2)+tsolm(k,3)+tsolm(k,4); # uvfi - the number of reactions
#
  AVSdim=size(AVS); 
  AVSMN=AVSdim(1,1);
#
#toemuutujaS=toemuutuja-1
#toemuutujaid=toemuutuja-pohivorrandiXe-1
#
if AVSMN > 1
k=1;
taskVorreid(1,k)=0;
             
    for j=1:AVSMN   
       VarrasN=AVS(j,1);
       str1=num2str(Nr1) ;
       AVS(1,6);
       AVS(j,6);
       AVStas= AVS(j,6); 
#
##    if AVStas == 0
#       
##    else  
## spInsertBtoAvect
#SpTeisendusMaatriksTr2x1(NSARV,NEARV,VarrasN,krdn,selem)
      SpTM2x2=SpTeisendusMaatriksTr2x1(NSARV,NEARV,VarrasN,krdn,selem);
#      SpTM2x2=-SpTM2x2;
# 
      str1=num2str(Nr1);    
      TAVS2=eval(sprintf('%3i',AVS(j,5)));  
      TVS2=int2str(TAVS2);
      str2=TVS2;    
      str=['spA=spInsertBtoAvect(spA',Kom, str1,Kom, str2,Kom, 'SpTM2x2',')',Semi];
      cmd = sprintf(str)
      eval(cmd); 
      
      k=k+1;
      taskVorreid(1,k)=2 ;     

      strB1=str1;
      NrB2=Nr1+1;
      strB2=num2str(NrB2);
      Arv123=Arv2;
      ontugesid=0;
  Kas_on_toe_solm=tsolmLaiend(Node,2)+tsolmLaiend(Node,3)+tsolmLaiend(Node,4);
#tsolmLaiend  
#Kas_on_toe_solm
  if  (Kas_on_toe_solm != 0)
         if  (uvfiLaiend(Node)-3) == 0  
         ontugesid=3; 
        endif
         TSMLD1=tsolmLaiend(Node,2)+tsolmLaiend(Node,3);
        if (TSMLD1 - 2) == 0
          ontugesid=2; 
        endif 
      if (TSMLD1 - 1) == 0        
        if ((tsolmLaiend(Node,2)) - 1) == 0
          ontugesid=1;
        endif  
        if ((tsolmLaiend(Node,3))- 1) == 0
          ontugesid=1;
#          ontugesid=11;
        endif  
       endif 
  endif     
#      NNK
## endif
 #
endfor  %% for j=2:AVSMN 
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (ontugesid != 0)
 switch (ontugesid);
case{1}
#               SpTM1x0xz=SpTeisendusUhikMaatriks1x0v(1);
               SpTM1x0xz=SpTeisendusUhikMaatriks0x1v(1);               
               SpTM1x0xz=-SpTM1x0xz;
               TAVS2=toemuutuja;
               TVS2=int2str(TAVS2);
               str2=TVS2;    
               str=['spA=spInsertBtoAvect(spA',Kom, str1,Kom, str2,Kom, 'SpTM1x0xz',')',Semi];
               cmd = sprintf(str) 
               eval(cmd)
               Nr1=Nr1+1;
#              taskVorreid(1,k)=2 ;
               toemuutuja=toemuutuja+1;

#
#%%%%%%%%%%%
case{2}
               SpTM2x2xz=SpTeisendusUhikMaatriks2x2(1);
               SpTM2x2xz=-SpTM2x2xz;
               TAVS2=toemuutuja;
               TVS2=int2str(TAVS2);
               str2=TVS2;    
               str=['spA=spInsertBtoA(spA',Kom, str1,Kom, str2,Kom, 'SpTM2x2xz',')',Semi];
               cmd = sprintf(str) 
               eval(cmd)
               Nr1=Nr1+2;
#              taskVorreid(1,k)=2 ;
               toemuutuja=toemuutuja+2;

#
#%%%%%%%%%%%
case{3}
#
#
#%%%%%%%%%%%
case{4}
#
#%%%%%%%%%%%
case{5}
#
#%%%%%%%%%%%
case{6}
#
#%%%%%%%%%%%
case{7}
#
#%%%%%%%%%%%
case{8}
#
#%%%%%%%%%%%
case{9}
#
#%%%%%%%%%%%
case{10}
#
#%%%%%%%%%%%
case{11}
#disp(' 467   ')
#              SpTM0x1xz=SpTeisendusUhikMaatriks0x1v(1);
#               SpTM0x1xz=-SpTM0x1xz;
#               TAVS2=toemuutuja;
#               TVS2=int2str(TAVS2);
#               str2=TVS2;    
#               str=['spA=spInsertBtoAvect(spA',Kom, str1,Kom, str2,Kom, 'SpTM0x1xz',')',Semi];
#               cmd = sprintf(str) 
#               eval(cmd)
#               Nr1=Nr1+1;
##              taskVorreid(1,k)=2 ;
#               toemuutuja=toemuutuja+1;
#%%%%%
endswitch
endif  ## ontugesid
#
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   strSolmB=num2str(Solm); 
    disp('      Nodal forces at the node')
      strB=['B(',strB1, koolonV, strB2, Kom,Arv1,')','=','sSolmF(', Arv1, koolonV, Arv123, Kom, strSolmB,')',Semi];
      cmd = sprintf(strB)
      eval(cmd) 
  
 maxtaskVorreid=max(taskVorreid(1,k)); 
# 
  vorrandeid=size(spA);
spRidu=vorrandeid(1,1); 
Nr1=spRidu+1;
  
 endif #%if AVSdim(1,1) > 1                                       
endfor  #% for i=1:NSARV
#
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
vorrandeid=size(spA);
spRidu=vorrandeid(1,1);
disp('----- ')
vorrandeidE=size(spA);
disp(' ')
spA_rows=vorrandeidE(1,1)
spA_rowsE=spA_rows;
spA_cols=vorrandeidE(1,2)
nnzE=nnz(spA);
spA_nnz=sprintf('non_zero_elements_in_spA: %d', nnz(spA))
disp('  ')
disp('-----The equilibrium equations are inserted into spA  ----  ')
disp('  ')
equilibrium_equations_rows=spA_rowsE-spA_rowsD
non_zero_elements_in_equilibrium_equations= nnzE-nnzD
%nnzE=nnz(spA)
disp('  ')
vorrandeid=size(spA);
spRidu=vorrandeid(1,1); 
Nr1=spRidu+1;
ABB=ABB;
ABBdim=size(ABB) ;
#
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
disp(' Side conditions (hinges) ')
From_rows=sprintf('Side conditions begin from row: %d', Nr1)
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
#
#for i=1:ABBdim(1,1)
## 
#    if ABB(i,11) == 1
#      str1=num2str(Nr1); 
#      Arv1=num2str(1);
#      TAVS2=eval(sprintf('%3i',ABB(i,7)));  
#      TVS2=int2str(TAVS2);
#      str2=TVS2;    
#      str=['spA=spSisestaArv(spA',Kom, str1,Kom, str2, Kom, Arv1,')',Semi];
# 
#      cmd = sprintf(str)
#      eval(cmd); 
#      Nr1=Nr1+1  ;  
#    endif
#endfor
#Nr1=Nr1-1;
#
disp('----- ')

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
vorrandeidN=size(spA);
#
disp(' ')
spA_rows=vorrandeidN(1,1)
spA_rowsN=spA_rows;
spA_cols=vorrandeidN(1,2)
#
nnzN=nnz(spA);
spA_nnz=sprintf('non_zero_elements_in_spA: %d', nnz(spA))
disp('  ')
disp('-----Side conditions are inserted into spA  ----  ')
disp('  ')
side_condition_rows=spA_rowsN-spA_rowsE
non_zero_elements_in_side_condition= nnzN-nnzE
#nnzN=nnz(spA)
disp('  ')
#%
#disp('----- ')
#
vorrandeid=size(spA);
spRidu=vorrandeid(1,1); 
Nr1=spRidu+1;   
#
  ABB;
  tsolm=tsolm;
  TSARV=size(tsolm);
  NTSARV=TSARV(1,1);
toereaktsioonid_algavad=Nr1;  
# 
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
disp(' Restrtictions on support displacements ')
From_rows=sprintf('Restrtictions on support displacements begin from row: %d', Nr1)
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
#
 toesiirdedN=1;
TSARV=size(tsolm);
NTSARV=TSARV(1,1);
ABBMoot=ABBdim(1,1);
ToeSidemeteArv=0;
ToeSiireRealNR=toereaktsioonid_algavad-1;

for i=1:NTSARV
tsolNr =tsolm(i,1);
  for j=2:4
    if (tsolm(i,j) == 1)
       ToeSidemeteArv=ToeSidemeteArv+1;
   endif    
  endfor
endfor
ToeSidemeteArv;
disp('   ')
ToeSidemed=zeros(ToeSidemeteArv,6);
ToeSolmes=zeros(NTSARV,8);
%tsolm;
ToeSolmes(:,1:4)=tsolm(:,1:4);

for i=1:NTSARV
tsolNr =tsolm(i,1);
LeidsinVarda=0;
  for j=1:ABBMoot
  if (LeidsinVarda == 0)
###     if (tsolNr-ABB(j,8)) == 0   
    if (tsolNr-ABB(j,6)) == 0   
      ToeSolmes(i,5:8)=ABB(j,1:4); 
      LeidsinVarda=1;
     endif   
   endif     
  endfor
endfor
#%%%%%%%%%
#ToeSolmes;
#tsolm;
#%%%%%%%%
nmr=0;
  for j=1:NTSARV
    for k=2:4
#    for k=2:3    
        KusOnYks=tsolm(j,k);
      if (KusOnYks-1) == 0 
           nmr=nmr+1;
        ToeSiireRealNR = ToeSiireRealNR +1; 
        ToeSidemed(nmr,1) = ToeSiireRealNR;
        ToeSidemed(nmr,2) = ToeSolmes(j,5);
        ToeSidemed(nmr,3) = k-1;
        ToeSidemed(nmr,4:6) = ToeSolmes(j,6:8);    
        ToeSidemed(nmr,7) = ToeSolmes(j,1);
       endif       
     endfor
  endfor
#%%%%%%%%%%%%%
#ToeSidemed;
#%%%%%%%%%%%%%
 for i=1:ToeSidemeteArv   
     Nr11=ToeSidemed(i,1); 
      str1=num2str(Nr11); 
      Arv1=num2str(1);
 UWFi=ToeSidemed(i,3); 
 SpTUv=sparse(1,3);
 SpTWv=sparse(1,3);
 SpTFiV=sparse(1,3);
#
 switch (UWFi);
case{1}  

VarrasS=ToeSidemed(i,2);
SpTUv=SpToeSiirdeUvektorTr(NSARV,NEARV,VarrasS,krdn,selem);
%[iiB, jjB, aaB]=find(SpTUv);
TAVS=ToeSidemed(i,4);
               TVS2=int2str(TAVS);
               str2=TVS2 ;   
 
% spA=spInsertBtoAvect(spA,139,121,SpTUv)
% size(spA)
               str=['spA=spInsertBtoAvect(spA',Kom, str1,Kom, str2,Kom, 'SpTUv',')',Semi];            
               cmd = sprintf(str)            
               eval(cmd)  
#
    strB1=str1;
    SolmB=ToeSidemed(i,7);
    strSolmB=num2str(SolmB);
    ToeSidemed(1,1);
    Arv1x=num2str(1); 
%        shift1=tSiire(:,1,SolmB)'
    shift1=tSiire(1,1,SolmB);
    strShift1=num2str(shift1);
%    disp('  ')
     strTxt1=['The support shift  '  'tSiire(' Arv1x Kom Arv1 Kom strSolmB ')' ' = ' strShift1 '  in x direction at the node ' strSolmB ]; 
%    disp(strTxt1)
      strB=['B(',strB1, Kom,Arv1,')','=','tSiire(', Arv1x, Kom, Arv1, Kom, strSolmB,')',Semi];
      cmd = sprintf(strB)
      eval(cmd)  
     disp('  ')                
case{2}

VarrasS=ToeSidemed(i,2);
SpTWv=SpToeSiirdeWvektorTr(NSARV,NEARV,VarrasS,krdn,selem);
##[iiB, jjB, aaB]=find(SpTWv)
##TAVS=ToeSidemed(i,5);
TAVS=ToeSidemed(i,4);
               TVS2=int2str(TAVS);
               str2=TVS2;    
               
               str=['spA=spInsertBtoAvect(spA',Kom, str1,Kom, str2,Kom, 'SpTWv',')',Semi];
               cmd = sprintf(str)
               eval(cmd)
#
    strB1=str1;
    SolmB=ToeSidemed(i,7);
    strSolmB=num2str(SolmB);
    ToeSidemed(1,1);
    Arv1z=num2str(2);
    shift2=tSiire(2,1,SolmB);
%     shift2=tSiire(:,1,SolmB)'   
    strShift2=num2str(shift2);
%    disp('  ')
    strTxt2=['The support shift  '  'tSiire(' Arv1z Kom Arv1 Kom strSolmB ')' ' = ' strShift2 '  in z direction at the node ' strSolmB ]; 
    disp(strTxt2)
      strB=['B(',strB1, Kom,Arv1,')','=','tSiire(', Arv1z, Kom, Arv1, Kom, strSolmB,')',Semi];
       cmd = sprintf(strB)
       eval(cmd)  
    disp('  ') 
#spADim=size(spA) 
#vorrandeid=size(spA)
 # spA
#case{3}
#spA=spInsertBtoAvect(spA,139,121,SpTUv)
#VarrasS=ToeSidemed(i,2);
#SpTFiV=SpToeSiirdeFiVektorTr(1)
#%TAVS=ToeSidemed(i,6);
#TAVS=ToeSidemed(i,4);
#               TVS2=int2str(TAVS);
#               str2=TVS2;    
#               
#               str=['spA=spInsertBtoA(spA',Kom, str1,Kom, str2,Kom, 'SpTFiV',')',Semi];
#               cmd = sprintf(str)
#               eval(cmd)
##
#    strB1=str1;
#    SolmB=ToeSidemed(i,7);
#    strSolmB=num2str(SolmB);
#    ToeSidemed(1,1);
#    Arv1y=num2str(3);
#    shift3=tSiire(3,1,SolmB);
#    strShift3=num2str(shift3);
#%    disp('  ')
#    strTxt3=['The support shift  '  'tSiire(' Arv1y Kom Arv1 Kom strSolmB ')' ' = ' strShift3 '  in y direction at the node ' strSolmB ]; 
#    disp(strTxt3)
#      strB=['B(',strB1, Kom,Arv1,')','=','tSiire(', Arv1y, Kom, Arv1, Kom, strSolmB,')',Semi];
#      cmd = sprintf(strB)
#      eval(cmd)  
#    disp('  ')                 
#%%%%%
endswitch  
# 
 endfor # for ToeSidemeteArv

disp(' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
#
vorrandeid=size(spA);
spRidu=vorrandeid(1,1);   
#
disp('----- ')
vorrandeidR=size(spA);
#
disp(' ')
spA_rows=vorrandeidR(1,1)
spA_rowsR=spA_rows;
spA_cols=vorrandeidR(1,2)
#
nnzR=nnz(spA);
spA_nnz=sprintf('non_zero_elements_in_spA: %d', nnz(spA))
disp('  ')
disp('-----Restrtictions equations are inserted into spA  ----  ')
disp('  ')
restrtictions_equations_rows=spA_rowsR-spA_rowsN
non_zero_elements_in_restrtictions_equations= nnzR-nnzN
#
disp('  ')
#
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
#
spA_rank = sprank(spA)
#if spA_rank != NNK
if ~(spA_rank==NNK)
#error(' spA is rank deficient. Rank must be NNK (NNK is the number of unknowns)!') 
end
#
 spA
#
B;
BsuurusS=size(B);
Bsuurus=BsuurusS(1,1);

#
disp(' Nr    B ')
disp('  ')
for i=1:Bsuurus
###  %9.3e %9.3f
vorrandiNr=i;
disp(sprintf('%3i %7.3f',vorrandiNr,B(i,1)));
endfor
disp('  ')
# 
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
disp(' Solving system of sparse equations. ')
disp(' X=spA\B; ')
#Support_reactions=sprintf('are the last rows: %d', restrtictions_equations_rows)
Support_reactions=sprintf('Support reactions begin from X row: %d', toereaktsioonid_algavad)
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
#
%%%%%%%%%%%%%%%%%%%%
X=spA\B; % lahend  võrrandisõteemist spA*X=B
%%%%%%%%%%%%%%%%%%%%%
%
%
disp(' Nr     X ')
disp('  ')
for i=1:Bsuurus
##%  %9.3e   %9.3f
vorrandiNr=i;
disp(sprintf('%3i   %9.3e',vorrandiNr,X(i,1)))
endfor
%%%%%%%%%%%%%%%%%%%%%%
for i=1:NEARV
elemendiN(i,1)=i;
siireVardaA(i,1:3)=selemjl(i,5:7);
siireVardaL(i,1:3)=selemjl(i,1:3);
joudVardaA(i,1)=selemjl(i,8);
joudVardaL(i,1)=selemjl(i,4);
endfor
#
#siireVardaA
#siireVardaL
#joudVardaA
#joudVardaL
#%%%%%%%%%%%%
for i=1:NEARV
JrN(i,1)=i;
endfor
#
JrNT=JrN';
#
disp('=======================================================')
disp('     Support displacements/(Cz, CMy) at nodes  ') 
disp('   X_No,  Element_No,   u/w_Nr,  u_No,  w_No,  node_No ')
disp('-------------------------------------------------------')
for i=1:ToeSidemeteArv
disp(sprintf('     %3i     %3i         %3i        %3i    %3i     %3i  '  ,ToeSidemed(i,1),    ToeSidemed(i,2), ToeSidemed(i,3), ToeSidemed(i,4), ToeSidemed(i,5), ToeSidemed(i,6)))
endfor
disp('-------------------------------------------------------')
#
#The_displacement_DaF-s_numbers_at_beginning_of_the_element=sprintf('elementNr, displacement: %d',JrN(i,1) siireVardaA)
disp('==================================================================')
disp(' Displacements and force numbers at beginning of the element  ') 
disp('   No,      u,     w,    fi,    N  ')
disp('------------------------------------------------------------------')
for i=1:NEARV
disp(sprintf('  %3i     %3i   %3i     %3i   %3i  '  ,JrN(i,1),    siireVardaA(i,1), siireVardaA(i,2), siireVardaA(i,3), joudVardaA(i,1)))
endfor
%
disp('-------------------------------------------------------')
disp(' ')
disp('==================================================================')
disp(' Displacements and forces numbers at end of the element  ') 
disp('   No,      u,     w,    fi,    N  ')
disp('------------------------------------------------------------------')
for i=1:NEARV
disp(sprintf('  %3i     %3i   %3i     %3i   %3i  '  ,JrN(i,1),    siireVardaL(i,1), siireVardaL(i,2), siireVardaL(i,3), joudVardaL(i,1)))
endfor
#
disp('-------------------------------------------------------')
disp(' ')
#  
#
#disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
#disp(' Initial parmeter vectors for elements displacements and forces  ')
#disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
#disp(' ')
#disp('---- Descaling multiplier for displacements = 1/baasi0 ------  ')
#disp(' ')

ToeSidemed;

%%%%%%%%%%%%%%%%%%%%%
%Algparameetrid
%
for i=1:1:NEARV
%%siireVardaA(i,1:3)
AlgPar(i,1:4)=[X(siireVardaA(i,1),1)/baasi0  X(siireVardaA(i,2),1)/baasi0 X(siireVardaA(i,3),1)/baasi0 ...
 X(joudVardaA(i,1),1)];
endfor
%
disp('=============================================================')
disp('  Unscaled initial parmeter vector (Sign Convention 2)   ')
disp('Element No    u          w          fi             N      ')
disp('--------------------------------------------------------------')
for i=1:NEARV
disp(sprintf('  %3i     %9.3e   %9.3e   %9.3e    %9.3f',JrN(i,1),  AlgPar(i,1),   AlgPar(i,2),   AlgPar(i,3),  AlgPar(i,4)))
%AlgPar
endfor

disp('---------------------------------------------------------------')
%
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
##disp('Support reactions, toereaktsioonid_algavad  ')
Support_reactions=sprintf('Support reactions begin from row X: %d', toereaktsioonid_algavad)
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ')
disp('  ')
#ToeSidemed 6 ja 3
disp('=======================================================')
disp(' No         X          Node   (Cx <=> 1 / Cz <=> 2) ')
disp('-------------------------------------------------------')
disp('  ')
for i=toereaktsioonid_algavad:Bsuurus
#  %9.3e   %9.3f
vorrandiNr=i;  %%9.3f
disp(sprintf('%3i   %+12.6e    %3i            %3i ',vorrandiNr,X(i,1), ToeSidemed(i-toereaktsioonid_algavad+1,7),ToeSidemed(i-toereaktsioonid_algavad+1,3) ))
endfor
#
disp('---------------------------------------------------------')
disp('  ')

AlgParm=AlgPar;

JJ=0;
for i=1:NEARV
%siireVardaL(i,1:3)
JJ=JJ+1;
SiireJoud(JJ,1:4)=[X(siireVardaA(i,1),1)/baasi0  X(siireVardaA(i,2),1)/baasi0  X(siireVardaA(i,3),1)/baasi0 ...
-X(joudVardaA(i,1),1)];
JJ=JJ+1;
SiireJoud(JJ,1:4)=[X(siireVardaL(i,1),1)/baasi0  X(siireVardaL(i,2),1)/baasi0  X(siireVardaL(i,3),1)/baasi0 ...
X(joudVardaL(i,1),1)];
endfor
%


#
endfunction

