| [382] | 1 | %------------------------------------------------------------------------ |
|---|
| 2 | % patch function |
|---|
| 3 | % OUTPUT: |
|---|
| 4 | % SubRange(NbCoord,NbSubdomain,2): range (min, max) of the coordiantes x and y respectively, for each subdomain |
|---|
| 5 | % NbSites(NbSubdomain): number of source points for each subdomain |
|---|
| 6 | % FF: false flags |
|---|
| 7 | % U_smooth, V_smooth: filtered velocity components at the positions of the initial data |
|---|
| 8 | % Coord_tps(NbSites,NbCoord,NbSubdomain): positions of the tps centres |
|---|
| 9 | % U_tps,V_tps: weight of the tps for each subdomain |
|---|
| 10 | % |
|---|
| 11 | % INPUT: |
|---|
| 12 | % X, Y: set of coordinates of the initial data |
|---|
| 13 | % U,V: set of velocity components of the initial data |
|---|
| 14 | % Rho: smoothing parameter |
|---|
| 15 | % Threshold: max diff accepted between smoothed and initial data |
|---|
| 16 | % Subdomain: estimated number of data points in each subdomain |
|---|
| 17 | |
|---|
| 18 | %function [SubRangx,SubRangy,nbpoints,FF,U_smooth,V_smooth,X_tps,Y_tps,U_tps,V_tps,Indices_tps] =filter_tps(Coord,U,V,W,SubDomain,Rho,Threshold) |
|---|
| 19 | function [SubRange,NbSites,Coord_tps,U_tps,V_tps,W_tps,U_smooth,V_smooth,W_smooth,FF] =filter_tps(Coord,U,V,W,SubDomain,Rho,Threshold) |
|---|
| 20 | %subdomain decomposition |
|---|
| 21 | warning off |
|---|
| 22 | % U=reshape(U,[],1); |
|---|
| 23 | % V=reshape(V,[],1); |
|---|
| 24 | % X=reshape(X,[],1); |
|---|
| 25 | % Y=reshape(Y,[],1); |
|---|
| 26 | nbvec=size(Coord,1); |
|---|
| 27 | W_tps=[];%default |
|---|
| 28 | W_smooth=[]; |
|---|
| 29 | NbCoord=size(Coord,2); |
|---|
| [389] | 30 | NbSubDomain=nbvec/SubDomain; |
|---|
| [382] | 31 | MinCoord=min(Coord,[],1); |
|---|
| 32 | MaxCoord=max(Coord,[],1); |
|---|
| 33 | Range=MaxCoord-MinCoord; |
|---|
| 34 | AspectRatio=Range(2)/Range(1); |
|---|
| 35 | NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1); |
|---|
| 36 | NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1); |
|---|
| 37 | NbSubDomain=NbSubDomainX*NbSubDomainY; |
|---|
| 38 | Siz(1)=Range(1)/NbSubDomainX;%width of subdomains |
|---|
| 39 | Siz(2)=Range(2)/NbSubDomainY;%height of subdomains |
|---|
| 40 | CentreX=linspace(MinCoord(1)+Siz(1)/2,MaxCoord(1)-Siz(1)/2,NbSubDomainX); |
|---|
| 41 | CentreY=linspace(MinCoord(2)+Siz(2)/2,MaxCoord(2)-Siz(2)/2,NbSubDomainY); |
|---|
| 42 | [CentreX,CentreY]=meshgrid(CentreX,CentreY); |
|---|
| 43 | CentreY=reshape(CentreY,1,[]);% Y positions of subdomain centres |
|---|
| 44 | CentreX=reshape(CentreX,1,[]);% X positions of subdomain centres |
|---|
| 45 | rho=Siz(1)*Siz(2)*Rho/1000000;%optimum rho increase as the area of the subdomain (division by 10^6 to reach good values with the default GUI input) |
|---|
| 46 | U_tps_sub=zeros(nbvec,NbSubDomain);%default spline |
|---|
| 47 | V_tps_sub=zeros(nbvec,NbSubDomain);%default spline |
|---|
| 48 | Indices_tps=zeros(nbvec,NbSubDomain);%default indices |
|---|
| 49 | U_smooth=zeros(nbvec,1); |
|---|
| 50 | V_smooth=zeros(nbvec,1); |
|---|
| 51 | nb_select=zeros(nbvec,1); |
|---|
| 52 | FF=zeros(nbvec,1); |
|---|
| 53 | check_empty=zeros(1,NbSubDomain); |
|---|
| 54 | SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the positions of subdomains |
|---|
| 55 | % SubRangy=zeros(NbSubDomain,2); |
|---|
| 56 | for isub=1:NbSubDomain |
|---|
| 57 | SubRange(1,:,isub)=[CentreX(isub)-0.55*Siz(1) CentreX(isub)+0.55*Siz(1)]; |
|---|
| 58 | SubRange(2,:,isub)=[CentreY(isub)-0.55*Siz(2) CentreY(isub)+0.55*Siz(2)]; |
|---|
| 59 | ind_sel_previous=[]; |
|---|
| 60 | ind_sel=0; |
|---|
| 61 | while numel(ind_sel)>numel(ind_sel_previous) %increase the subdomain during four iterations at most |
|---|
| 62 | ind_sel_previous=ind_sel; |
|---|
| 63 | ind_sel=find(Coord(:,1)>=SubRange(1,1,isub) & Coord(:,1)<=SubRange(1,2,isub) & Coord(:,2)>=SubRange(2,1,isub) & Coord(:,2)<=SubRange(2,2,isub)); |
|---|
| 64 | % if no vector in the subdomain, skip the subdomain |
|---|
| 65 | if isempty(ind_sel) |
|---|
| 66 | check_empty(isub)=1; |
|---|
| 67 | U_tps(1,isub)=0;%define U_tps and V_tps by default |
|---|
| 68 | V_tps(1,isub)=0; |
|---|
| 69 | break |
|---|
| 70 | % if too few selected vectors, increase the subrange for next iteration |
|---|
| 71 | elseif numel(ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous); |
|---|
| [387] | 72 | SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4; |
|---|
| 73 | SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4; |
|---|
| [389] | 74 | else |
|---|
| [382] | 75 | [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel,:),U(ind_sel),rho); |
|---|
| 76 | [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel,:),V(ind_sel),rho); |
|---|
| 77 | UDiff=U_smooth_sub-U(ind_sel); |
|---|
| 78 | VDiff=V_smooth_sub-V(ind_sel); |
|---|
| 79 | NormDiff=UDiff.*UDiff+VDiff.*VDiff; |
|---|
| 80 | ind_ind_sel=1:numel(ind_sel);%default |
|---|
| 81 | if exist('Threshold','var') |
|---|
| 82 | FF(ind_sel)=20*(NormDiff>Threshold);%put FF value to 20 to identify the criterium of elimmination |
|---|
| 83 | ind_ind_sel=find(FF(ind_sel)==0); % select the indices of ind_sel corresponding to the remaining vectors |
|---|
| 84 | end |
|---|
| [387] | 85 | % if no value exceeds threshold, the result is recorded |
|---|
| [382] | 86 | if isequal(numel(ind_ind_sel),numel(ind_sel)) |
|---|
| 87 | U_smooth(ind_sel)=U_smooth(ind_sel)+U_smooth_sub; |
|---|
| 88 | V_smooth(ind_sel)=V_smooth(ind_sel)+V_smooth_sub; |
|---|
| 89 | NbSites(isub)=numel(ind_sel); |
|---|
| 90 | Coord_tps(1:NbSites(isub),:,isub)=Coord(ind_sel,:); |
|---|
| 91 | U_tps(1:NbSites(isub)+3,isub)=U_tps_sub; |
|---|
| 92 | V_tps(1:NbSites(isub)+3,isub)=V_tps_sub; |
|---|
| 93 | nb_select(ind_sel)=nb_select(ind_sel)+1; |
|---|
| [387] | 94 | display('good') |
|---|
| [382] | 95 | break |
|---|
| [387] | 96 | % if too few selected vectors, increase the subrange for next iteration |
|---|
| [382] | 97 | elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous); |
|---|
| [387] | 98 | SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4; |
|---|
| 99 | SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4; |
|---|
| 100 | % else interpolation-smoothing is done again with the selected vectors |
|---|
| [382] | 101 | else |
|---|
| [387] | 102 | [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),U(ind_sel(ind_ind_sel)),rho); |
|---|
| 103 | [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),V(ind_sel(ind_ind_sel)),rho); |
|---|
| [382] | 104 | U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub; |
|---|
| [389] | 105 | V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub; |
|---|
| [382] | 106 | NbSites(isub)=numel(ind_ind_sel); |
|---|
| 107 | Coord_tps(1:NbSites(isub),:,isub)=Coord(ind_sel(ind_ind_sel),:); |
|---|
| 108 | U_tps(1:NbSites(isub)+3,isub)=U_tps_sub; |
|---|
| 109 | V_tps(1:NbSites(isub)+3,isub)=V_tps_sub; |
|---|
| 110 | nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1; |
|---|
| 111 | display('good2') |
|---|
| 112 | break |
|---|
| 113 | end |
|---|
| 114 | end |
|---|
| 115 | end |
|---|
| 116 | end |
|---|
| 117 | ind_empty=find(check_empty); |
|---|
| 118 | %remove empty subdomains |
|---|
| 119 | if ~isempty(ind_empty) |
|---|
| 120 | SubRange(:,:,ind_empty)=[]; |
|---|
| 121 | Coord_tps(:,:,ind_empty)=[]; |
|---|
| 122 | U_tps(:,ind_empty)=[]; |
|---|
| 123 | V_tps(:,ind_empty)=[]; |
|---|
| 124 | end |
|---|
| 125 | nb_select(nb_select==0)=1;%ones(size(find(nb_select==0))); |
|---|
| 126 | U_smooth=U_smooth./nb_select; |
|---|
| 127 | V_smooth=V_smooth./nb_select; |
|---|