C# 中的 N 体模拟

2024-03-10

我正在尝试使用 Runge Kutta 4 或 Velocity Verlet 集成算法在 C# 中实现 N 体模拟。

在我转向更多数量的粒子之前,我想通过模拟地球绕太阳的轨道来测试模拟,但是,由于某种原因,我得到的不是椭圆轨道,而是一个奇怪的螺旋。

我无法解决这个问题,因为我使用相同的算法对太阳系进行了更简单的模拟,其中太阳固定在位置并且一切都运行良好。积分器工作得很好,因为无论我使用哪一个,我都会得到螺旋。

任何帮助,将不胜感激。

这是代码:

class NBODY
{
    public static double G = 4 * Math.PI * Math.PI;

    class Particle
    {
        public double[] r;       // position vector
        public double[] v;       // velocity vector
        public double mass;    

        //constructor
        public Particle() {}
        public Particle(double x, double y, double z, double vx, double vy, double vz, double m)
        {
            this.r = new double[3];
            this.v = new double[3];

            this.r[0] = x;
            this.r[1] = y;
            this.r[2] = z;
            this.v[0] = vx;
            this.v[1] = vy;
            this.v[2] = vz;
            this.mass = m;
        }

        public void Update(Particle[] particles, double t, double h, int particleNumber)
        {
            RungeKutta4(particles, t, h, particleNumber);
        }

        private double acc(double r, Particle[] particles, int particleNumber, double[] r_temp, int l)
        {

            // dv/dt = f(x) = -G * m_i * (x - x_i) / [(x - x_i)^2 + (y - y_i)^2 + (z - z_i)^2]^(3/2)

            double sum = 0;

            switch (l)
            {
                case 0:
                    for (int i = 0; i < particles.Length; i++)
                        if (i != particleNumber)
                            sum += particles[i].mass * (r - particles[i].r[l]) / Math.Pow( Math.Pow(r - particles[i].r[l], 2)
                                + Math.Pow(r_temp[1] - particles[i].r[1], 2) + Math.Pow(r_temp[2] - particles[i].r[2], 2), 1.5);
                    break;

                case 1:
                    for (int i = 0; i < particles.Length; i++)
                        if (i != particleNumber)
                            sum += particles[i].mass * (r - particles[i].r[l]) / Math.Pow(Math.Pow(r - particles[i].r[l], 2)
                                + Math.Pow(r_temp[0] - particles[i].r[0], 2) + Math.Pow(r_temp[2] - particles[i].r[2], 2), 1.5);
                    break;

                case 2:
                    for (int i = 0; i < particles.Length; i++)
                        if (i != particleNumber)
                            sum += particles[i].mass * (r - particles[i].r[l]) / Math.Pow(Math.Pow(r - particles[i].r[l], 2)
                                + Math.Pow(r_temp[0] - particles[i].r[0], 2) + Math.Pow(r_temp[1] - particles[i].r[1], 2), 1.5);
                    break;
            }

            return -G * sum;
        }

        private void RungeKutta4(Particle[] particles, double t, double h, int particleNumber)
        {
            //current position of the particle is saved in a vector
            double[] r_temp = new double[3];

            for (int j = 0; j < 3; j++)
                r_temp[j] = this.r[j];

            //loop going over all the coordinates and updating each using RK4 algorithm
            for (int l = 0; l < 3; l++)
            {   
                double[,] k = new double[4, 2];

                k[0, 0] = this.v[l];                                                                //k1_r
                k[0, 1] = acc(this.r[l], particles, particleNumber, r_temp, l);                     //k1_v

                k[1, 0] = this.v[l] + k[0, 1] * 0.5 * h;                                            //k2_r
                k[1, 1] = acc(this.r[l] + k[0, 0] * 0.5 * h, particles, particleNumber, r_temp, l); //k2_v

                k[2, 0] = this.v[l] + k[1, 1] * 0.5 * h;                                            //k3_r
                k[2, 1] = acc(this.r[l] + k[1, 0] * 0.5 * h, particles, particleNumber, r_temp, l); //k3_v

                k[3, 0] = this.v[l] + k[2, 1] * h;                                                  //k4_r
                k[3, 1] = acc(this.r[l] + k[2, 0] * h, particles, particleNumber, r_temp, l);       //k4_v

                this.r[l] += (h / 6.0) * (k[0, 0] + 2 * k[1, 0] + 2 * k[2, 0] + k[3, 0]);
                this.v[l] += (h / 6.0) * (k[0, 1] + 2 * k[1, 1] + 2 * k[2, 1] + k[3, 1]);   
            }
        }

