Changeset 1342


Ignore:
Timestamp:
07/17/09 09:01:18 (16 years ago)
Author:
Mathieu Morlighem
Message:

Improved Yams and QualityMesh

Location:
issm/trunk/src/m/utils/Mesh
Files:
2 added
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • issm/trunk/src/m/utils/Mesh/MeshQuality.m

    r1341 r1342  
    1 function meshquality(md);
     1function MeshQuality(md,epsilon,hmin,hmax);
    22%MESHQUALITY - compute mesh quality
    33%
    44%   Usage:
    5 %      meshquality(md);
     5%      MeshQuality(md,epsilon,hmin,hmax);
    66
    7 t1=clock; fprintf('%s','      computing Hessian...');
    8 %initialization
    9 field=md.vel_obs;
     7%Get some variables from the model
     8index=md.elements;
    109x=md.x;
    1110y=md.y;
    12 index=md.elements;
    13 numberofgrids=md.numberofgrids;
    14 numberofelements=md.numberofelements;
    15 gradx=zeros(numberofgrids,1);
    16 grady=zeros(numberofgrids,1);
    17 metric=zeros(numberofelements,3);
    1811
    1912%2d geometric parameter (do not change)
    2013scale=2/9;
    21 hmin=500;
    22 hmax=10^6;
    23 epsilon=5;
    2414
    25 %build some usefull variables
    26 line=index(:);
    27 linesize=3*numberofelements;
     15%Compute Hessian
     16hessian=ComputeHessian(index,x,y,md.vel_obs,'node');
    2817
    29 %get areas and  nodal functions coefficients N(x,y)=alpha x + beta y + gamma
    30 [alpha beta]=GetNodalFunctionsCoeff(index,md.x,md.y);
    31 areas=GetAreas(index,md.x,md.y);
    32 
    33 %Compute gradient for each element
    34 grad_elx=sum(field(index).*alpha,2);
    35 grad_ely=sum(field(index).*beta,2);
    36 
    37 %update weights that holds the volume of all the element holding the grid i
    38 weights=sparse(line,ones(linesize,1),repmat(areas,3,1),numberofgrids,1);
    39 
    40 %Compute gradient for each grid (average of the elements around)
    41 gradx=sparse(line,ones(linesize,1),repmat(areas.*grad_elx,3,1),numberofgrids,1);
    42 grady=sparse(line,ones(linesize,1),repmat(areas.*grad_ely,3,1),numberofgrids,1);
    43 gradx=gradx./weights;
    44 grady=grady./weights;
    45 
    46 %Compute hessian for each element
    47 hessian_elem=[sum(gradx(index).*alpha,2) sum(grady(index).*alpha,2) sum(grady(index).*beta,2)];
    48 
    49 %Compute Hessian on the nodes (average of the elements around)
    50 hessian=[sparse(line,ones(linesize,1),repmat(areas.*hessian_elem(:,1),3,1),numberofgrids,1)./weights ...
    51         sparse(line,ones(linesize,1),repmat(areas.*hessian_elem(:,2),3,1),numberofgrids,1)./weights ...
    52         sparse(line,ones(linesize,1),repmat(areas.*hessian_elem(:,3),3,1),numberofgrids,1)./weights ];
    53 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);
    54 
    55 t1=clock; fprintf('%s','      computing metric...');
    56 %first, find the eigen values of eah line of H=[hessian(i,1) hessian(i,2); hessian(i,2)  hessian(i,3)]
    57 a=hessian(:,1); b=hessian(:,2); d=hessian(:,3);
    58 lambda1=0.5*((a+d)+sqrt(4*b.^2+(a-d).^2));
    59 lambda2=0.5*((a+d)-sqrt(4*b.^2+(a-d).^2));
    60 pos1=find(lambda1==0);
    61 pos2=find(lambda2==0);
    62 
    63 %Modify the eigen values to control the shape of the elements
    64 lambda1=min(max(abs(lambda1)*scale/epsilon,1/hmax^2),1/hmin^2);
    65 lambda2=min(max(abs(lambda2)*scale/epsilon,1/hmax^2),1/hmin^2);
    66 
    67 %compute eigen vectors
    68 norm1=sqrt(8*b.^2+2*(d-a).^2+2*(d-a).*sqrt((a-d).^2+4*b.^2));
    69 v1x=2*b./norm1;
    70 v1y=((d-a)+sqrt((a-d).^2+4*b.^2))./norm1;
    71 norm2=sqrt(8*b.^2+2*(d-a).^2-2*(d-a).*sqrt((a-d).^2+4*b.^2));
    72 v2x=2*b./norm2;
    73 v2y=((d-a)-sqrt((a-d).^2+4*b.^2))./norm2;
    74 
    75 %Compute new metric (for each node M=V*Lambda*V^-1)
    76 metric=full([(v1x.*v2y-v1y.*v2x).^(-1).*(lambda1.*v2y.*v1x-lambda2.*v1y.*v2x) ...
    77         (v1x.*v2y-v1y.*v2x).^(-1).*(lambda1.*v1y.*v2y-lambda2.*v1y.*v2y) ...
    78         (v1x.*v2y-v1y.*v2x).^(-1).*(-lambda1.*v2x.*v1y+lambda2.*v1x.*v2y)]);
    79 
    80 %some corrections for 0 eigen values
    81 metric(pos1,:)=repmat([1/hmax^2 0 1/hmax^2],length(pos1),1);
    82 metric(pos2,:)=repmat([1/hmax^2 0 1/hmax^2],length(pos2),1);
    83 if length(md.gridonwater)==numberofgrids;
     18%Compute metric
     19if length(md.gridonwater)==md.numberofgrids,
    8420        pos=find(md.gridonwater);
    85         metric(pos,:)=repmat([1/hmax^2 0 1/hmax^2],length(pos),1);
     21else
     22        pos=[];
    8623end
    87 
    88 %take care of water elements
    89 if length(md.gridonwater)==numberofgrids;
    90         pos=find(md.gridonwater);
    91         metric(pos,:)=repmat([1/hmax^2 0 1/hmax^2],length(pos),1);
    92 end
    93 
    94 if any(isnan(metric)),
    95         error('YamsCall error message: NaN in the metric...')
    96 end
    97 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);
     24metric=ComputeMetric(hessian,scale,epsilon,hmin,hmax,pos);
    9825
    9926%Get Areas
     
    12249
    12350%compute quality:
    124 Q=4*sqrt(3)*V./(L1+L2+L3);
    125 
    126 %display
    127 X=0:0.1:4;
    128 hist(Q,X);
    129 xlim([0 3]);
    130 title('mesh quality distribution');
     51quality=4*sqrt(3)*V./(L1+L2+L3);
    13152
    13253%compute error
    133 %{
    134 %a=hessian_elem(:,1); b=hessian_elem(:,2); d=hessian_elem(:,3);
    13554a=hessian(:,1); b=hessian(:,2); d=hessian(:,3);
    13655a=a(index)*[1;1;1]/3;
     
    14160lambda1=min(max(abs(lambda1)*scale/epsilon,1/hmax^2),1/hmin^2);
    14261lambda2=min(max(abs(lambda2)*scale/epsilon,1/hmax^2),1/hmin^2);
    143 if length(md.elementonwater)==numberofelements;
     62if length(md.gridonwater)==md.numberofgrids;
    14463        pos=find(md.gridonwater);
    145         lambda1(pos)=0;
    146         lambda2(pos)=0;
    147 end
    148 %}
    149 if length(md.gridonwater)==numberofgrids;
    15064        lambda1(pos)=0;
    15165        lambda2(pos)=0;
     
    15973
    16074%display
    161 plotmodel(md,'data',epsilon,'title','Interpolation error','figure',2,'log',10)
    162 
    163 disp(['Average interpolation error = ' num2str(mean(epsilon))  ' m/yr']);
    164 disp(['Maximum interpolation error = ' num2str(max(epsilon))  ' m/yr']);
     75X=0:0.1:4; hist(quality,X); xlim([0 3]); title('mesh quality distribution','FontSize',14);
     76plotmodel(md,'data',epsilon,'title','Interpolation error','figure',2)
     77disp(sprintf('\n%s','Mesh Quality'));
     78disp(sprintf('   %s %g','Average Mesh quality:',mean(quality)));
     79disp(sprintf('   %s %g','Maximum interpolation error:',max(quality)));
     80disp(sprintf('\n%s','Interpolation Error'));
     81disp(sprintf('   %s %g %s','Average interpolation error:',mean(epsilon),'m/yr'));
     82disp(sprintf('   %s %g %s','Maximum interpolation error:',max(epsilon),'m/yr'));
  • issm/trunk/src/m/utils/Mesh/YamsCall.m

    r1336 r1342  
    1919global ISSM_DIR
    2020
    21 t1=clock; fprintf('%s','      computing Hessian...');
    22 %initialization
    23 index=md.elements;
    24 numberofgrids=md.numberofgrids;
    25 numberofelements=md.numberofelements;
    26 gradx=zeros(numberofgrids,1);
    27 grady=zeros(numberofgrids,1);
    28 metric=zeros(numberofelements,3);
    29 
    3021%2d geometric parameter (do not change)
    3122scale=2/9;
    3223
    33 %build some usefull variables
    34 line=index(:);
    35 summation=1/3*ones(3,1);
    36 linesize=3*numberofelements;
    37 
    38 %get areas and  nodal functions coefficients N(x,y)=alpha x + beta y + gamma
    39 [alpha beta]=GetNodalFunctionsCoeff(index,md.x,md.y);
    40 areas=GetAreas(index,md.x,md.y);
    41 
    42 %Compute gradient for each element
    43 grad_elx=sum(field(index).*alpha,2);
    44 grad_ely=sum(field(index).*beta,2);
    45 
    46 %update weights that holds the volume of all the element holding the grid i
    47 weights=sparse(line,ones(linesize,1),repmat(areas,3,1),numberofgrids,1);
    48 
    49 %Compute gradient for each grid (average of the elements around)
    50 gradx=sparse(line,ones(linesize,1),repmat(areas.*grad_elx,3,1),numberofgrids,1);
    51 grady=sparse(line,ones(linesize,1),repmat(areas.*grad_ely,3,1),numberofgrids,1);
    52 gradx=gradx./weights;
    53 grady=grady./weights;
    54 
    55 %Compute hessian for each element
    56 hessian=[sum(gradx(index).*alpha,2) sum(grady(index).*alpha,2) sum(grady(index).*beta,2)];
    57 
    58 %Compute Hessian on the nodes (average of the elements around)
    59 hessian=[sparse(line,ones(linesize,1),repmat(areas.*hessian(:,1),3,1),numberofgrids,1)./weights ...
    60         sparse(line,ones(linesize,1),repmat(areas.*hessian(:,2),3,1),numberofgrids,1)./weights ...
    61         sparse(line,ones(linesize,1),repmat(areas.*hessian(:,3),3,1),numberofgrids,1)./weights ];
     24%Compute Hessian
     25t1=clock; fprintf('%s','      computing Hessian...');
     26hessian=ComputeHessian(md.elements,md.x,md.y,field,'node');
    6227t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);
    6328
     29%Compute metric
    6430t1=clock; fprintf('%s','      computing metric...');
    65 %first, find the eigen values of eah line of H=[hessian(i,1) hessian(i,2); hessian(i,2)  hessian(i,3)]
    66 a=hessian(:,1); b=hessian(:,2); d=hessian(:,3);
    67 lambda1=0.5*((a+d)+sqrt(4*b.^2+(a-d).^2));
    68 lambda2=0.5*((a+d)-sqrt(4*b.^2+(a-d).^2));
    69 pos1=find(lambda1==0);
    70 pos2=find(lambda2==0);
    71 
    72 %Modify the eigen values to control the shape of the elements
    73 lambda1=min(max(abs(lambda1)*scale/epsilon,1/hmax^2),1/hmin^2);
    74 lambda2=min(max(abs(lambda2)*scale/epsilon,1/hmax^2),1/hmin^2);
    75 
    76 %compute eigen vectors
    77 norm1=sqrt(8*b.^2+2*(d-a).^2+2*(d-a).*sqrt((a-d).^2+4*b.^2));
    78 v1x=2*b./norm1;
    79 v1y=((d-a)+sqrt((a-d).^2+4*b.^2))./norm1;
    80 norm2=sqrt(8*b.^2+2*(d-a).^2-2*(d-a).*sqrt((a-d).^2+4*b.^2));
    81 v2x=2*b./norm2;
    82 v2y=((d-a)-sqrt((a-d).^2+4*b.^2))./norm2;
    83 
    84 %Compute new metric (for each node M=V*Lambda*V^-1)
    85 metric=full([(v1x.*v2y-v1y.*v2x).^(-1).*(lambda1.*v2y.*v1x-lambda2.*v1y.*v2x) ...
    86         (v1x.*v2y-v1y.*v2x).^(-1).*(lambda1.*v1y.*v2y-lambda2.*v1y.*v2y) ...
    87         (v1x.*v2y-v1y.*v2x).^(-1).*(-lambda1.*v2x.*v1y+lambda2.*v1x.*v2y)]);
    88 
    89 %some corrections for 0 eigen values
    90 metric(pos1,:)=repmat([1/hmax^2 0 1/hmax^2],length(pos1),1);
    91 metric(pos2,:)=repmat([1/hmax^2 0 1/hmax^2],length(pos2),1);
    92 
    93 %take care of water elements
    94 if length(md.gridonwater)==numberofgrids;
     31if length(md.gridonwater)==md.numberofgrids,
    9532        pos=find(md.gridonwater);
    96         metric(pos,:)=repmat([1/hmax^2 0 1/hmax^2],length(pos),1);
     33else
     34        pos=[];
    9735end
    98 
    99 if any(isnan(metric)),
    100         error('YamsCall error message: NaN in the metric...')
    101 end
     36metric=ComputeMetric(hessian,scale,epsilon,hmin,hmax,pos);
    10237t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);
    10338
    104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Build FILES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    105 
     39%write files
    10640t1=clock; fprintf('%s','      writing initial mesh files...');
    10741save -ascii carre0.met  metric
     
    13266t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);
    13367
    134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% call Yams %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    135 
    13668%call yams
    13769fprintf('%s\n','      call Yams...');
    138 system(['yams2-linux -O 1 -v 1 -ecp -hgrad ' num2str(gradation)  ' carre0 carre1']);
     70system(['yams2-linux -O 1 -v 2 -ecp -hgrad ' num2str(gradation)  ' carre0 carre1']);
    13971
    14072%plug new mesh
Note: See TracChangeset for help on using the changeset viewer.