Ressonância entre um pente de frequências e um átomo de dois níveis - programa em C#

Voltar

Programa em C# para o estudo da ressonância da interação entre um pente de frequências e um átomo de dois níveis.


Download do executável.
Download do programa.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoisNiveisWin
{
    public partial class FormDoisNiveis : Form
    {
        double q12, q2, gama, w, alpha, N, wL, w2;
        double dd, Omega, h, t, Tr, fr, phi;
        double Tp, a10, a20, x, y;
        int Pulsos, i, j, k, m, PassoRKfento, g, cor, espessura;
        double[] a = new double[5];
        double[] b = new double[5];
        double[] c = new double[5];
        double[] k1 = new double[5];
        double[] k2 = new double[5];
        double[] k3 = new double[5];
        double[] k4 = new double[5];
        double Pi = 3.141592654;
        double nb, nt;

        public double func(double a1, double a2, double a12, double b12, int i)
        {
            if (i == 1) return +2 * Omega * (b12 * Math.Cos(alpha) - a12 * Math.Sin(alpha)) + q2 * a2 - (a1 - a10) * gama;
            else if (i == 2) return -2 * Omega * (b12 * Math.Cos(alpha) - a12 * Math.Sin(alpha)) - q2 * a2 - a2 * gama;
            else if (i == 3) return -dd * b12 - Omega * (a2 - a1) * Math.Sin(alpha) - 0.5 * q2 * a12 - a12 * gama;
            else if (i == 4) return dd * a12 + Omega * (a2 - a1) * Math.Cos(alpha) - 0.5 * q2 * b12 - b12 * gama;
            else return 0;
        }

        public FormDoisNiveis()
        {
            InitializeComponent();
        }

        public void CondIniciais()
        {
            gama = 0.0025 * 1e9 * 0;
            PassoRKfento = 10;
            q2 = (2 * Pi) * 5e6;
            q12 = 0.5 * q2;
            fr = double.Parse(textBoxTaxa.Text) * 1e6;
            Tp = double.Parse(textBoxLargura.Text) * 1e-15;
            Pulsos = int.Parse(textBoxPulsos.Text);
            Omega = double.Parse(textBoxOmega.Text) * q12 / (fr * Tp);
            a10 = 1;
            a20 = 0;
            w2 = (2 * Pi) * 384e12 + (2 * Pi) * 20e6 * 0;
            wL = (2 * Pi) * 384e12 + (2 * Pi) * 40e6 * 0;
            phi = (2 * Pi) * 0.4 * 0;

            nb = nt = 0;
            b[1] = b[2] = 0;

            a[1] = a10;
            a[2] = a20;
            a[3] = 0;
            a[4] = 0;
            t = 0; N = -1;
            dd = double.Parse(textBoxDelta.Text) * 1e6 * 2 * Pi;
            Tr = 1 / fr;
        }

        public void RungeKutta()
        {
            N = N + 1;
            g = PassoRKfento;
            h = Tp / g;

            alpha = -N * wL * Tr + N * phi;

            for (k = 1; k <= g; k++)
            {
                t = t + h;

                for (j = 1; j <= 4; j++)
                {
                    k1[j] = func(a[1], a[2], a[3], a[4], j);
                }

                for (j = 1; j <= 4; j++)
                {
                    k2[j] = func(a[1] + k1[1] * h / 2, a[2] + k1[2] * h / 2, a[3] + k1[3] * h / 2,
                    a[4] + k1[4] * h / 2, j);
                }

                for (j = 1; j <= 4; j++)
                {
                    k3[j] = func(a[1] + k2[1] * h / 2, a[2] + k2[2] * h / 2, a[3] + k2[3] * h / 2,
                    a[4] + k2[4] * h / 2, j);
                }

                for (j = 1; j <= 4; j++)
                {
                    k4[j] = func(a[1] + k3[1] * h, a[2] + k3[2] * h, a[3] + k3[3] * h,
                    a[4] + k3[4] * h, j);
                }

                for (j = 1; j <= 4; j++)
                {
                    b[j] = a[j] + h * (k1[j] / 6 + k2[j] / 3 + k3[j] / 3 + k4[j] / 6);
                }

                for (m = 1; m <= 4; m++)
                    a[m] = b[m];

            }

        }

        public void Decaimento()
        {
            nb = b[2];
            nt = t;
            t = t + (Tr - Tp);
            x = dd * (Tr - Tp);
            y = (Tr - Tp) * q2;

            b[1] = a[1] + a[2] * (1 - Math.Exp(-y));
            b[2] = a[2] * Math.Exp(-y);
            b[3] = (a[3] * Math.Cos(x) - a[4] * Math.Sin(x)) * Math.Exp(-0.5 * y);
            b[4] = (a[3] * Math.Sin(x) + a[4] * Math.Cos(x)) * Math.Exp(-0.5 * y);

            for (m = 1; m <= 4; m++)
                a[m] = b[m];
        }

        public void AmbienteGrafico()
    {
        System.Drawing.Graphics graphicsObj;

        graphicsObj = this.CreateGraphics();

        Pen myPen = new Pen(System.Drawing.Color.Red, 5);

        // myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;

        if (cor == 1)
            myPen.Color = System.Drawing.Color.Red;
        if (cor == 2)
            myPen.Color = System.Drawing.Color.Blue;
        if (cor == 3)
            myPen.Color = System.Drawing.Color.Green;

        myPen.Width = 2;

        if (espessura == 1)
            myPen.Width = 1;
        if (espessura == 2)
            myPen.Width = 2;
        if (espessura == 3)
            myPen.Width = 3;

        double ex = 0.4e12 / double.Parse(textBoxX2.Text);
        double ey = 2e2 / double.Parse(textBoxY2.Text);

        double z1 = ex * nt + 200;
        double z2 = ex * t + 200;
        double z3 = 400 - ey * nb;
        double z4 = 400 - ey * b[2];

        graphicsObj.DrawLine(myPen, (int)z1, (int)z3, (int)z2, (int)z4);
    }

        public void Dados()
        {
            //Apaga Arquivos .txt e .dat
            using (System.IO.StreamWriter file = new System.IO.StreamWriter("Data.txt", false))
            {

            }
            using (System.IO.StreamWriter file = new System.IO.StreamWriter("Data.dat", false))
            {

            }

            //Apaga listBox
            listBox1.Items.Clear();            
        }

        public void AdicionaDados()
        {
            //Soma das populações
            w = b[1] + b[2];

            //Adiciona populações no listBox
            listBox1.Items.Add(i.ToString("0000") +
            "                        " + b[1].ToString("#0.0000000") + "                      "
            + b[2].ToString("#0.0000000"));
        }

        public void GerarArquivo()
        {
            if (checkBoxTxt.Checked == true)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter("Data.txt", true))
                {
                    file.WriteLine((t * 1e9 - 0.99999).ToString() + " " + b[1].ToString() + " " + b[2].ToString());
                }
            }
            if (checkBoxDat.Checked == true)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter("Data.dat", true))
                {
                    file.WriteLine((t * 1e9 - 0.99999).ToString() + " " + b[1].ToString() + " " + b[2].ToString());
                }
            }
        }

        public void Bloch()
        {
            nb = b[2];
            nt = t;
            if (i % 2 == 0)
            {
                RungeKutta();
            }

            if (i % 2 == 1)
            {
                Decaimento();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Preparação do listBox
            Dados();

            //Condições iniciais
            CondIniciais(); 

            //Início da iteração
            for (i = 1; i <= 2 * Pulsos + 1; i++)
            {
                //Resolve as equações de Bloch
                Bloch();
                
                //Adiciona dados no listBox
                AdicionaDados();

                //Gera arquivo .txt com os dados
                GerarArquivo();
                
                //Inicia ambiente gráfico e plota a curva
                AmbienteGrafico();                
            }   
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Limpa o listBox
            listBox1.Items.Clear();

            //Limpa o ambiente gráfico
            System.Drawing.Graphics graphicsObj;
            graphicsObj = this.CreateGraphics();
            graphicsObj.Clear(Color.Gainsboro);
        }
  
        private void buttonCalcular_Paint(object sender, PaintEventArgs e){}

        private void Form1_MouseDown(object sender, MouseEventArgs e){}
        
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            double m1 = double.Parse(textBoxPulsos.Text);
            double m2 = double.Parse(textBoxTaxa.Text);

            if (checkBoxEscalaH.Checked == true)
            {
                textBoxX2.Text = (1200*m1/m2).ToString();
            }

            if (textBoxY2.Text == "")
                textBoxY2.Text = "1";

            if (double.Parse(textBoxY2.Text) > 1)
                textBoxY2.Text = "1";

            if (double.Parse(textBoxY2.Text) <= 0)
                textBoxY2.Text = "0.5";

            if (textBoxX2.Text == "")
                textBoxX2.Text = (1200 * m1 / m2).ToString();

            if (double.Parse(textBoxX2.Text) <= 0)
                textBoxY2.Text = (1200 * m1 / m2).ToString();
        }

        private void sobreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormSobre Sobre = new FormSobre();
            Sobre.ShowDialog();
        }

        private void sairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void vermelhaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cor = 1;
        }

        private void azulToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cor = 2;
        }

        private void verdeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cor = 3;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            espessura = 1;
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            espessura = 2;
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            espessura = 3;
        }
        
    }
}