        /*
            Velocity Verlet algorithm:
            1. Calculate y(t+h) = y(t) + v(t)h + 0.5a(t)h*h
            2. Derive a(t+h) from dv/dt = -y using y(t+h)
            3. Calculate v(t+h) = v(t) + 0.5*(a(t) + a(t+h))*h
        */
        private void VelocityVerlet(Particle[] particles, double t, double h, int particleNumber)
        {

            double[] r_temp = new double[3];

            for (int j = 0; j < 3; j++)
                r_temp[j] = this.r[j];

            //loop going over all the coordinates and updating each using RK4 algorithm
            for (int l = 0; l < 3; l++)
            {
                //position
                this.r[l] += h * this.v[l] + 0.5 * h * h * acc(this.r[l], particles, particleNumber, r_temp, l);
                //velocity
                this.v[l] += 0.5 * h * (acc(r_temp[l], particles, particleNumber, r_temp,l)
                    + acc(this.r[l], particles, particleNumber, r_temp,l));
            }
        }     


    }

    static void Main(string[] args)
    {
        //output file
        TextWriter output = new StreamWriter("ispis.txt");

        // declarations of variables
        Particle[] particles = new Particle[2];
        particles[0] = new Particle(0, 0, 0, 0, 0, 0, 1);                  //sun
        particles[1] = new Particle(1, 0, 0, 0, 6.28, 0,  3.003467E-06);   //earth


        int N = 200;
        double h, t, tmax;
        double[,,] x = new double[particles.Length, N, 3];  //output


        //  setting initial values, step size and max time tmax
        h = 0.01;   // the step size in years
        tmax = h * N;

        // initial time        
        t = 0;

        int i = 0;

        while (t <= tmax) {

            //updates position of all particles
            for (int z = 1; z < particles.Length; z++)
                particles[z].Update(particles, t, h, z);

            //saves the position for output
            for (int j = 1; j < particles.Length ; j++)
                for (int z = 0; z < 3; z++ )
                    x[j,i,z] = particles[j].r[z];

            t += h;
            i++;
        }

        //output to file
        for (int k = 0; k < particles.Length; k++ )
        {
            for (int f = 0; f < 3; f++)

            {
                for (int l = 0; l < N; l++)
                    output.Write(string.Format("{0,-15:0.########},", x[k,l,f]));
                output.Write(string.Format("\n\n"));
            }
            output.Write(string.Format("\n\n\n\n"));
        }

        output.Close();
    }
}

这是地球轨道的输出数据图:


