Пример #1
0
    void OnTriggerStay(Collider other)
    {
        LennardJonesParticle other_lj = other.GetComponent <LennardJonesParticle>();

        if (other_lj == null)
        {
            return;
        }

        Vector3 dist_vec   = other.attachedRigidbody.position - transform.position;
        float   sigma      = sphere_radius + other_lj.sphere_radius;
        float   rinv       = 1.0f / dist_vec.magnitude;
        float   r1s1       = sigma * rinv;
        float   r3s3       = r1s1 * r1s1 * r1s1;
        float   r6s6       = r3s3 * r3s3;
        float   r12s12     = r6s6 * r6s6;
        float   derivative = 24.0f * epsilon * (r6s6 - 2.0f * r12s12) * rinv;

        m_Rigidbody.AddForce(derivative * rinv * dist_vec);
    }
    // Start is called before the first frame update
    void Start()
    {
        // read input file
        string    input_file_path = Application.dataPath + "/../input/input.toml";
        TomlTable root            = Toml.ReadFile(input_file_path);

        // generate initial particle position, velocity and system temperature
        List <TomlTable> systems = root.Get <List <TomlTable> >("systems");

        if (2 <= systems.Count)
        {
            throw new System.Exception($"There are {systems.Count} systems. the multiple systems case is not supported.");
        }
        List <LennardJonesParticle> ljparticles = new List <LennardJonesParticle>();

        float[] upper_boundary = new float[3];
        float[] lower_boundary = new float[3];
        m_NormalizedRandom = new NormalizedRandom();
        foreach (TomlTable system in systems)
        {
            temperature = system.Get <TomlTable>("attributes").Get <float>("temperature");
            if (system.ContainsKey("boundary_shape"))
            {
                TomlTable boundary_shape = system.Get <TomlTable>("boundary_shape");
                upper_boundary = boundary_shape.Get <float[]>("upper");
                lower_boundary = boundary_shape.Get <float[]>("lower");
            }
            else
            {
                throw new System.Exception("There is no boundary_shape information. UnlimitedBoundary is not supported now.");
            }
            List <TomlTable> particles = system.Get <List <TomlTable> >("particles");
            foreach (TomlTable particle_info in particles)
            {
                // initialize particle position
                float[] position = particle_info.Get <float[]>("pos");
                LennardJonesParticle new_particle =
                    Instantiate(m_LJParticle,
                                new Vector3(position[0], position[1], position[2]),
                                transform.rotation);

                // initialize particle velocity
                Rigidbody new_rigid = new_particle.GetComponent <Rigidbody>();
                new_rigid.mass = particle_info.Get <float>("m");
                if (particle_info.ContainsKey("vel"))
                {
                    float[] velocity = particle_info.Get <float[]>("vel");
                    new_rigid.velocity = new Vector3(velocity[0], velocity[1], velocity[2]);
                }
                else
                {
                    float sigma = Mathf.Sqrt(kb * temperature / new_rigid.mass);
                    new_rigid.velocity = new Vector3(m_NormalizedRandom.Generate(0.0f, sigma),
                                                     m_NormalizedRandom.Generate(0.0f, sigma),
                                                     m_NormalizedRandom.Generate(0.0f, sigma));
                }
                ljparticles.Add(new_particle);
            }
        }
        Debug.Log("System initialization finished.");

        List <TomlTable> ffs = root.Get <List <TomlTable> >("forcefields");

        foreach (TomlTable ff in ffs)
        {
            List <TomlTable> global_ffs = ff.Get <List <TomlTable> >("global");
            foreach (TomlTable global_ff in global_ffs)
            {
                Assert.AreEqual("LennardJones", global_ff.Get <string>("potential"),
                                "The potential field is only allowed \"LennardJones\". Other potential or null is here.");
                List <TomlTable> parameters = global_ff.Get <List <TomlTable> >("parameters");
                foreach (TomlTable parameter in parameters)
                {
                    int   index  = parameter.Get <int>("index");
                    float sigma  = parameter.Get <float>("sigma"); // sigma correspond to diameter.
                    float radius = sigma / 2;
                    ljparticles[index].sphere_radius        = radius;
                    ljparticles[index].epsilon              = parameter.Get <float>("epsilon");
                    ljparticles[index].transform.localScale = new Vector3(sigma, sigma, sigma);
                }
            }
        }

        // Initialize SystemManager
        m_SystemManager = GetComponent <SystemManager>();
        m_SystemManager.Init(ljparticles,
                             new Vector3(upper_boundary[0], upper_boundary[1], upper_boundary[2]),
                             new Vector3(lower_boundary[0], lower_boundary[1], lower_boundary[2]));
        Debug.Log("SystemManager initialization finished.");
    }