`
leonzhx
  • 浏览: 786174 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

1.  Goal: Simulate the motion of N moving particles that behave according to the laws of elastic collision.

     Hard disc model:

         - Moving particles interact via elastic collisions with each other and walls.

         - Each particle is a disc with known position, velocity, mass, and radius.

         - No other forces.

 

2.  N bouncing balls in the unit square without considering balls colliding with each other

public class Ball
{
    private double rx, ry; // position
    private double vx, vy; // velocity
    private final double radius; // radius
    public Ball(...)
    { /* initialize position and velocity */ }
    public void move(double dt)
    {
        if ((rx + vx*dt < radius) || (rx + vx*dt > 1.0 - radius)) {
            rx = 1.0 - radius - vx * dt +  (1.0 - radius - rx);
            vx = -vx; 
        } else {
            rx = rx + vx * dt;    
        }
        if ((ry + vy*dt < radius) || (ry + vy*dt > 1.0 - radius)) {
            ry = 1.0 - radius - vy * dt +  (1.0 - radius - ry);
            vy = -vy; 
       } else {
            ry = ry + vy*dt; 
       }
    }

    public void draw()
    {
        StdDraw.filledCircle(rx, ry, radius); 
    }
}

public class BouncingBalls
{
    public static void main(String[] args)
    {
        int N = Integer.parseInt(args[0]);
        Ball[] balls = new Ball[N];
        for (int i = 0; i < N; i++)
            balls[i] = new Ball();
        while(true)
        {
            StdDraw.clear();
            for (int i = 0; i < N; i++)
            {
                balls[i].move(0.5);
                balls[i].draw();
            }
            StdDraw.show(50);
        }
    }
}

 

3.  Naive Implementation for handling collision:

    -- Discretize time in quanta of size dt.

    -- Update the position of each particle after every dt units of time, and check for overlaps.

    -- If overlap, roll back the clock to the time of the collision, update the velocities of the colliding particles, and continue the simulation.

 

    Main drawbacks:

    -- ~ N^2 / 2 overlap checks per time quantum.

    -- Simulation is too slow if dt is very small.

    -- May miss collisions if dt is too large. (if colliding particles fail to overlap when we are looking)



 

 

4.  Event-Driven Simulation : 

    Change state only when something happens.

    -- Between collisions, particles move in straight-line trajectories.

    -- Focus only on times when collisions occur.

    -- Maintain PQ of collision events, prioritized by time.

    -- Remove the min = get next collision.

   Collision prediction.

   -- Particle i: radius si, position (rxi, ryi), velocity (vxi, vyi).

   -- Particle j: radius sj, position (rxj, ryj), velocity (vxj, vyj).

   -- Will particles i and j collide? If so, when?

    

 

    Collision resolution.

    -- If collision occurs, update colliding particle(s) according to laws of elastic collisions.



 

 

5.  Collision system :

    Initialization:

    -- Fill PQ with all potential particle-wall collisions.

    -- Fill PQ with all potential particle-particle collisions.

    Main loop:

    -- Delete the impending event from PQ (min priority = t).

    -- If the event has been invalidated, ignore it.

    -- Advance all particles to time t, on a straight-line trajectory.

    -- Update the velocities of the colliding particle(s).

    -- Predict future particle-wall and particle-particle collisions involving the colliding particle(s) and insert events onto PQ.

/**
Conventions.
-- Neither particle null ⇒ particle-particle collision.
-- One particle null ⇒ particle-wall collision.
-- Both particles null ⇒ redraw event.
*/
private class Event implements Comparable<Event>
{
    private double time; // time of event
    private Particle a, b; // particles involved in event
    private int countA, countB; // collision counts for a and b, used to check whether the collsion is still valid
    public Event(double t, Particle a, Particle b) { }
    public int compareTo(Event that)
   {return this.time - that.time; }
    public boolean isValid() { }
}

 

public class Particle
{
    private double rx, ry; // position
    private double vx, vy; // velocity
    private final double radius; // radius
    private final double mass; // mass
    private int count; // number of collisions
    public Particle(...) { }
    public void move(double dt) { }
    public void draw() { }
    public double timeToHit(Particle that) {
        if (this == that) return INFINITY;
        double dx = that.rx - this.rx, dy = that.ry - this.ry;
        double dvx = that.vx - this.vx; dvy = that.vy - this.vy;
        double dvdr = dx*dvx + dy*dvy;
        if( dvdr > 0) return INFINITY;
        double dvdv = dvx*dvx + dvy*dvy;
        double drdr = dx*dx + dy*dy;
        double sigma = this.radius + that.radius;
        double d = (dvdr*dvdr) - dvdv * (drdr - sigma*sigma);
        if (d < 0) return INFINITY;
        return -(dvdr + Math.sqrt(d)) / dvdv;
    }
    public double timeToHitVerticalWall() { }
    public double timeToHitHorizontalWall() { }
    public void bounceOff(Particle that) {
        double dx = that.rx - this.rx, dy = that.ry - this.ry;
        double dvx = that.vx - this.vx, dvy = that.vy - this.vy;
        double dvdr = dx*dvx + dy*dvy;
        double dist = this.radius + that.radius;
        double J = 2 * this.mass * that.mass * dvdr / ((this.mass + that.mass) * dist);
        double Jx = J * dx / dist;
        double Jy = J * dy / dist;
        this.vx += Jx / this.mass;
        this.vy += Jy / this.mass;
        that.vx -= Jx / that.mass;
        that.vy -= Jy / that.mass;
        this.count++;
        that.count++;
    }
    public void bounceOffVerticalWall() { }
    public void bounceOffHorizontalWall() { }
}

 

public class CollisionSystem
{
    private MinPQ<Event> pq; // the priority queue
    private double t = 0.0; // simulation clock time
    private Particle[] particles; // the array of particles
    public CollisionSystem(Particle[] particles) { }
    private void predict(Particle a)
    {
        if (a == null) return;
        for (int i = 0; i < N; i++)
        {
            double dt = a.timeToHit(particles[i]);
            pq.insert(new Event(t + dt, a, particles[i]));
        }
        pq.insert(new Event(t + a.timeToHitVerticalWall() , a, null));
        pq.insert(new Event(t + a.timeToHitHorizontalWall(), null, a));
    }

    private void redraw() { }
    public void simulate() {
        pq = new MinPQ<Event>();
        for(int i = 0; i < N; i++) predict(particles[i]);
        pq.insert(new Event(0, null, null));
        while(!pq.isEmpty())
        {
            Event event = pq.delMin();
            if(!event.isValid()) continue;
            Particle a = event.a;
            Particle b = event.b;
            for(int i = 0; i < N; i++)
                particles[i].move(event.time - t);
            t = event.time;
            if (a != null && b != null) a.bounceOff(b);
            else if (a != null && b == null) a.bounceOffVerticalWall();
            else if (a == null && b != null) b.bounceOffHorizontalWall();
            else if (a == null && b == null) redraw();
            predict(a);
            predict(b);
        }
    }
}

 

  • 大小: 16 KB
  • 大小: 21.4 KB
  • 大小: 26.3 KB
分享到:
评论

相关推荐

    Event-Driven-Simulation-of-Priority-Based-Queuing-System:具有优先级排队功能的MMmk排队系统,用于收集绩效指标,这将帮助组织跟踪,确定优先次序并确保有效地向其客户提供服务和交易

    基于优先级的排队系统的事件驱动模拟 该项目为具有两类客户的排队网络实现了仿真。 网络中有两个队列。 假定队列容量是无限的,并且每个队列都有一个服务器。 每个队列中的高优先级客户始终在每个队列中的低优先级...

    自动化专业英语常用词汇.doc

    电磁干扰embedded system 嵌入式系统encoder 编码器end effector 末端执行器error budget 误差预算error correction 纠错event-driven simulation 事件驱动模拟evolutionary algorithm 进化算法expert system 专家...

    Writing_Testbench.pdf

    12. 激励和响应(Stimulus and Response)、事件驱动仿真(Event-Driven Simulation)和周期基础仿真(Cycle-Based Simulation):激励和响应是仿真的输入和输出。事件驱动仿真处理设计中的所有事件,而周期基础仿真...

    Writing Testbenches using System Verilog

    Event-Driven Simulation . . . . . . . . . . 31 Cycle-Based Simulation . . . . . . . . . . . 33 Co-Simulators . . . . . . 35 Verification Intellectual Property . . . 38 Waveform Viewers 39 Code ...

    Analysis of fMRI Data

    Abstract—Clustering analysis is a promising data-driven method for analyzing functional magnetic resonance imaging (fMRI) time series data. The huge computational load, however, creates practical ...

    Bank_simulation.rar_MCS_event driven process

    (银行)服务-客户交互的蒙特卡洛模拟 消息驱动机制 配置柜台数和客户特征,输出平均队列长度和平均等待时间。

    python for finance

    - **Event-driven Programming**: Designing event-driven systems to trigger trades based on predefined conditions. - **High-Frequency Trading (HFT)**: Understanding the principles and challenges of HFT,...

    OrderBook-Simulation:该程序模拟股票订单簿

    3. 事件驱动(Event Driven):模拟市场中的各种事件,如新订单的提交、部分或全部订单的匹配成交、撤单等。这些事件会触发订单簿的更新。 4. 匹配引擎(Matching Engine):负责查找并执行可以匹配的买卖订单,例如...

    PyPI 官网下载 | helics-3.0.1-py2-none-macosx_10_9_x86_64.whl

    Helics(Hybrid Event-driven and Logical Clocked Integration Co-simulation)是一个开源库,专门设计用于跨多种计算环境和时间同步模型进行协同仿真。它支持异构系统之间的数据交互,允许不同仿真工具和模型在一...

    共享单车matlab程序源代码

    在"共享单车matlab程序源代码"这个项目中,开发者可能运用了离散事件仿真(Discrete Event Simulation, DES)方法来模拟共享单车的使用过程。DES是一种模拟技术,用于研究系统中离散发生的事件及其相互影响。在这个...

    Python库 | FeLS-1.2.9.tar.gz

    FeLS,全称为Flexible Event-Driven Library for Simulation,是一个基于Python开发的语言,专门用于构建灵活的事件驱动模拟系统。在后端开发中,这样的库往往用于创建复杂的模型,尤其是在需要模拟现实世界行为或...

    Verilog HDL入门教程(华为).pdf

    - 事件驱动(event-driven)编程在某些复杂场景下非常有用。 6. **代码审查与优化**: - 华为强调代码审查,以确保代码质量,这包括检查代码的正确性、效率和风格一致性。 - 代码优化旨在减少资源消耗,提高速度...

    Pioneer_Simulation

    例如,事件驱动编程(Event-Driven Programming)可能被用来处理异步事件和交互,使模拟更加动态和真实。同时,为了实现高效的模拟,可能会运用到蒙特卡洛方法,这是一种通过随机抽样来解决问题的技术,常用于统计...

    Booksim NOC评估代码

    - **Event-Driven Router**(事件驱动路由器): 根据事件触发机制处理数据包。 不同的路由器组织方式会影响到数据包的处理效率及复杂度。 ##### 3.6 分配器 分配器用于决定数据包如何从输入端口转发至输出端口,...

    Verilog基本语法

    8. **事件驱动(Event-driven)**:Verilog设计是事件驱动的,意味着行为取决于特定事件的发生,如时钟边沿检测。 9. **进程(Always Blocks)**:`always`块用于定义时序逻辑,通常分为两种:`@(*)`(任意事件触发...

    ns by example

    NS (version 2) is an object-oriented, discrete event driven network simulator developed at UC Berkely written in C++ and OTcl. NS is primarily useful for simulating local and wide area networks. ...

    ETA:ETA是一种用于事件标签处理的图形事件驱动编程语言

    ETA,全称未在描述中明确给出,可能是Event-Tagging Algorithm或类似的缩写,是一种专为处理事件标签设计的图形事件驱动编程语言。这种编程语言的核心在于其事件驱动特性,即程序的执行由发生的特定事件来触发,而...

Global site tag (gtag.js) - Google Analytics