首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >符号距离函数是某度量空间X中的一个集合Ω的函数。

符号距离函数是某度量空间X中的一个集合Ω的函数。

作者头像
裴来凡
发布于 2022-05-28 15:12:48
发布于 2022-05-28 15:12:48
9440
举报

exampleLevelSet.m

代码语言:javascript
复制
% Level Set Functions for Certain Shapes
%
% Two dimensional level method for a prescribed
% velocity field based on upwinding
%
clear all
close all
N=200;         % number of grid points in one direction
numc=1;        % number of circles for the initial condition
R1=.3;         % initial radius of circles
h=2/(N-1);     % grid spacing
dt=.1*h;       % time step
tfin=2;       % total simulation time
nit=tfin/dt;   % number of time steps
x=-1:h:1;
y=x;
[X,Y]=meshgrid(x);
%
%
%   Initialize the level set function
%   Union of numc randomly located circles
a1=2*(1-R1-h)*rand-(1-R1-h);
b1=2*(1-R1-h)*rand-(1-R1-h);
phi=((X-a1).*(X-a1)+(Y-b1).*(Y-b1)).^.5-R1;
for k=1:numc-1
    a1=2*(1-R1-h)*rand-(1-R1-h);
    b1=2*(1-R1-h)*rand-(1-R1-h);
    phi1=((X-a1).*(X-a1)+(Y-b1).*(Y-b1)).^.5-R1;
    phi=min(phi,phi1);
end
figure(1)
contour(X,Y,phi,[0,0],'r');
axis([-1 1 -1 1])
axis('square')
%
%   Initialize the velocity field
    u=2-cos(2*pi*Y);
    v=2+sin(2*pi*X);
%
%      arrays for the periodic boundary conditions
for i=1:N
    ip(i)=i+1;
    im(i)=i-1;
end
im(1)=N;
ip(N)=1;
%
%      begin simulation loop
for iter=1:nit
    for i=1:N
        for j=1:N
            dmx=(phi(i,j)-phi(im(i),j))/h;                     % x backward difference
            dpx=(phi(ip(i),j)-phi(i,j))/h;                     % x forward difference
            dmy=(phi(i,j)-phi(i,im(j)))/h;                     % y backward difference
            dpy=(phi(i,ip(j))-phi(i,j))/h;                     % y forward difference
            convx=max(u(i,j),0)*dmx+min(u(i,j),0)*dpx;
            convy=max(v(i,j),0)*dmy+min(v(i,j),0)*dpy;
            phin(i,j)=phi(i,j)-(convx+convy)*dt;         % advance by dt
        end
    end
    phi=phin;                                             % update
    %
    %         Plotting
    contour(X,Y,phi,[0,0],'r');
    axis([-1 1 -1 1])
    axis('square')
    pause(.001)
end

http://mpvideo.qpic.cn/0bf2qaaagaaa6qaluj5s6jpfbagdaoaaaaya.f10002.mp4?dis_k=15c921b4ada3ebf8d090f9e03d659206&dis_t=1653729463&vid=wxv_1328401797383503872&format_id=10002&support_redirect=0&mmversion=false

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-05-06,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档