% ODEZVA SYSTEMU NA KONCENTRACNI PULS V UZLU 1
% definice topologie a geometrie vetvene site 
% --------------------------------------------
% xi yi zi qi (4 sloupce)
xyz=load('xyzq.txt'); %souradnice uzlu
% nuc-celkovy pocet uzlu, vcetne centralnich uzlu vetveni
nuc = size(xyzq,1);
% conektivita elementu vetveni. 7 sloupcu
% i1 i2 i3       icentr       d1 d2 d3
% koncove uzly   centr.uzel   prumery  uhel vetveni
cv=load('cv.txt');
nev = size(cv,1);
% conektivita elementu trubka. 3 sloupce
% i1 i2           d
% koncove uzly  prumer
cp=load('cp.txt');
nep = size(cp,1);
% sekvence zpracovani elementu
% i 
% index uzlu (v absolutni hodnote). i>0 trubka, i<0 vetveni
seq=load('seq.txt');
nseq = size(seq,1);
%
% prenos do vektoru
x=xyzq(1:end,1);
y=xyzq(1:end,2);
z=xyzq(1:end,3);
q=xyzq(1:end,4);
iuv=cv(:,1:3);
iuc=cv(:,4);
dv=cv(:,5:7);
iup=cp(:,1:2);
dp=cp(:,3);
%
% definice casove skaly
tmax=200;
tpulse=2;
n=2^14;
dt=tmax/(n-1);
for i=1:n
    t(i)=(i-1)*dt;
    if t(i)<tpulse
        c(i,1)=1/tpulse;
    else
        c(i,1)=0;
    end
end
% sekvence elementu
for ie=1:nseq
iel=seq(ie);
if iel>0
% element typu trubka
    i1=iup(iel,1);
    i2=iup(iel,2);
    l=sqrt((x(i1)-x(i2))^2+(y(i1)-y(i2))^2+(z(i1)-z(i2))^2);
    c(:,i2)=convpipe(t,n,dt,c(:,i1),dp(iel),l,q(i1));
else
% element typu vetveni
    iev=-iel
    i1=iup(iev,1);
    i2=iup(iev,2);
    i3=iup(iev,3);
    ic=iuc(iev);
    l1=sqrt((x(i1)-x(ic))^2+(y(i1)-y(ic))^2+(z(i1)-z(ic))^2);
    l2=sqrt((x(i2)-x(ic))^2+(y(i2)-y(ic))^2+(z(i2)-z(ic))^2);
    l3=sqrt((x(i3)-x(ic))^2+(y(i3)-y(ic))^2+(z(i3)-z(ic))^2);
    cs=convpipe(t,n,dt,c(:,i1),dv(iev,1),l1,q(i1));
    c(:,i2)=convpipe(t,n,dt,cs,dv(iev,2),l2,q(i2));
    c(:,i3)=convpipe(t,n,dt,cs,dv(iev,3),l3,q(i3));
end
end










    