您的模型计算两个粒子之间的重力两次:对于第一个粒子,力基于其原始坐标,对于第二个粒子,力基于第一个粒子的更新位置。这明显违反了牛顿第三定律。在任何更新之前,您必须预先计算所有力。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 中的 N 体模拟 的相关文章

  • C 编程 - 文件 - fwrite

    我有一个关于编程和文件的问题 while current NULL if current gt Id Doctor 0 current current gt next id doc current gt Id Doctor if curre
  • “构建”构建我的项目,“构建解决方案”则不构建

    我刚刚开始使用VS2010 我有一个较大的解决方案 已从 VS2008 成功迁移 我已将一个名为 Test 的控制台应用程序项目添加到解决方案中 选择构建 gt 构建解决方案不编译新项目 选择构建 gt 构建测试确实构建了项目 在失败的情况
  • Web 客户端和 Expect100Continue

    使用 WebClient C NET 时设置 Expect100Continue 的最佳方法是什么 我有下面的代码 我仍然在标题中看到 100 continue 愚蠢的 apache 仍然抱怨 505 错误 string url http
  • 用于检查类是否具有运算符/成员的 C++ 类型特征[重复]

    这个问题在这里已经有答案了 可能的重复 是否可以编写一个 C 模板来检查函数是否存在 https stackoverflow com questions 257288 is it possible to write a c template
  • 从Web API同步调用外部api

    我需要从我的 Web API 2 控制器调用外部 api 类似于此处的要求 使用 HttpClient 从 Web API 操作调用外部 HTTP 服务 https stackoverflow com questions 13222998
  • C# 中通过 Process.Kill() 终止的进程的退出代码

    如果在我的 C 应用程序中 我正在创建一个可以正常终止或开始行为异常的子进程 在这种情况下 我通过调用 Process Kill 来终止它 但是 我想知道该进程是否已退出通常情况下 我知道我可以获得终止进程的错误代码 但是正常的退出代码是什
  • C#中如何移动PictureBox?

    我已经使用此代码来移动图片框pictureBox MouseMove event pictureBox Location new System Drawing Point e Location 但是当我尝试执行时 图片框闪烁并且无法识别确切
  • C++ OpenSSL 导出私钥

    到目前为止 我成功地使用了 SSL 但遇到了令人困惑的障碍 我生成了 RSA 密钥对 之前使用 PEM write bio RSAPrivateKey 来导出它们 然而 手册页声称该格式已经过时 实际上它看起来与通常的 PEM 格式不同 相
  • 创建链表而不将节点声明为指针

    我已经在谷歌和一些教科书上搜索了很长一段时间 我似乎无法理解为什么在构建链表时 节点需要是指针 例如 如果我有一个节点定义为 typedef struct Node int value struct Node next Node 为什么为了
  • 将多个表映射到实体框架中的单个实体类

    我正在开发一个旧数据库 该数据库有 2 个具有 1 1 关系的表 目前 我为每个定义的表定义了一种类型 1Test 1Result 我想将这些特定的表合并到一个类中 当前的类型如下所示 public class Result public
  • 如何设计以 char* 指针作为类成员变量的类?

    首先我想介绍一下我的情况 我写了一些类 将 char 指针作为私有类成员 而且这个项目有 GUI 所以当单击按钮时 某些函数可能会执行多次 这些类是设计的单班在项目中 但是其中的某些函数可以执行多次 然后我发现我的项目存在内存泄漏 所以我想
  • 转发声明和包含

    在使用库时 无论是我自己的还是外部的 都有很多带有前向声明的类 根据情况 相同的类也包含在内 当我使用某个类时 我需要知道该类使用的某些对象是前向声明的还是 include d 原因是我想知道是否应该包含两个标题还是只包含一个标题 现在我知
  • 控件的命名约定[重复]

    这个问题在这里已经有答案了 Microsoft 在其网站上提供了命名指南 here http msdn microsoft com en us library xzf533w0 VS 71 aspx 我还有 框架设计指南 一书 我找不到有关
  • 链接器错误:已定义

    我尝试在 Microsoft Visual Studio 2012 中编译我的 Visual C 项目 使用 MFC 但出现以下错误 error LNK2005 void cdecl operator new unsigned int 2
  • 如何使用 C# / .Net 将文件列表从 AWS S3 下载到我的设备?

    我希望下载存储在 S3 中的多个图像 但目前如果我只能下载一个就足够了 我有对象路径的信息 当我运行以下代码时 出现此错误 遇到错误 消息 读取对象时 访问被拒绝 我首先做一个亚马逊S3客户端基于我的密钥和访问配置的对象连接到服务器 然后创
  • 向现有 TCP 和 UDP 代码添加 SSL 支持?

    这是我的问题 现在我有一个 Linux 服务器应用程序 使用 C gcc 编写 它与 Windows C 客户端应用程序 Visual Studio 9 Qt 4 5 进行通信 是什么very在不完全破坏现有协议的情况下向双方添加 SSL
  • 为什么编译时浮点计算可能不会得到与运行时计算相同的结果?

    In the speaker mentioned Compile time floating point calculations might not have the same results as runtime calculation
  • 测试用例执行完成后,无论是否通过,如何将测试用例结果保存在变量中?

    我正在使用 NUNIT 在 Visual Studio 中使用 Selenium WebDriver 测试用例的代码是 我想在执行测试用例后立即在变量中记录测试用例通过或失败的情况 我怎样才能实现这一点 NUnit 假设您使用 NUnit
  • 在一个区域中拟合二维多边形的算法?

    这有标准吗 算法名称 说 我有 10 个不同大小的多边形 我有一个特定大小的区域 我想知道如何填充该区域中的最多多边形 以及它们是如何拟合的 笔记 多边形可以根据限制集进行旋转 一个可能的名称是包装问题 http en wikipedia
  • 对来自流读取器的过滤数据执行小计

    编辑问题未得到解答 我有一个基于 1 个标准的过滤输出 前 3 个数字是 110 210 或 310 给出 3 个不同的组 从流阅读器控制台 问题已编辑 因为第一个答案是我给出的具体示例的字面解决方案 我使用的实际字符串长度为 450 个

随机推荐

  • 通过连接字符串连接到远程数据库

    我正在尝试让我的程序从连接在同一 LAN 网络 内联网 中的另一台计算机读取访问数据库 这是我正在使用的代码 namespace CalUnderFoot public partial class Window1 Window CarsDB
  • Angular mat-selection-list,如何使单个复选框选择类似于单选按钮?

    如何使单个复选框选择mat selection list 类似于单选按钮 它接受一组值中的一个值 组件 gt 9 1 0 选择列表现在直接支持单选模式 将多个输入设置为 false 来启用它
  • 如何取消 NSBlockOperation

    我有一个很长的运行循环 我想在后台运行NSOperation 我想使用一个块 NSBlockOperation operation NSBlockOperation blockOperationWithBlock while not can
  • Swift 中处理窗口关闭事件

    如何使用swift处理窗口的关闭事件 例如询问 您确定要关闭表单吗 如果 是 则该表格将关闭 如果 否 该表格将不关闭 显示消息框对我来说不是问题 viewWillDisappear 也适用于最小化 但我只需要关闭事件 Thanks 就像上
  • 为什么 Laravel Redis::scan('*') 返回预期的键,但 Redis::keys('*') 没有返回?

    Problem 我使用 Python 代码向 redis 添加了一个值 当我尝试查询时使用 Laravel Redis get key name 它返回null Redis keys 返回使用 Laravel 但不使用 Python 创建的
  • limitTo 在 AngularJs 的 ng-repeat 中不起作用

    我正在为某些应用程序编写一些代码 我想限制聊天中的消息 this limitM 10 scope msgsCount contains the number of messages
  • 将复杂类型作为数据传递给 jquery ajax post

    我的数据模型类如下所示 DataContract public class Order DataMember public string Id get set DataMember public string AdditionalInstr
  • CF 标志的行为难以理解

    假设有一段代码 mov al 12 mov bl 4 sub al bl 在这种情况下 CF 0 标志 但在我看来它应该等于 1 因为减法运算是在加法运算上实现的 并且处理器不知道我们将其作为输入提供什么 无论是有符号还是无符号数字 它只是
  • 如何告诉窗体在关​​闭时不要释放特定控件?

    我想为我的子类表单对象编写一个函数 该函数必须关闭窗体并返回该窗体上的控件 以便我可以将其放在另一个窗体上 我无法阻止控件被处置 我认为使用 this Controls Remove someControl 从控件集合中删除它足以阻止它处置
  • Spring security中每个请求不同的csrf令牌

    我在用
  • 具有重载赋值的嵌套派生类型

    我有一个派生类型 wrapper 包含其他派生类型 over 对于后者 赋值运算符已被重载 由于派生类型的分配按默认组件方式发生 因此我希望分配两个实例wrapper将调用重载分配over在某一点 然而 使用下面的程序 情况似乎并非如此 仅
  • SelectById2 的指针标注

    我正在尝试将我在 VBA 中编写的一些代码移植到 Python 中以控制 Solidworks 特别是自动化草图编辑 我在 Python 中使用 Solidworks SelectById2 时遇到问题 在 VBA 中 以下代码工作正常 P
  • PHP continue 函数内

    这可能非常微不足道 但我一直无法弄清楚 这有效 function MyFunction Do stuff foreach x as y MyFunction if foo bar continue Do stuff echo output
  • Java 并发:Synchronized(this) => 和 this.wait() 和 this.notify()

    我非常感谢您帮助理解以下内容的 并发示例 http forums sun com thread jspa threadID 735386 http forums sun com thread jspa threadID 735386 pub
  • 计算时间跨度的最佳方法是什么

    在我的 C 程序中 我的要求是计算 foreach 循环内的业务逻辑执行的时间跨度 我必须存储时间跨度 我正在使用以下代码 for int i 0 i lt 100 i DateTime start DateTime Now Busines
  • Docker 信任初始化

    当对 tuf 上的公证人对 docker 内容信任的初始信任初始化时 我了解了 TUF 公证人和内容信任的工作原理 但我不清楚的是 最初的信任是如何建立的 我怎么知道第一次拉取没有受到损害并且初始 root json 是值得信赖的 例如 如
  • 使用foldr实现zip

    我目前正在阅读 Real World Haskell 的第 4 章 我正在努力理清思路根据foldr 实现foldl http book realworldhaskell org read functional programming ht
  • 寻找一种非“蛮力”算法来删除矩形集合的相交区域

    我有一个 n 大小的矩形集合 其中大部分彼此相交 我想删除相交并将相交的矩形减少为较小的非相交的矩形 我可以轻松地暴力破解解决方案 但我正在寻找一种有效的算法 这是一个可视化 原来的 处理 理想情况下 方法签名如下所示 public sta
  • 如何解决以下 SDK 版本报告了严重问题:com.google.android.gms:play-services-safetynet:17.0.0

    我在发布应用程序时收到此警告 play services safetynet 的开发商 com google android gms play services safetynet 已报告严重 版本 17 0 0 存在问题 在发布新版本之前
  • C# 中的 N 体模拟

    我正在尝试使用 Runge Kutta 4 或 Velocity Verlet 集成算法在 C 中实现 N 体模拟 在我转向更多数量的粒子之前 我想通过模拟地球绕太阳的轨道来测试模拟 但是 由于某种原因 我得到的不是椭圆轨道 而是一个奇怪的