source: issm/oecreview/Archive/23390-24306/ISSM-23559-23560.diff@ 24308

Last change on this file since 24308 was 24307, checked in by Mathieu Morlighem, 5 years ago

NEW: adding Archive/23390-24306

File size: 6.1 KB
  • ../trunk-jpl/src/m/contrib/tsantos/mismip/if_position.m

     
     1function [ifx ify] = if_position(md,step,level),
     2
     3                if(~isfield(md.results.TransientSolution,'MaskIceLevelset'))
     4                        ifx=[];
     5                        ify=[];
     6                        return
     7                end
     8
     9                %initialization of some variables
     10                data                                            = md.results.TransientSolution(step).MaskIceLevelset;
     11                if(isfield(md.results.TransientSolution,'MeshElements'))
     12                        index                                   = md.results.TransientSolution(step).MeshElements;
     13                        x                                               = md.results.TransientSolution(step).MeshX;
     14                        y                                               = md.results.TransientSolution(step).MeshY;
     15                else
     16                        index                                   = md.mesh.elements;
     17                        x                                               = md.mesh.x;
     18                        y                                               = md.mesh.y;
     19                end
     20                numberofelements                = size(index,1);
     21                elementslist                    = 1:numberofelements;
     22                c                                                       = [];
     23                h                                                       = [];
     24
     25                %get unique edges in mesh
     26                %1: list of edges
     27                edges=[index(:,[1,2]); index(:,[2,3]); index(:,[3,1])];
     28                %2: find unique edges
     29                [edges,I,J]=unique(sort(edges,2),'rows');
     30                %3: unique edge numbers
     31                vec=J;
     32                %4: unique edges numbers in each triangle (2 triangles sharing the same edge will have
     33                %   the same edge number)
     34                edges_tria=[vec(elementslist), vec(elementslist+numberofelements), vec(elementslist+2*numberofelements)];
     35
     36                %segments [nodes1 nodes2]
     37                Seg1=index(:,[1 2]);
     38                Seg2=index(:,[2 3]);
     39                Seg3=index(:,[3 1]);
     40
     41                %segment numbers [1;4;6;...]
     42                Seg1_num=edges_tria(:,1);
     43                Seg2_num=edges_tria(:,2);
     44                Seg3_num=edges_tria(:,3);
     45
     46                %value of data on each tips of the segments
     47                Data1=data(Seg1);
     48                Data2=data(Seg2);
     49                Data3=data(Seg3);
     50
     51                %get the ranges for each segment
     52                Range1=sort(Data1,2);
     53                Range2=sort(Data2,2);
     54                Range3=sort(Data3,2);
     55
     56                %find the segments that contain this value
     57                pos1=(Range1(:,1)<level & Range1(:,2)>level);
     58                pos2=(Range2(:,1)<level & Range2(:,2)>level);
     59                pos3=(Range3(:,1)<level & Range3(:,2)>level);
     60
     61                %get elements
     62                poselem12=(pos1 & pos2);
     63                poselem13=(pos1 & pos3);
     64                poselem23=(pos2 & pos3);
     65                poselem=find(poselem12 | poselem13 | poselem23);
     66                numelems=length(poselem);
     67
     68                %if no element has been flagged, skip to the next level
     69                if numelems==0,
     70                        return,
     71                end
     72
     73                %go through the elements and build the coordinates for each segment (1 by element)
     74                x1=zeros(numelems,1);
     75                x2=zeros(numelems,1);
     76                y1=zeros(numelems,1);
     77                y2=zeros(numelems,1);
     78                edge_l=zeros(numelems,2);
     79
     80                for j=1:numelems,
     81
     82                        weight1=(level-Data1(poselem(j),1))/(Data1(poselem(j),2)-Data1(poselem(j),1));
     83                        weight2=(level-Data2(poselem(j),1))/(Data2(poselem(j),2)-Data2(poselem(j),1));
     84                        weight3=(level-Data3(poselem(j),1))/(Data3(poselem(j),2)-Data3(poselem(j),1));
     85
     86                        if poselem12(poselem(j));
     87
     88                                x1(j)=x(Seg1(poselem(j),1))+weight1*(x(Seg1(poselem(j),2))-x(Seg1(poselem(j),1)));
     89                                x2(j)=x(Seg2(poselem(j),1))+weight2*(x(Seg2(poselem(j),2))-x(Seg2(poselem(j),1)));
     90                                y1(j)=y(Seg1(poselem(j),1))+weight1*(y(Seg1(poselem(j),2))-y(Seg1(poselem(j),1)));
     91                                y2(j)=y(Seg2(poselem(j),1))+weight2*(y(Seg2(poselem(j),2))-y(Seg2(poselem(j),1)));
     92                                edge_l(j,1)=Seg1_num(poselem(j));
     93                                edge_l(j,2)=Seg2_num(poselem(j));
     94
     95                        elseif poselem13(poselem(j)),
     96
     97                                x1(j)=x(Seg1(poselem(j),1))+weight1*(x(Seg1(poselem(j),2))-x(Seg1(poselem(j),1)));
     98                                x2(j)=x(Seg3(poselem(j),1))+weight3*(x(Seg3(poselem(j),2))-x(Seg3(poselem(j),1)));
     99                                y1(j)=y(Seg1(poselem(j),1))+weight1*(y(Seg1(poselem(j),2))-y(Seg1(poselem(j),1)));
     100                                y2(j)=y(Seg3(poselem(j),1))+weight3*(y(Seg3(poselem(j),2))-y(Seg3(poselem(j),1)));
     101                                edge_l(j,1)=Seg1_num(poselem(j));
     102                                edge_l(j,2)=Seg3_num(poselem(j));
     103
     104                        elseif poselem23(poselem(j)),
     105
     106                                x1(j)=x(Seg2(poselem(j),1))+weight2*(x(Seg2(poselem(j),2))-x(Seg2(poselem(j),1)));
     107                                x2(j)=x(Seg3(poselem(j),1))+weight3*(x(Seg3(poselem(j),2))-x(Seg3(poselem(j),1)));
     108                                y1(j)=y(Seg2(poselem(j),1))+weight2*(y(Seg2(poselem(j),2))-y(Seg2(poselem(j),1)));
     109                                y2(j)=y(Seg3(poselem(j),1))+weight3*(y(Seg3(poselem(j),2))-y(Seg3(poselem(j),1)));
     110                                edge_l(j,1)=Seg2_num(poselem(j));
     111                                edge_l(j,2)=Seg3_num(poselem(j));
     112                        else
     113                                %it shoud not go here
     114                        end
     115                end
     116
     117                %now that we have the segments, we must try to connect them...
     118
     119                %loop over the subcontours
     120                indice=0;
     121                while ~isempty(edge_l),
     122                        indice=indice+1;
     123
     124                        %take the right edge of the second segment and connect it to the next segments if any
     125                        e1=edge_l(1,1);   e2=edge_l(1,2);
     126                        xc=[x1(1);x2(1)]; yc=[y1(1);y2(1)];
     127
     128                        %erase the lines corresponding to this edge
     129                        edge_l(1,:)=[];
     130                        x1(1)=[]; x2(1)=[];
     131                        y1(1)=[]; y2(1)=[];
     132
     133                        [ro1,co1]=find(edge_l==e1);
     134
     135                        while ~isempty(ro1)
     136
     137                                if co1==1,
     138                                        xc=[x2(ro1);xc]; yc=[y2(ro1);yc];
     139
     140                                        %next edge:
     141                                        e1=edge_l(ro1,2);
     142
     143                                else
     144                                        xc=[x1(ro1);xc]; yc=[y1(ro1);yc];
     145
     146                                        %next edge:
     147                                        e1=edge_l(ro1,1);
     148                                end
     149
     150                                %erase the lines of this
     151                                edge_l(ro1,:)=[];
     152                                x1(ro1)=[]; x2(ro1)=[];
     153                                y1(ro1)=[]; y2(ro1)=[];
     154
     155                                %next connection
     156                                [ro1,co1]=find(edge_l==e1);
     157                        end
     158
     159                        %same thing the other way (to the right)
     160                        [ro2,co2]=find(edge_l==e2);
     161
     162                        while ~isempty(ro2)
     163
     164                                if co2==1,
     165                                        xc=[xc;x2(ro2)]; yc=[yc;y2(ro2)];
     166
     167                                        %next edge:
     168                                        e2=edge_l(ro2,2);
     169                                else
     170                                        xc=[xc;x1(ro2)]; yc=[yc;y1(ro2)];
     171
     172                                        %next edge:
     173                                        e2=edge_l(ro2,1);
     174                                end
     175
     176                                %erase the lines of this
     177                                edge_l(ro2,:)=[];
     178                                x1(ro2)=[]; x2(ro2)=[];
     179                                y1(ro2)=[]; y2(ro2)=[];
     180
     181                                %next connection
     182                                [ro2,co2]=find(edge_l==e2);
     183                        end
     184
     185                        % Update the CS data structure as per "contours.m"
     186                        % so that clabel works
     187                        c = horzcat(c,[level, xc'; length(xc), yc']);
     188        %               y0=find(c(2,:)==0);
     189        %               y50=find(c(2,:)==50000);
     190        %               gl0(glstep)=c(1,y0);
     191        %               gl50(glstep)=c(1,y50);
     192        ifx=c(1,1:end)';
     193        ify=c(2,1:end)';
     194        pos=find(ifx~=0);
     195        ifx=ifx(pos);
     196        ify=ify(pos);
     197
     198end
     199        %min(c(1,2:end))
Note: See TracBrowser for help on using the repository browser.