Advertisement
AnttiLehikoinen

Slightly-less ugly meshplot

Sep 2nd, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 7.29 KB | None | 0 0
  1. function f = meshplot(file, alfa, flag_air, flag_raja)
  2. % Plots a mesh from cim.fedat file or fedat structure
  3.  
  4.   if nargin == 0
  5.     clc;
  6.     close all;
  7.     file = [fcroot '\ajot\cim.fedat'];
  8.     flag_air = 0;
  9.     flag_raja = 0;
  10.   end,
  11.   if nargin < 2
  12.     alfa = 0;
  13.   end;
  14.   if nargin < 3
  15.     flag_air = 0;
  16.   end;
  17.   if nargin < 4
  18.     flag_raja = 1;
  19.   end;
  20.   flag_air = 1;
  21.  
  22.   if isstruct(file)
  23.     data = file;
  24.   else
  25.     data = readcimfedat(file,1);
  26.   end;
  27.  
  28.   % Rotate whole mesh by a given angle
  29.   alfa = alfa*pi/180;
  30.   x = data.cord(:,1);
  31.   y = data.cord(:,2);
  32.   data.cord(:,1) = x*cos(alfa)-y*sin(alfa);
  33.   data.cord(:,2) = y*cos(alfa)+x*sin(alfa);
  34.  
  35. %   for k = 1 : data.maxel
  36. %     ala = elarea(data.cord(data.nop(k,1:3),1),data.cord(data.nop(k,1:3),2));
  37. %     disp(ala)
  38. %     if ala < 0
  39. %       return
  40. %     end;
  41. %   end;
  42. %   return
  43.  
  44. %   elx = mask(data.nop(:,1:3), data.cord(:,1))
  45. %   ely = mask(data.nop(:,1:3), data.cord(:,2))
  46. %   cpx = mean(elx, 2);
  47. %   cpy = mean(ely, 2);
  48. %  
  49. %   ind = ((cpx.^2 + cpy.^2 > 0.215^2) & (data.imat == 1));
  50. % %
  51. % %   data.nop(ind,:) = [];
  52. %   data.imat(ind,:) = 8;
  53. %  
  54. %   iel = [194]
  55. %   data.imat(
  56.  
  57. %   x1 = data.cord(4000,1);
  58. %   y1 = data.cord(4000,2);
  59. %   x2 = data.cord(3999,1);
  60. %   y2 = data.cord(3999,2);
  61.  
  62. % imove = 3863
  63. % [kmove xx] = find(data.nop == imove);
  64. % kmove
  65. %
  66. % data.cord(4088,:) = mean(data.cord([5259;4000],:),1);
  67. % data.cord(5260,:) = mean(data.cord([5259;5261],:),1);
  68. % data.cord(5258,:) = mean(data.cord([5257;5259],:),1);
  69. % data.cord(4164,:) = mean(data.cord([4128;5259],:),1);
  70. % data.cord(4059,:) = mean(data.cord([4058;5259],:),1);
  71. %
  72. %
  73. % data.cord(4060,:) = mean(data.cord([3974;3863],:),1);
  74. % data.cord(3864,:) = mean(data.cord([3865;3863],:),1);
  75. % data.cord(4031,:) = mean(data.cord([4032;3863],:),1);
  76. % data.cord(4130,:) = mean(data.cord([4090;3863],:),1);
  77. % data.cord(3862,:) = mean(data.cord([3861;3863],:),1);
  78.  
  79. %   k = (y2-y1)/(x2-x1);
  80. %  
  81. %   data.cord(5259,2) = (y1-k*x1);
  82. %   data.cord(data.ibc(5259),1) = (y1-k*x1);
  83. %  
  84. %   data.imat([1907:1912,1942]) = data.imat(1882)
  85. %   data.imat([1883:1888,1913]) = data.imat(1882)
  86.  
  87. % unique(data.imat)
  88. % return
  89.  
  90.  
  91.   % Set rgb colors to each material index
  92.   %setcol = @(ind,c) eval(['col(1+[' num2str(ind) '],:) = ones(' num2str(length(ind)) ',1)*c;']);
  93.   col = zeros(302, 3);
  94.   setcol = @(arg_col, ind, c)( aux_setcol(arg_col, ind, c) );
  95.  
  96.   col = setcol(col, 0       , [1 1 1]);     % Air
  97.   col = setcol(col, 1       , [1 1 1]*0.6); % Shaft
  98.   col = setcol(col, 2       , [1 1 1]*0.9); % Rotor steel
  99.   col = setcol(col, 3:10    , [1 1 1]*0.7); % Laminations
  100. %   setcol(11      , [0.6 0.6 1]); % Low-permeability layer
  101.   col = setcol(col, 11      , [1 1 0.3]*0.7); % Low-permeability layer
  102.   col = setcol(col, 11      , [1 0 1]); % Low-permeability layer
  103.   col = setcol(col, 32      , [1 1 1]*0.9); % Something
  104.   col = setcol(col, [41 44 61] , [0.4 0.4 1]); % Phase a winding
  105. %   setcol([42 45 62] , [0 1 0]);     % Phase b winding, green
  106. %   setcol([43 46 63] , [1 0 0]);     % Phase c winding, reg
  107.   col = setcol(col, [42 45 62] , [1 0 0]);     % Phase b winding, green
  108.   col = setcol(col, [43 46 63] , [0 1 0]);     % Phase c winding, reg
  109.   col = setcol(col, 47:48   , [1 0.7 0]);   % Rotor windings
  110.   col = setcol(col, 81:92   , [1 0.7 0]);   % Rotor windings
  111.   col = setcol(col, 501:600 , [1 1 0]);     % Rotor bars?
  112.   col = setcol(col, 200:299 , [1 1 0]);     % Rotor bars=
  113.   col = setcol(col, 301     , [1 0 0.8]);   % Permanent magnets
  114.   col = setcol(col, 302     , [1 0 0.5]);   % Permanent magnets
  115.   col = setcol(col, 303     , [1 0 0.2]);   % Permanent magnets
  116.  
  117.   % 1st order elements
  118.   ne = [3 4 6 8]; % These node numbers are ok
  119.   for in = 1:length(ne)
  120.     if ~any(data.nodel == ne(in)), continue; end;
  121.  
  122.     % Linear elements
  123.     if ne(in) <= 4
  124.       imats = unique(data.imat);
  125.       if flag_air == 0
  126.         imats(imats == 0) = [];
  127.       end;  
  128.       for im = 1:length(imats);
  129.         if flag_raja == 1
  130.           ind = find((data.nodel == ne(in)) & (data.imat == imats(im)));
  131.           p(imats(im)+1) = patch('Vertices', data.cord, 'Faces', data.nop((data.nodel == ne(in)) & (data.imat == imats(im)),1:ne(in)), 'FaceColor', col(imats(im)+1,:)); hold on;
  132.         else
  133.           p(imats(im)+1) = patch('Vertices', data.cord, 'Faces', data.nop((data.nodel == ne(in)) & (data.imat == imats(im)),1:ne(in)), 'FaceColor', col(imats(im)+1,:), 'EdgeColor', 'none'); hold on;
  134.         end;
  135.       end;
  136.  
  137.     % 2nd order elements, circle arcs needed
  138.     else
  139.       if ne(in) == 6
  140.         ip = [1 4 2; 2 5 3; 3 6 1];
  141.       else
  142.         ip = [1 5 2; 2 6 3; 3 7 4; 4 8 1];
  143.       end;
  144.      
  145.       kni = find(data.nodel == ne(in));
  146.       for ik = 1: length(kni)
  147.         k = kni(ik);
  148.         if (flag_air == 0) & (data.imat(k) == 0)
  149.           continue;
  150.         end;
  151.         crd = [];
  152.         for i = 1 : size(ip,1);
  153.            x = data.cord(data.nop(k,ip(i,:)),1);
  154.            y = data.cord(data.nop(k,ip(i,:)),2);
  155.            ala = abs(elarea([x, y]));
  156.            if ala > 1e-9
  157.              [xc yc r fi1 fi2] = circlearc(x,y);
  158.              fi = unwrap([fi1 fi2]);
  159.              fi1 = fi(1);
  160.              fi2 = fi(2);
  161.              fii = linspace(fi1,fi2,12); fii([1 end]) = [];
  162.              crd(end+1,:) = data.cord(data.nop(k,i),:);
  163.              for ii = 1 : length(fii)
  164.                crd(end+1,:) = [xc  yc]+r*[cos(fii(ii)) sin(fii(ii))];
  165.              end;
  166.            else
  167.              crd(end+1,:) = data.cord(data.nop(k,i),:);
  168.            end;
  169.         end;
  170.         nop = 1:size(crd,1);
  171.         h = patch('Vertices', crd, 'Faces', nop); hold on;
  172.         set(h, 'FaceColor',col(data.imat(k)+1,:));
  173.         if flag_raja == 0
  174.           set(h, 'EdgeColor','none');
  175.         end;
  176.       end;
  177.     end;
  178.   end;
  179.  
  180. %   % Plot material limits
  181.   if flag_raja == 0
  182.     sivut = [];
  183.     smat = [];
  184.     for k = 1 : data.maxel
  185.       for i = 1 : 3
  186.         nik1 = data.nop(k,i);
  187.         nik2 = data.nop(k,circind(i+1,3));
  188.         rivi = findrows(sivut, [nik1, nik2]);
  189.         if (k > 1) & length(rivi) == 0
  190.           sivut(end+1,:) = [nik1 nik2];
  191.           smat(end+1) = data.imat(k);
  192.           iraja(length(sivut)) = 1;
  193.         elseif (k == 1)
  194.           sivut(end+1,:) = [nik1 nik2];
  195.           smat(end+1) = data.imat(k);
  196.           iraja(length(sivut)) = 1;
  197.         else
  198.           iraja(rivi) = 0;
  199.           if smat(rivi) ~= data.imat(k);
  200.             iraja(rivi) = 1;
  201.           end;
  202.         end;
  203.       end;
  204.     end;
  205.     iraja = find(iraja)';
  206.     for i = 1 : length(iraja);
  207.       plot(data.cord(sivut(iraja(i),:),1), data.cord(sivut(iraja(i),:),2), 'k-');
  208.     end;
  209.   end;
  210.   set(gcf,'renderer','zbuffer')
  211.   axis equal;
  212.  
  213. return
  214.  
  215. %   text(data.cord(:,1), data.cord(:,2), num2strcell(1:data.maxnp))
  216. %   axis equal
  217. %   return
  218.  
  219.   idir = find(data.ibc == 0);
  220.   iper = find(data.ibc > 0);
  221.   iref = data.ibc(find(data.ibc > 0));
  222.   hold on;
  223.   plot(data.cord(idir, 1), data.cord(idir, 2), 'b.');
  224.   plot(data.cord(iper, 1), data.cord(iper, 2), 'ro');
  225.   plot(data.cord(iref, 1), data.cord(iref, 2), 'go');
  226.  
  227.   unique(data.cord(iref,1))-unique(data.cord(iper,2))
  228.  
  229. end
  230.  
  231. function col = aux_setcol(col, ind, c)
  232.  
  233. N = size(col, 1);
  234. if max(ind) > N
  235.     col = [col; zeros(max(ind)-N, 3)];
  236. end
  237.  
  238. col(1+ind, :) = ones( numel(ind) , 1)*c;
  239.  
  240. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement