首页 > 其他分享 >Project0 Nbody-simulation

Project0 Nbody-simulation

时间:2024-04-29 10:55:52浏览次数:26  
标签:yyPos xxPos double Project0 Planet other simulation public Nbody

讲义:NBody Simulation | CS 61B Spring 2018

为什么使用Git命令+javac??

答:
这样可以单独的编译和运行某一个程序,而IDE编译一个程序的时候还会编译其它的程序,会很烦人

翻译时绕过数学公式

goole浏览器按F12进入网页控制台,输入$('math, .math, .MathJax').attr('translate','no');
image.png

javac编译的时候注意事项

javac -encoding UTF-8 Planet.java TestCalcForceExertedBy.java	//有中文
javac TestCalcForceExertedBy.java	//无中文

增强的for循环

public class EnhancedForBreakDemo {
    public static void main(String[] args) {
        String[] a = {"cat", "dog", "laser horse", "ketchup", "horse", "horbse"};

        for (String s : a) {
            for (int j = 0; j < 3; j += 1) {
                System.out.println(s);
                if (s.contains("horse")) {
                    break;
                }                
            }
        }
    }
}

可视化代码,超好用
Online Java Compiler, Visual Debugger, and AI Tutor - Learn Java programming by visualizing code

Planets类

import java.security.PublicKey;

public class Planet {
    public double xxPos;
    public double yyPos;
    public double xxVel;
    public double yyVel;
    public double mass;
    public String imgFileName;
    public Planet(double xP,double yP,double xV,double yV,double m,String img){
            this.xxPos=xP;
            this.yyPos=yP;
            this.xxVel=xV;
            this.yyVel=yV;
            this.mass=m;
            this.imgFileName=img;
    }
//    public Planet(){
//        this(0,0,0,0,0,null);
//    }
    public Planet(Planet p){
        this(p.xxPos,p.yyPos,p.xxVel,p.yyVel,p.mass,p.imgFileName);
    }
    /*计算两个物体之间的距离的平方*/
    public double calcDistance(Planet other){
        return Math.sqrt((this.xxPos-other.xxPos)*(this.xxPos- other.xxPos)+(this.yyPos-other.yyPos)*(this.yyPos-other.yyPos));
    }
    /*计算两个物体之间的受力,牛顿万有引力*/
    public double calcForceExertedBy(Planet other){
        double G=6.67e-11;
        return (G*(this.mass*other.mass))/(this.calcDistance(other)*this.calcDistance(other));
    }
    //this代表受力的物体,other代表施力的物体
    //X分力
    public double calcForceExertedByX(Planet other){
        double F=this.calcForceExertedBy(other);
        double r=this.calcDistance(other);
        return F*(other.xxPos-this.xxPos)/r;
    }
    //Y方向上的分力
    public double calcForceExertedByY(Planet other){
        double F=this.calcForceExertedBy(other);
        double r=this.calcDistance(other);
        return F*(other.yyPos-this.yyPos)/r;
    }
    //计算受力物体受其他物体X方向上的力
    public double calcNetForceExertedByX(Planet[] planets){
        double xNetForce=0.0;
        for(Planet p:planets){
            if(!this.equals(p))
                xNetForce+=this.calcForceExertedByX(p);
        }
        return xNetForce;
    }
    //计算受力物体受其它物体Y方向的力
    public double calcNetForceExertedByY(Planet[] planets){
        double yNetForce=0;
        for(Planet p:planets){
            if(!this.equals(p))
                yNetForce+=this.calcForceExertedByY(p);
        }
        return yNetForce;
    }
    //更新速度,牛顿第二定理
    //已知x,y方向的分力以及作用的时间,求出变化后的速度

    public void update(double time,double xForce,double yForce){
        //由F=ma先求出a,然后v=v0+a*t;
        double ax=xForce/this.mass;
        double ay=yForce/this.mass;
        this.xxVel=this.xxVel+ax*time;
        this.yyVel=this.yyVel+ay*time;
        //讲义给的公式:p_new=p_old+v_new*dt
        //我高中学的:p_new=p_old+0.5*a*t*t;算了按讲义来吧
        this.xxPos=this.xxPos+this.xxVel*time;
        this.yyPos=this.yyPos+this.yyVel*time;
    }
    public void draw(){
        StdDraw.picture(this.xxPos,this.yyPos,"images/"+this.imgFileName);
    }
}

public class NBody {
    //返回宇宙的半径
    public static double readRadius(String Filename){
        In in=new In(Filename);
        int N=in.readInt();
        return in.readDouble();
    }
    //返回行星数组
    public static Planet[] readPlanets(String Filename){
        In in=new In(Filename);
        int N=in.readInt();
        Planet[] planets=new Planet[N];
        double radius=in.readDouble();
        for(int i=0;i<N;i++){
            double xxPos =in.readDouble() ,
                    yyPos = in.readDouble(),
                    xxVel = in.readDouble(),
                    yyVel = in.readDouble(),
                    mass =in.readDouble();
            String imageFile=in.readString();
            Planet p=new Planet(xxPos,yyPos,xxVel,yyVel,mass,imageFile);
            planets[i]=p;
        }
        return planets;
    }

    public static void main(String[] args){
        double T=Double.parseDouble(args[0]);
        double dt=Double.parseDouble(args[1]);
        String filename="./"+args[2];
        Double Radius=readRadius(filename);
        Planet[] planets=readPlanets(filename);
        int N=planets.length;
        StdDraw.setXscale(-Radius,Radius);
        StdDraw.setYscale(-Radius,Radius);
        StdDraw.enableDoubleBuffering();
        //StdDraw.picture(0,0,"images/starfield.jpg");
        //for(int i=0;i<N;i++)
        //    planets[i].draw();

        double t=0;
        while(t<=T){
            double[] xForces=new double[N];
            double[] yForces=new double[N];
            for(int i=0;i<N;i++){
                xForces[i]=planets[i].calcNetForceExertedByX(planets);
                yForces[i]=planets[i].calcNetForceExertedByY(planets);
            }
            for (int i=0;i<N;i++){
                planets[i].update(dt,xForces[i],yForces[i]);
            }
            StdDraw.picture(0,0,"images/starfield.jpg");
            for(int i=0;i<N;i++){
                planets[i].draw();
            }
            StdDraw.show();
            StdDraw.pause(10);
            t+=dt;
        }
        StdOut.printf("%d\n", planets.length);
        StdOut.printf("%.2e\n", Radius);
        for (int i = 0; i < planets.length; i++) {
            StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
                    planets[i].xxPos, planets[i].yyPos, planets[i].xxVel,
                    planets[i].yyVel, planets[i].mass, planets[i].imgFileName);
        }
    }
}

恰当的位置绘制行星于背景

        StdDraw.setXscale(-Radius,Radius);
        StdDraw.setYscale(-Radius,Radius);
        StdDraw.picture(0,0,"images/starfield.jpg");
        for(int i=0;i<N;i++)
            planets[i].draw();

image.png

标签:yyPos,xxPos,double,Project0,Planet,other,simulation,public,Nbody
From: https://www.cnblogs.com/ahnultq/p/18165205

相关文章

  • 52 Things: Number 32: difference between game-based and simulation-based securit
    52Things:Number32:differencebetweengame-basedandsimulation-basedsecuritydefinitions52件事:数字32:基于游戏和基于模拟的安全定义之间的区别 Thisisthelatestinaseriesofblogpoststoaddressthelistof'52ThingsEveryPhDStudentShouldKnowt......
  • HSPF(Hydrological Simulation Program Fortran)模型
    HSPF模型与SWAT模型一样都是著名的水文模型软件,在世界各地的水文模拟中得到广泛的应用。由于种种原因,HSPF模型在国内的影响力不如SWAT;但是,HSPF模型也有其自身的优势,比如:1.它有很高集成度的前后处理软件,减轻建模的负担;2.它可以自主调节水文响应单元的大小,模型有更好的灵活性;3.它......
  • CMU-15445(Fall 2023) Project0 C++ Primer 个人笔记
    CMU-15445Project0c++语法问题我直接问的gpt测试文件测试文件都存放在/bustub-private/test目录下,可以自己修改里边的测试方法并且查看有哪些特殊情况需要处理。Task1Get方法使用一个cur节点指向当前正在查找的节点,index指向当前当前正在查找的字符,在children_中查找key[......
  • simulation roadmap
    thenextstepwouldbedevelopinganewmodellanguage.SIMANPegden,C.D.,IntroductiontoSIMAN,WinterSimulationConference1983.https://informs-sim.org/wsc83papers/1983_0004.pdfSturrock,D.T.,Pegden,C.D.,IntroductiontoSIMAN,ProceedingsWinte......
  • 利用SOLIDWORKS Flow Simulation来进行旋转流体仿真
    前段时间,一个朋友去到一家做水泵的行业,问我SOLIDWORKS能够做流体仿真么?我说,能啊。朋友又问,我现在做水泵,里面的叶片旋转,可以模拟么?我说,当然可以了啊。那么,我就做了个小例子给他,首先,我先建了个如下图所示模型,当然真正的泵不是这样的,我这个,只是玩具,甚至连玩具都称不上。  看到......
  • Cloth Simulation with Root Finding and Optimization
    目录0前言1ImplicitMethod1.1Root-finding1.2Optimization1.3Insight2Newton-RaphsonMethod3Mass-SpringSystem3.1Matrixcalculus3.2ASpringwithTwoEnds4ExplainationofInitCodewith3DImage4.1Initialize4.2Index4.3Edge4.4SortEdge5Update6Get_G......
  • Microsoft 365 新功能速递:Microsoft 365 Data loss prevention simulation mode
    51CTOBlog地址:https://blog.51cto.com/u_13969817预计2024年2月底,Microsoft365将推出DLP的新功能:Simulationmode ,可以为DLP管理员提供了一种独立的体验,可以尝试DLP策略,评估其影响,并建立对策略有效性的信心,从而最终减少策略执行的时间。Simulationmode 是对现有测试模式行为......
  • 技术美术|游戏中的流体模拟(Fluid Simulation)
    【USparkle专栏】如果你深怀绝技,爱“搞点研究”,乐于分享也博采众长,我们期待你的加入,让智慧的火花碰撞交织,让知识的传递生生不息!一、闲聊最近一直在研究流体模拟,很神奇的一个东西,好在网上有很多参考资料,研究过程不算太困难。分享下最近一段时间的学习心得。二、效果演示 ......
  • How to Use Docker and NS-3 to Create Realistic Network Simulations
    https://insights.sei.cmu.edu/blog/how-to-use-docker-and-ns-3-to-create-realistic-network-simulations/ HowtoUseDockerandNS-3toCreateRealisticNetworkSimulationsALEJANDROGOMEZMARCH27,2023Sometimes,researchersanddevelopersneedt......
  • InBody 组件 将dom挂载到body
    <template><div><transitionv-bind:name="transName"appear><slot></slot></transition></div></template><script>exportdefault{data(){return{......