Esempio n. 1
0
        void WriteBands(TightBinding tb, KptList kpts, StreamWriter w)
        {
            int bandCount = kpts.Kpts[0].Wavefunctions.Count;


            BandTetrahedron tet = null;

            for (int i = 0; i < tb.KPath.Kpts.Count; i++)
            {
                var kpt = tb.KPath.Kpts[i];

                if (tet == null || tet.Contains(kpt) == false)
                {
                    GetTetrahedron(tb, kpt, kpts);
                }

                w.Write(i);
                w.Write("   ");

                for (int band = 0; band < bandCount; band++)
                {
                    double energy = tet.Interpolate(kpt);

                    w.Write("{0}  ", energy);
                }

                w.WriteLine();
            }
        }
Esempio n. 2
0
        public static int Main(string[] args)
        {
            using (BootStrap b = new BootStrap())
            {
                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                string filename = b.GetInputFile("Tight Binding code", "tb", args);

                TightBinding c = new TightBinding();

                c.LoadTB(filename);
                c.RunTB();

                watch.Stop();
                Output.WriteLine("Total time: {0} s", watch.ElapsedMilliseconds / 1000.0);

                long milliseconds = watch.ElapsedMilliseconds;
                int seconds = (int)(milliseconds / 1000L);
                int minutes = seconds / 60;
                int hours = minutes / 60;
                minutes %= 60;
                seconds %= 60;
                milliseconds %= 1000;

                Output.WriteLine("            {0}:{1:00}:{2:00}.{3:000}", hours, minutes, seconds, milliseconds);
                return 0;
            }
        }
Esempio n. 3
0
        BandTetrahedron GetTetrahedron(TightBinding tb, KPoint kpt, KptList kpts)
        {
            List <Pair <int, double> > lst = new List <Pair <int, double> >();

            double[] weights = new double[kpts.Kpts.Count];
            for (int j = 0; j < kpts.Kpts.Count; j++)
            {
                double distance = CalcDistance(tb, kpts.Kpts[j].Value, kpt.Value);

                weights[j] = 1 / (distance + 0.00001);
            }

            for (int j = 0; j < weights.Length; j++)
            {
                lst.Add(new Pair <int, double>(j, weights[j]));
            }

            lst.Sort((x, y) => { return(y.Second.CompareTo(x.Second)); });

            lst.RemoveRange(4, lst.Count - 4);
            List <int> ilist = lst.Select(x => x.First).ToList();

            BandTetrahedron retval = new BandTetrahedron(tb, kpt.Value, kpts, ilist);

            return(retval);
        }
Esempio n. 4
0
        void OutputBands(TightBinding tb, KptList ks,
                         List <RpaParams> rpa, MatrixGetter g, string name)
        {
            using (StreamWriter w = new StreamWriter("eigenvalues." + name + ".q"))
            {
                w.WriteLine("# Grid");
                w.WriteLine("{0}  {1}  {2}  {3}  {4}  {5}", ks.Mesh[0], ks.Mesh[1], ks.Mesh[2],
                            ks.Shift[0], ks.Shift[1], ks.Shift[2]);

                w.WriteLine("# Eigenvalues");

                foreach (var rpa_i in rpa)
                {
                    var qpt = rpa_i.QptValue;

                    w.Write("{0}    {1}    {2}             ",
                            qpt.X, qpt.Y, qpt.Z);

                    Matrix chi     = g(rpa_i);
                    Matrix evalues = chi.EigenValues();

                    for (int j = 0; j < evalues.Rows; j++)
                    {
                        w.Write("{0}   ", evalues[j, 0].RealPart);
                    }

                    w.WriteLine();
                }
            }
        }
Esempio n. 5
0
        public List<RpaParams> CreateRpaParameterList(TightBinding tb, List<KPoint> QMesh)
        {
            double[] FrequencyMesh = tb.FrequencyMesh;
            double[] TemperatureMesh = tb.TemperatureMesh;

            List<RpaParams> rpa = new List<RpaParams>();

            for (int tempIndex = 0; tempIndex < TemperatureMesh.Length; tempIndex++)
            {
                for (int muIndex = 0; muIndex < tb.MuMesh.Length; muIndex++)
                {
                    for (int qIndex = 0; qIndex < QMesh.Count; qIndex++)
                    {
                        for (int freqIndex = 0; freqIndex < FrequencyMesh.Length; freqIndex++)
                        {
                            rpa.Add(new RpaParams(
                                qIndex,
                                QMesh[qIndex].Value,
                                TemperatureMesh[tempIndex],
                                FrequencyMesh[freqIndex],
                                tb.MuMesh[muIndex]));
                        }
                    }
                }
            }
            return rpa;
        }
Esempio n. 6
0
        public static int Main(string[] args)
        {
            using (BootStrap b = new BootStrap())
            {
                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();


                string filename = b.GetInputFile("Tight Binding code", "tb", args);

                TightBinding c = new TightBinding();

                c.LoadTB(filename);
                c.RunTB();

                watch.Stop();
                Output.WriteLine("Total time: {0} s", watch.ElapsedMilliseconds / 1000.0);

                long milliseconds = watch.ElapsedMilliseconds;
                int  seconds      = (int)(milliseconds / 1000L);
                int  minutes      = seconds / 60;
                int  hours        = minutes / 60;
                minutes      %= 60;
                seconds      %= 60;
                milliseconds %= 1000;

                Output.WriteLine("            {0}:{1:00}:{2:00}.{3:000}", hours, minutes, seconds, milliseconds);
                return(0);
            }
        }
Esempio n. 7
0
        void Run(TightBinding tb, List <string> args)
        {
            foreach (var arg in args)
            {
                Console.WriteLine("Reading file " + arg);

                CreateBands(tb, arg);
            }
        }
Esempio n. 8
0
        void CreateBands(TightBinding tb, string name)
        {
            using (StreamReader r = new StreamReader(name))
            {
                string line = r.ReadLine();

                if (line != "# Grid")
                {
                    Console.WriteLine("Not an eigenvalues file!");
                    System.Environment.Exit(3);
                }

                int[] grid  = new int[3];
                int[] shift = new int[3];


                ParseGrid(grid, shift, line);

                if (line != "# Eigenvalues")
                {
                    Console.WriteLine("Not an eigenvalues file!");
                    System.Environment.Exit(2);
                }

                KptList kpts = new KptList();
                kpts.Mesh  = grid;
                kpts.Shift = shift;

                while (r.EndOfStream == false)
                {
                    line = r.ReadLine();
                    string[] elements = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    KPoint kpt = new KPoint(new Vector3(double.Parse(elements[0]),
                                                        double.Parse(elements[1]),
                                                        double.Parse(elements[2])));

                    for (int i = 3; i < elements.Length; i++)
                    {
                        Wavefunction wfk = new Wavefunction(0);
                        wfk.Energy = double.Parse(elements[i]);

                        kpt.Wavefunctions.Add(wfk);
                    }

                    kpts.Kpts.Add(kpt);
                }

                CreateTetrahedronMesh(kpts);
                string outputfile = name + ".bands";

                using (StreamWriter w = new StreamWriter(outputfile))
                {
                    WriteBands(tb, kpts, w);
                }
            }
        }
Esempio n. 9
0
        void CreateBands(TightBinding tb, string name)
        {
            using (StreamReader r = new StreamReader(name))
            {
                string line = r.ReadLine();

                if (line != "# Grid")
                {
                    Console.WriteLine("Not an eigenvalues file!");
                    System.Environment.Exit(3);
                }

                int[] grid = new int[3];
                int[] shift = new int[3];

                ParseGrid(grid, shift, line);

                if (line != "# Eigenvalues")
                {
                    Console.WriteLine("Not an eigenvalues file!");
                    System.Environment.Exit(2);
                }

                KptList kpts = new KptList();
                kpts.Mesh = grid;
                kpts.Shift = shift;

                while (r.EndOfStream == false)
                {
                    line = r.ReadLine();
                    string[] elements = line.Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries );

                    KPoint kpt = new KPoint(new Vector3(double.Parse(elements[0]),
                                                        double.Parse(elements[1]),
                                                        double.Parse(elements[2])));

                    for (int i = 3; i < elements.Length; i++)
                    {
                        Wavefunction wfk = new Wavefunction(0);
                        wfk.Energy = double.Parse(elements[i]);

                        kpt.Wavefunctions.Add(wfk);
                    }

                    kpts.Kpts.Add(kpt);
                }

                CreateTetrahedronMesh(kpts);
                string outputfile = name + ".bands";

                using (StreamWriter w = new StreamWriter(outputfile))
                {
                    WriteBands(tb, kpts, w);
                }
            }
        }
Esempio n. 10
0
        private void SaveMatricesQPlane(TightBinding tb, List <KPoint> QMesh, List <RpaParams> chi, MatrixGetter g, string name)
        {
            if (tb.TemperatureMesh.Length > 1)
            {
                Directory.CreateDirectory("temperature");
                SaveByTemperature(tb, QMesh, chi, g, name);
            }

            SaveByQPlane(tb, QMesh, chi, g, name);
        }
Esempio n. 11
0
        double CalcDistance(TightBinding tb, Vector3 v1, Vector3 v2)
        {
            Vector3 delta = v1 - v2;

            ShiftDelta(ref delta, tb.Lattice.G1);
            ShiftDelta(ref delta, tb.Lattice.G2);
            ShiftDelta(ref delta, tb.Lattice.G3);

            return(delta.Magnitude);
        }
Esempio n. 12
0
        double CalcDistance(TightBinding tb, Vector3 v1, Vector3 v2)
        {
            Vector3 delta = v1 - v2;

            ShiftDelta(ref delta, tb.Lattice.G1);
            ShiftDelta(ref delta, tb.Lattice.G2);
            ShiftDelta(ref delta, tb.Lattice.G3);

            return delta.Magnitude;
        }
Esempio n. 13
0
        private void VerifySymmetry(TightBinding tb, Matrix S, int a, int b)
        {
            for (int m1 = 0; m1 < 2; m1++)
            {
                for (int m2 = 0; m2 < 2; m2++)
                {
                    for (int m3 = 0; m3 < 2; m3++)
                    {
                        for (int m4 = 0; m4 < 2; m4++)
                        {
                            int l1 = Select(m1, a, b);
                            int l2 = Select(m2, a, b);
                            int l3 = Select(m3, a, b);
                            int l4 = Select(m4, a, b);

                            int a1 = Select(m1, b, a);
                            int a2 = Select(m2, b, a);
                            int a3 = Select(m3, b, a);
                            int a4 = Select(m4, b, a);

                            int i = GetIndex(tb, l1, l2);
                            int j = GetIndex(tb, l3, l4);

                            int ii = GetIndex(tb, a1, a2);
                            int jj = GetIndex(tb, a3, a4);

                            var diff = S[i, j] - S[ii, jj];

                            if (diff.Magnitude > 1e-8)
                            {
                                Output.WriteLine("ERROR: Failed to verify symmetry.");
                                Output.WriteLine("  a = {0}   b = {1}", a, b);
                                Output.WriteLine("  L = {0}{1}{2}{3}", l1, l2, l3, l4);
                                Output.WriteLine("  A = {0}{1}{2}{3}", a1, a2, a3, a4);
                                Output.WriteLine("  ij = {0},{1}      {2},{3}", i, j, ii, jj);

                                Output.WriteLine("  M[i,j] = {0}        {1}", S[i, j], S[ii, jj]);
                                Output.WriteLine("  diff = {0}", diff.Magnitude);

                                try
                                {
                                    throw new Exception("blah");
                                }
                                catch (Exception e)
                                {
                                    Output.WriteLine(e.StackTrace);
                                }

                                Environment.Exit(4);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 14
0
        double InteractionAdjustment(List <RpaParams> rpa, Matrix[] S, Matrix[] C, TightBinding tb)
        {
            double    largest       = double.MinValue;
            RpaParams largestParams = null;
            bool      Cdiv          = false;

            for (int i = 0; i < rpa.Count; i++)
            {
                RpaParams p  = rpa[i];
                Matrix    x0 = p.X0;

                double lv = LargestPositiveEigenvalue(x0 * S[i]);

                if (lv > largest)
                {
                    largest       = lv;
                    largestParams = p;
                    Cdiv          = false;
                }

                lv = LargestPositiveEigenvalue(-x0 * C[i]);

                if (lv > largest)
                {
                    largest       = lv;
                    largestParams = p;
                    Cdiv          = true;
                }
            }

            if (largest >= 1)
            {
                Output.WriteLine("Interaction should be reduced to avoid divergence.", largest);
            }

            Output.WriteLine("Largest eigenvalue of denominator found at:");
            Output.WriteLine("    Eigenvalue: {0}", largest);
            Output.WriteLine("    {0} susceptibility", Cdiv ? "Charge" : "Spin");
            Output.WriteLine("    q = {0}", largestParams.QptValue);
            Output.WriteLine("    Temperature = {0}", largestParams.Temperature);
            Output.WriteLine("    Chemical Potential = {0}", largestParams.ChemicalPotential);
            Output.WriteLine("    Frequency = {0}", largestParams.Frequency);

            largest /= tb.Interactions.MaxEigenvalue;

            Output.WriteLine();
            return(1 / largest);
        }
Esempio n. 15
0
        static void Main(string[] args)
        {
            using (BootStrap b = new BootStrap())
            {
                string inputfile = b.GetInputFile("RPA Analysis code", "rpanal", args);

                TightBinding tb = new TightBinding(inputfile);
                RPA inst = new RPA();

                Vector3 q = GetQpoint();

                List<KPoint> qpt = new List<KPoint>();
                qpt.Add(new KPoint(q));

                var parameters = inst.CreateRpaParameterList(tb, qpt);
            }
        }
Esempio n. 16
0
        static void Main(string[] args)
        {
            using (BootStrap b = new BootStrap())
            {
                string inputfile = b.GetInputFile("RPA Analysis code", "rpanal", args);

                TightBinding tb   = new TightBinding(inputfile);
                RPA          inst = new RPA();

                Vector3 q = GetQpoint();

                List <KPoint> qpt = new List <KPoint>();
                qpt.Add(new KPoint(q));

                var parameters = inst.CreateRpaParameterList(tb, qpt);
            }
        }
Esempio n. 17
0
        public BandTetrahedron(TightBinding tb, Vector3 anchor, KptList kpts, List<int> indices)
        {
            for (int i = 0; i < indices.Count; i++)
            {
                KPoint kpt = kpts.Kpts[indices[i]].Clone();

                Vector3 delta = kpt.Value - anchor;

                ShiftDelta(ref delta, tb.Lattice.G1);
                ShiftDelta(ref delta, tb.Lattice.G2);
                ShiftDelta(ref delta, tb.Lattice.G3);

                kpt.Value = delta + anchor;

                this.kpts.Add(kpt);
            }

            CalculateVelocityMatrix();
        }
Esempio n. 18
0
        public BandTetrahedron(TightBinding tb, Vector3 anchor, KptList kpts, List <int> indices)
        {
            for (int i = 0; i < indices.Count; i++)
            {
                KPoint kpt = kpts.Kpts[indices[i]].Clone();

                Vector3 delta = kpt.Value - anchor;

                ShiftDelta(ref delta, tb.Lattice.G1);
                ShiftDelta(ref delta, tb.Lattice.G2);
                ShiftDelta(ref delta, tb.Lattice.G3);

                kpt.Value = delta + anchor;

                this.kpts.Add(kpt);
            }

            CalculateVelocityMatrix();
        }
Esempio n. 19
0
        private RpaThreadInfo[] CreateThreadInfos(TightBinding tb, List <RpaParams> rpa, KptList qpts)
        {
            RpaThreadInfo[] infos = new RpaThreadInfo[threads];

            for (int i = 0; i < infos.Length; i++)
            {
                infos[i]      = new RpaThreadInfo();
                infos[i].tb   = tb.Clone();
                infos[i].qpts = qpts;
            }

            infos[0].PrimaryThread = true;

            for (int i = 0; i < rpa.Count; i++)
            {
                infos[i % threads].RpaParams.Add(rpa[i]);
            }

            return(infos);
        }
Esempio n. 20
0
        public void RunRpa(TightBinding tb, KptList qpts, bool plane)
        {
            List <KPoint>    QMesh = qpts.Kpts;
            List <RpaParams> rpa   = CreateRpaParameterList(tb, QMesh);

            Output.WriteLine("Calculating susceptibility for {0} q-points.", QMesh.Count);

            CalcSusceptibility(tb, qpts, rpa);

            if (plane)
            {
                SaveMatricesQPlane(tb, QMesh, rpa, x => x.X0, "chi_0");
                SaveMatricesQPlane(tb, QMesh, rpa, x => x.Xs, "chi_s");
                SaveMatricesQPlane(tb, QMesh, rpa, x => x.Xc, "chi_c");
            }
            else
            {
                OutputBands(tb, qpts, rpa, CalcX0 => CalcX0.X0, "chi_0");
            }
        }
Esempio n. 21
0
        public void RunRpa(TightBinding tb, KptList qpts, bool plane)
        {
            List<KPoint> QMesh = qpts.Kpts;
            List<RpaParams> rpa = CreateRpaParameterList(tb, QMesh);

            Output.WriteLine("Calculating susceptibility for {0} q-points.", QMesh.Count);

            CalcSusceptibility(tb, qpts, rpa);

            if (plane)
            {
                SaveMatricesQPlane(tb, QMesh, rpa, x => x.X0, "chi_0");
                SaveMatricesQPlane(tb, QMesh, rpa, x => x.Xs, "chi_s");
                SaveMatricesQPlane(tb, QMesh, rpa, x => x.Xc, "chi_c");
            }
            else
            {
                OutputBands(tb, qpts, rpa, CalcX0 => CalcX0.X0, "chi_0");
            }
        }
Esempio n. 22
0
        public static void Main(string[] args)
        {
            Directory.SetCurrentDirectory("/home/eylvisaker/Calculations/rpa/tests");

            using (BootStrap b = new BootStrap())
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Must specify tight binding input and eigenvalues files on command line.");
                    System.Environment.Exit(1);
                }

                string inputfile = b.GetInputFile("Band Path code", "bandpath", args);
                TightBinding tb = new TightBinding(inputfile);

                var argsList = args.ToList();
                argsList.RemoveAt(0);

                new BandPath().Run(tb, argsList);
            }
        }
Esempio n. 23
0
        public static void Main(string[] args)
        {
            Directory.SetCurrentDirectory("/home/eylvisaker/Calculations/rpa/tests");

            using (BootStrap b = new BootStrap())
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Must specify tight binding input and eigenvalues files on command line.");
                    System.Environment.Exit(1);
                }

                string       inputfile = b.GetInputFile("Band Path code", "bandpath", args);
                TightBinding tb        = new TightBinding(inputfile);

                var argsList = args.ToList();
                argsList.RemoveAt(0);

                new BandPath().Run(tb, argsList);
            }
        }
Esempio n. 24
0
        private void CalcOnSiteInteraction(TightBinding tb, Matrix _S, Matrix _C, InteractionPair interaction)
        {
            foreach (int l1 in interaction.OrbitalsLeft)
            {
                foreach (int l2 in interaction.OrbitalsLeft)
                {
                    foreach (int l3 in interaction.OrbitalsRight)
                    {
                        foreach (int l4 in interaction.OrbitalsRight)
                        {
                            int i = GetIndex(tb, l1, l2);
                            int j = GetIndex(tb, l3, l4);

                            if (l1 == l2 && l2 == l3 && l3 == l4)
                            {
                                _S[i, j] += interaction.HubbardU;
                                _C[i, j] += interaction.HubbardU;
                            }
                            else if (l1 == l4 && l4 != l2 && l2 == l3)
                            {
                                _S[i, j] += interaction.InterorbitalU;
                                _C[i, j] += (-interaction.InterorbitalU + interaction.Exchange);
                            }
                            else if (l1 == l2 && l2 != l3 && l3 == l4)
                            {
                                _S[i, j] += interaction.Exchange;
                                _C[i, j] += 2 * interaction.InterorbitalU - interaction.Exchange;
                            }
                            else if (l1 == l3 && l3 != l2 && l2 == l4)
                            {
                                _S[i, j] += interaction.PairHopping;
                                _C[i, j] += interaction.PairHopping;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 25
0
        public TightBinding Clone()
        {
            TightBinding retval = new TightBinding();

            retval.lattice  = lattice.Clone();
            retval.sites    = sites.Clone();
            retval.hoppings = hoppings.Clone();
            retval.kpath    = kpath.Clone();
            retval.kmesh    = kmesh.Clone();
            if (qmesh != null)
            {
                retval.qmesh = qmesh.Clone();
            }
            if (qplane != null)
            {
                retval.qplane = qplane.Clone();
            }
            retval.poles.AddRange(poles);
            retval.FrequencyMesh   = (double[])FrequencyMesh.Clone();
            retval.TemperatureMesh = (double[])TemperatureMesh.Clone();
            retval.MuMesh          = (double[])MuMesh.Clone();
            retval.Nelec           = (double[])Nelec.Clone();

            retval.Interactions = Interactions.Clone();
            retval.kgrid        = (int[])kgrid.Clone();
            retval.kshift       = (int[])kshift.Clone();

            retval.symmetries     = symmetries.Clone();
            retval.qgrid          = (int[])qgrid.Clone();
            retval.qshift         = (int[])qshift.Clone();
            retval.qplaneDef      = (Vector3[])qplaneDef.Clone();
            retval.setQplane      = setQplane;
            retval.specifiedNelec = specifiedNelec;

            retval.outputfile = outputfile;

            return(retval);
        }
Esempio n. 26
0
        private void SaveByTemperature(TightBinding tb, List <KPoint> QMesh, List <RpaParams> rpa, MatrixGetter g, string name)
        {
            rpa.Sort(RpaParams.TemperatureComparison);

            Complex[] chisum    = new Complex[rpa.Count];
            double[]  chimag    = new double[rpa.Count];
            double[]  chimagsqr = new double[rpa.Count];

            for (int l1 = 0; l1 < tb.Orbitals.Count; l1++)
            {
                for (int l2 = 0; l2 < tb.Orbitals.Count; l2++)
                {
                    for (int l3 = 0; l3 < tb.Orbitals.Count; l3++)
                    {
                        for (int l4 = 0; l4 < tb.Orbitals.Count; l4++)
                        {
                            int i = GetIndex(tb, l1, l2);
                            int j = GetIndex(tb, l3, l4);

                            // organize by temperature
                            string filename = string.Format(
                                "temperature/{0}.{1}{2}{3}{4}.T", name, l1, l2, l3, l4);

                            double lastFreq = double.MinValue;
                            double lastMu   = double.MinValue;
                            double lastq    = int.MinValue;

                            using (StreamWriter w = new StreamWriter(filename))
                            {
                                for (int index = 0; index < rpa.Count; index++)
                                {
                                    bool newline = false;

                                    newline |= ChangeValue(ref lastFreq, rpa[index].Frequency);
                                    newline |= ChangeValue(ref lastMu, rpa[index].ChemicalPotential);
                                    newline |= ChangeValue(ref lastq, rpa[index].Qindex);

                                    if (newline)
                                    {
                                        w.WriteLine();
                                        w.WriteLine("# Frequency: {0}", rpa[index].Frequency);
                                        w.WriteLine("# Chemical Potential: {0}", rpa[index].ChemicalPotential);
                                        w.WriteLine("# Q: {0}", QMesh[rpa[index].Qindex]);
                                        w.WriteLine("#");
                                        w.WriteLine("# Temperature\tRe(Chi)\tIm(Chi)");
                                    }

                                    Complex val = g(rpa[index])[i, j];

                                    chisum[index]    += val;
                                    chimag[index]    += val.Magnitude;
                                    chimagsqr[index] += val.MagnitudeSquared;

                                    w.WriteLine("\t{0:0.000000}\t{1:0.0000000}\t{2:0.0000000}",
                                                rpa[index].Temperature, val.RealPart, val.ImagPart);
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 27
0
        public TightBinding Clone()
        {
            TightBinding retval = new TightBinding();

            retval.lattice = lattice.Clone();
            retval.sites = sites.Clone();
            retval.hoppings = hoppings.Clone();
            retval.kpath = kpath.Clone();
            retval.kmesh = kmesh.Clone();
            if (qmesh != null) retval.qmesh = qmesh.Clone();
            if (qplane != null)	retval.qplane = qplane.Clone();
            retval.poles.AddRange(poles);
            retval.FrequencyMesh = (double[])FrequencyMesh.Clone();
            retval.TemperatureMesh = (double[])TemperatureMesh.Clone();
            retval.MuMesh = (double[])MuMesh.Clone();
            retval.Nelec = (double[])Nelec.Clone();

            retval.Interactions = Interactions.Clone();
            retval.kgrid = (int[])kgrid.Clone();
            retval.kshift = (int[])kshift.Clone();

            retval.symmetries = symmetries.Clone();
            retval.qgrid = (int[])qgrid.Clone();
            retval.qshift = (int[])qshift.Clone();
            retval.qplaneDef = (Vector3[])qplaneDef.Clone();
            retval.setQplane = setQplane;
            retval.specifiedNelec = specifiedNelec;

            retval.outputfile = outputfile;

            return retval;
        }
Esempio n. 28
0
        BandTetrahedron GetTetrahedron(TightBinding tb, KPoint kpt, KptList kpts)
        {
            List<Pair<int, double>> lst = new List<Pair<int, double>>();

            double[] weights = new double[kpts.Kpts.Count];
            for (int j = 0; j < kpts.Kpts.Count; j++)
            {
                double distance = CalcDistance(tb, kpts.Kpts[j].Value, kpt.Value);

                weights[j] = 1 / (distance + 0.00001);
            }

            for (int j = 0; j < weights.Length; j++)
            {
                lst.Add(new Pair<int, double>(j, weights[j]));
            }

            lst.Sort((x,y) => { return y.Second.CompareTo(x.Second); });

            lst.RemoveRange(4, lst.Count - 4);
            List<int> ilist = lst.Select(x => x.First).ToList();

            BandTetrahedron retval = new BandTetrahedron(tb, kpt.Value, kpts, ilist);

            return retval;
        }
Esempio n. 29
0
        void Run(TightBinding tb, List<string> args)
        {
            foreach(var arg in args)
            {
                Console.WriteLine("Reading file " + arg);

                CreateBands(tb, arg);
            }
        }
Esempio n. 30
0
        private void SaveMatricesQPlane(TightBinding tb, List<KPoint> QMesh, List<RpaParams> chi, MatrixGetter g, string name)
        {
            if (tb.TemperatureMesh.Length > 1)
            {
                Directory.CreateDirectory("temperature");
                SaveByTemperature(tb, QMesh, chi, g, name);
            }

            SaveByQPlane(tb, QMesh, chi, g, name);
        }
Esempio n. 31
0
        private void SaveByQPlane(TightBinding tb, List<KPoint> QMesh, List<RpaParams> rpa, MatrixGetter g, string name)
        {
            rpa.Sort(RpaParams.QIndexComparison);

            Complex[] chisum = new Complex[rpa.Count];
            double[] chimag = new double[rpa.Count];
            double[] chimagsqr = new double[rpa.Count];

            for (int l1 = 0; l1 < tb.Orbitals.Count; l1++)
            {
                for (int l2 = 0; l2 < tb.Orbitals.Count; l2++)
                {
                    for (int l3 = 0; l3 < tb.Orbitals.Count; l3++)
                    {
                        for (int l4 = 0; l4 < tb.Orbitals.Count; l4++)
                        {
                            double lastFreq = double.MinValue;
                            double lastMu = double.MinValue;
                            double lastq = int.MinValue;

                            int baseIndex = 0;

                            for (int ti = 0; ti < tb.TemperatureMesh.Length; ti++)
                            {
                                for (int ui = 0; ui < tb.MuMesh.Length; ui++)
                                {
                                    for (int wi = 0; wi < tb.FrequencyMesh.Length; wi++)
                                    {
                                        string filename_re = string.Format("{0}.re.{1}{2}{3}{4}.w{5}.T{6}.u{7}.qm",
                                                               name, l1, l2, l3, l4, wi, ti, ui);
                                        string filename_im = string.Format("{0}.im.{1}{2}{3}{4}.w{5}.T{6}.u{7}.qm",
                                                               name, l1, l2, l3, l4, wi, ti, ui);
                                        string filename_mag = string.Format("{0}.mag.{1}{2}{3}{4}.w{5}.T{6}.u{7}.qm",
                                                               name, l1, l2, l3, l4, wi, ti, ui);

                                        Complex maxvalue = new Complex(double.MinValue, double.MinValue);
                                        Complex minvalue = new Complex(double.MaxValue, double.MaxValue);

                                        using (StreamWriter w_re = new StreamWriter(filename_re))
                                        using (StreamWriter w_im = new StreamWriter(filename_im))
                                        using (StreamWriter w_mag = new StreamWriter(filename_mag))
                                        {
                                            double last_t;
                                            double last_s;

                                            tb.QPlane.GetPlaneST(tb.QPlane.AllKpts[0], out last_s, out last_t);

                                            for (int qi = 0; qi < tb.QPlane.AllKpts.Count; qi++)
                                            {
                                                Vector3 qpt = tb.QPlane.AllKpts[qi];
                                                List<int> orbitalMap;

                                                double s, t;
                                                tb.QPlane.GetPlaneST(tb.QPlane.AllKpts[qi], out s, out t);

                                                if (Math.Abs(t - last_t) > 1e-6)
                                                {
                                                    w_re.WriteLine();
                                                    w_im.WriteLine();
                                                    w_mag.WriteLine();
                                                }

                                                int kindex =
                                                    tb.QPlane.IrreducibleIndex(qpt, tb.Lattice, tb.Symmetries, out orbitalMap);

                                                int index = GetRpaIndex(rpa, kindex,
                                                    tb.TemperatureMesh[ti],
                                                    tb.FrequencyMesh[wi],
                                                    tb.MuMesh[ui]);

                                                int newL1 = tb.Symmetries.TransformOrbital(orbitalMap, l1);
                                                int newL2 = tb.Symmetries.TransformOrbital(orbitalMap, l2);
                                                int newL3 = tb.Symmetries.TransformOrbital(orbitalMap, l3);
                                                int newL4 = tb.Symmetries.TransformOrbital(orbitalMap, l4);

                                                int newii = GetIndex(tb, newL1, newL2);
                                                int newjj = GetIndex(tb, newL3, newL4);

                                                Complex val = g(rpa[index])[newii, newjj];

                                                w_re.WriteLine(" {0}       {1}       {2:0.0000000}", s, t, val.RealPart);
                                                w_im.WriteLine(" {0}       {1}       {2:0.0000000}", s, t, val.ImagPart);
                                                w_mag.WriteLine(" {0}       {1}       {2:0.0000000}", s, t, val.Magnitude);

                                                if (val.RealPart > maxvalue.RealPart) maxvalue.RealPart = val.RealPart;
                                                if (val.ImagPart > maxvalue.ImagPart) maxvalue.ImagPart = val.ImagPart;
                                                if (val.RealPart < minvalue.RealPart) minvalue.RealPart = val.RealPart;
                                                if (val.ImagPart < minvalue.ImagPart) minvalue.ImagPart = val.ImagPart;

                                                last_t = t;
                                                last_s = s;
                                            }
                                        }

                                        for (int i = 0; i < 3; i++)
                                        {
                                            string filename;
                                            switch (i)
                                            {
                                                case 0: filename = filename_re; break;
                                                case 1: filename = filename_im; break;
                                                case 2: filename = filename_mag; break;
                                                default:
                                                    continue;
                                            }

                                            string gpfilename = "gnuplot." + filename;

                                            //minvalue.RealPart = Math.Floor(minvalue.RealPart);
                                            //maxvalue.RealPart = Math.Ceiling(maxvalue.RealPart);

                                            using (StreamWriter w = new StreamWriter(gpfilename))
                                            {
                                                w.WriteLine("#!/usr/bin/gnuplot");
                                                //w.WriteLine("set pm3d at bs flush center ftriangles scansbackward interpolate 1,1");
                                                w.WriteLine("set pm3d map flush center ftriangles scansbackward interpolate 5,5");
                                                w.WriteLine("set palette rgbformula 23,9,-36");
                                                //w.WriteLine("set border 895");
                                                w.WriteLine("set key off");
                                                //w.WriteLine("set zrange [{0}:{1}]", minvalue.RealPart, maxvalue.RealPart);
                                                // label z = minvalue - 0.5 * (maxvalue - minvalue)
                                                //  set label 1 "G" at 0,0,1 font "Symbol" center front
                                                w.WriteLine("splot '{0}' with pm3d", filename);
                                            }
                                        }
                                    }

                                    baseIndex += QMesh.Count;
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 32
0
        void OutputBands(TightBinding tb, KptList ks, 
            List<RpaParams> rpa, MatrixGetter g, string name)
        {
            using (StreamWriter w = new StreamWriter("eigenvalues." + name + ".q"))
            {
                w.WriteLine("# Grid");
                w.WriteLine("{0}  {1}  {2}  {3}  {4}  {5}", ks.Mesh[0], ks.Mesh[1], ks.Mesh[2],
                            ks.Shift[0], ks.Shift[1], ks.Shift[2]);

                w.WriteLine("# Eigenvalues");

                foreach(var rpa_i in rpa)
                {
                    var qpt = rpa_i.QptValue;

                    w.Write("{0}    {1}    {2}             ",
                            qpt.X, qpt.Y, qpt.Z);

                    Matrix chi = g(rpa_i);
                    Matrix evalues = chi.EigenValues();

                    for (int j = 0; j < evalues.Rows; j++)
                    {
                        w.Write("{0}   ", evalues[j, 0].RealPart);
                    }

                    w.WriteLine();
                }
            }
        }
Esempio n. 33
0
        private void CalcOnSiteInteraction(TightBinding tb, Matrix _S, Matrix _C, InteractionPair interaction)
        {
            foreach (int l1 in interaction.OrbitalsLeft)
            {
                foreach (int l2 in interaction.OrbitalsLeft)
                {
                    foreach (int l3 in interaction.OrbitalsRight)
                    {
                        foreach (int l4 in interaction.OrbitalsRight)
                        {
                            int i = GetIndex(tb, l1, l2);
                            int j = GetIndex(tb, l3, l4);

                            if (l1 == l2 && l2 == l3 && l3 == l4)
                            {
                                _S[i, j] += interaction.HubbardU;
                                _C[i, j] += interaction.HubbardU;
                            }
                            else if (l1 == l4 && l4 != l2 && l2 == l3)
                            {
                                _S[i, j] += interaction.InterorbitalU;
                                _C[i, j] += (-interaction.InterorbitalU + interaction.Exchange);
                            }
                            else if (l1 == l2 && l2 != l3 && l3 == l4)
                            {
                                _S[i, j] += interaction.Exchange;
                                _C[i, j] += 2 * interaction.InterorbitalU - interaction.Exchange;
                            }
                            else if (l1 == l3 && l3 != l2 && l2 == l4)
                            {
                                _S[i, j] += interaction.PairHopping;
                                _C[i, j] += interaction.PairHopping;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 34
0
        private void CalcOffSiteInteraction(TightBinding tb, Matrix _S, Matrix _C, InteractionPair interaction, double structureFactor)
        {
            foreach (int l1 in interaction.OrbitalsLeft)
            {
                foreach (int l2 in interaction.OrbitalsLeft)
                {
                    foreach (int l3 in interaction.OrbitalsRight)
                    {
                        foreach (int l4 in interaction.OrbitalsRight)
                        {
                            int i = GetIndex(tb, l1, l2);
                            int j = GetIndex(tb, l3, l4);
                            double Sval = 0, Cval = 0;

                            if (l1 == l2 && l2 == l3 && l3 == l4)
                            {
                                Sval = -0.5 * interaction.Exchange * structureFactor;
                                Cval = (2 * interaction.InterorbitalU - 0.5 * interaction.Exchange) * structureFactor;
                            }
                            else if (l1 == l2 && l2 != l3 && l3 == l4)
                            {
                                Sval = -0.25 * interaction.Exchange * structureFactor;
                                Cval = (2 * interaction.InterorbitalU - 0.5 * interaction.Exchange) * structureFactor;
                            }

                            _S[i, j] += Sval;
                            _S[j, i] += Sval;

                            _C[i, j] += Cval;
                            _C[j, i] += Cval;
                        }
                    }
                }
            }
        }
Esempio n. 35
0
        private void VerifySymmetry(TightBinding tb, Matrix S, int a, int b)
        {
            for (int m1 = 0; m1 < 2; m1++)
            {
                for (int m2 = 0; m2 < 2; m2++)
                {
                    for (int m3 = 0; m3 < 2; m3++)
                    {
                        for (int m4 = 0; m4 < 2; m4++)
                        {
                            int l1 = Select(m1, a, b);
                            int l2 = Select(m2, a, b);
                            int l3 = Select(m3, a, b);
                            int l4 = Select(m4, a, b);

                            int a1 = Select(m1, b, a);
                            int a2 = Select(m2, b, a);
                            int a3 = Select(m3, b, a);
                            int a4 = Select(m4, b, a);

                            int i = GetIndex(tb, l1, l2);
                            int j = GetIndex(tb, l3, l4);

                            int ii = GetIndex(tb, a1, a2);
                            int jj = GetIndex(tb, a3, a4);

                            var diff = S[i, j] - S[ii, jj];

                            if (diff.Magnitude > 1e-8)
                            {
                                Output.WriteLine("ERROR: Failed to verify symmetry.");
                                Output.WriteLine("  a = {0}   b = {1}", a, b);
                                Output.WriteLine("  L = {0}{1}{2}{3}", l1, l2, l3, l4);
                                Output.WriteLine("  A = {0}{1}{2}{3}", a1, a2, a3, a4);
                                Output.WriteLine("  ij = {0},{1}      {2},{3}", i, j, ii, jj);

                                Output.WriteLine("  M[i,j] = {0}        {1}", S[i,j], S[ii,jj]);
                                Output.WriteLine("  diff = {0}", diff.Magnitude);

                                try
                                {
                                    throw new Exception("blah");
                                }
                                catch(Exception e)
                                {
                                    Output.WriteLine(e.StackTrace);
                                }

                                Environment.Exit(4);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 36
0
        void SetTemperature(TightBinding tb, double temperature, double mu)
        {
            //currentTemperature = value;
            Beta = 1 / temperature;

            tb.KMesh.SetTemperature(temperature, mu);
        }
Esempio n. 37
0
        Matrix CalcX0(TightBinding tb, double freq, Vector3 q)
        {
            int    orbitalCount = tb.Orbitals.Count;
            int    size         = orbitalCount * orbitalCount;
            Matrix x            = new Matrix(size, size);

            Complex denom_factor = new Complex(0, 1e-4);

            //StreamWriter w = new StreamWriter(string.Format("qcont.{0}", q.ToString("0.000")));
            //bool writeThis = false;

            for (int l1 = 0; l1 < orbitalCount; l1++)
            {
                for (int l4 = 0; l4 < orbitalCount; l4++)
                {
                    for (int l3 = l1; l3 < orbitalCount; l3++)
                    {
                        for (int l2 = l4; l2 < orbitalCount; l2++)
                        {
                            int  i             = GetIndex(tb, l1, l2);
                            int  j             = GetIndex(tb, l3, l4);
                            bool foundSymmetry = false;

                            //if (l1 == 0 && l2 == 0 && l3 == 0 && l4 == 0)
                            //    writeThis = true;
                            //else
                            //writeThis = false;

                            //if (writeThis)
                            //    w.WriteLine("{0}{1}{2}{3}", l1, l2, l3, l4);

                            for (int s = 0; s < tb.Symmetries.Count; s++)
                            {
                                Symmetry sym = tb.Symmetries[s];

                                if (sym.OrbitalTransform == null || sym.OrbitalTransform.Count == 0)
                                {
                                    continue;
                                }

                                int newL1 = sym.OrbitalTransform[l1];
                                int newL2 = sym.OrbitalTransform[l2];
                                int newL3 = sym.OrbitalTransform[l3];
                                int newL4 = sym.OrbitalTransform[l4];

                                int newI = GetIndex(tb, newL1, newL2);
                                int newJ = GetIndex(tb, newL3, newL4);

                                if (newI == i && newJ == j)
                                {
                                    continue;
                                }

                                foundSymmetry = true;

                                if (newL1 > l1)
                                {
                                    foundSymmetry = false;
                                }
                                if (newL2 > l2)
                                {
                                    foundSymmetry = false;
                                }
                                if (newL3 > l3)
                                {
                                    foundSymmetry = false;
                                }
                                if (newL4 > l4)
                                {
                                    foundSymmetry = false;
                                }

                                if (foundSymmetry)
                                {
                                    x[i, j] = x[newI, newJ];
                                    x[j, i] = x[i, j].Conjugate();

                                    break;
                                }
                            }

                            if (foundSymmetry)
                            {
                                continue;
                            }

                            Complex total = 0;

                            for (int allkindex = 0; allkindex < tb.KMesh.AllKpts.Count; allkindex++)
                            {
                                Complex val = 0;
                                Vector3 k   = tb.KMesh.AllKpts[allkindex];
                                Vector3 kq  = k + q;

                                //List<int> kOrbitalMap;
                                //List<int> kqOrbitalMap;

                                //int kindex = tb.KMesh.IrreducibleIndex(k, tb.Lattice, tb.Symmetries, out kOrbitalMap);
                                //int kqindex = tb.KMesh.IrreducibleIndex(kq, tb.Lattice, tb.Symmetries, out kqOrbitalMap);
                                int kindex  = tb.KMesh.AllKindex(k, tb.Lattice);
                                int kqindex = tb.KMesh.AllKindex(kq, tb.Lattice);

                                System.Diagnostics.Debug.Assert(kindex == allkindex);

                                //int newL1 = TransformOrbital(kqOrbitalMap, l1);
                                //int newL2 = TransformOrbital(kOrbitalMap, l2);
                                //int newL3 = TransformOrbital(kqOrbitalMap, l3);
                                //int newL4 = TransformOrbital(kOrbitalMap, l4);

                                for (int n1 = 0; n1 < orbitalCount; n1++)
                                {
                                    Wavefunction wfk = Bands(tb, kindex, n1);
                                    double       e1  = wfk.Energy;
                                    double       f1  = wfk.FermiFunction;

                                    for (int n2 = 0; n2 < orbitalCount; n2++)
                                    {
                                        Wavefunction wfq = Bands(tb, kqindex, n2);
                                        double       e2  = wfq.Energy;
                                        double       f2  = wfq.FermiFunction;

                                        Complex coeff =
                                            wfq.Coeffs[l1] * wfq.Coeffs[l4].Conjugate() *
                                            wfk.Coeffs[l3] * wfk.Coeffs[l2].Conjugate();

                                        if (coeff == 0)
                                        {
                                            continue;
                                        }
                                        if (f1 < 1e-15 && f2 < 1e-15)
                                        {
                                            continue;
                                        }

                                        Complex denom_p = (e2 - e1 + freq + denom_factor);
                                        //Complex denom_n = (e2 - e1 - freq - denom_factor);
                                        //Complex lindhard = (f1 - f2) * (1.0 / denom_p + 1.0 / denom_n);
                                        Complex lindhard = (f1 - f2) * (1.0 / denom_p);
                                        Complex contrib  = coeff * lindhard;

                                        if (Math.Abs(f1 - f2) < 1e-11 && freq == 0.0)
                                        {
                                            contrib = coeff * f1 * (1 - f1) * Beta;
                                        }

                                        //w.Write("{0}  {1}   {2}   {3}           ", kindex, kqindex, n1, n2);
                                        //w.WriteLine("{0}   {1}   {2}   {3}   {4}", coeff, e1, e2, f1, f2);

                                        if (double.IsNaN(contrib.RealPart) || double.IsNaN(contrib.ImagPart))
                                        {
                                            throw new Exception("Found NaN when evaluating X0");
                                        }

                                        val += contrib;
                                    }
                                }

                                //w.WriteLine("{0}  {1}   total           {2} + {3}i", kindex, kqindex,
                                //    Math.Round(val.RealPart, 4), Math.Round(val.ImagPart, 4));

                                //Output.WriteLine(tb.KMesh.AllKpts[kindex].Weight.ToString());
                                val   *= tb.KMesh.AllKpts[kindex].Weight;
                                total += val;

                                //if (writeThis)
                                //    w.WriteLine("{0}        {1}              {2}", allkindex, total, val);
                            }

                            x[i, j] = total;
                            x[j, i] = total.Conjugate();

                            //if (writeThis)
                            //{
                            //    w.WriteLine("total for {0}{1}{2}{3}: {4}", l1, l2, l3, l4, total);
                            //    w.WriteLine("---------------------");
                            //}
                        }
                    }
                }
            }

            //w.Close();

            return(x);
        }
 public TbInputFileReader(string filename, TightBinding tb)
     : base(filename)
 {
     this.tb = tb;
 }
Esempio n. 39
0
        private void CalcSusceptibility(TightBinding tb, KptList qpts, List <RpaParams> rpa)
        {
            Matrix ident = Matrix.Identity(tb.Orbitals.Count * tb.Orbitals.Count);

            Matrix[] S, C;
            CalcSpinChargeMatrices(tb, rpa, out S, out C);

            Output.WriteLine("Calculating X0...");


            RpaThreadInfo[] threadInfos = CreateThreadInfos(tb, rpa, qpts);

            Output.WriteLine("Using {0} threads.", threads);

            for (int i = 0; i < threadInfos.Length; i++)
            {
                RunRpaThread(threadInfos[i]);

                if (i == 0)
                {
                    Thread.Sleep(20);
                }
            }

            bool threadsRunning;

            do
            {
                threadsRunning = false;

                for (int i = 0; i < threadInfos.Length; i++)
                {
                    if (threadInfos[i].Thread.ThreadState == ThreadState.Running)
                    {
                        threadsRunning = true;
                    }
                }

                Thread.Sleep(10);
            } while (threadsRunning);

            Output.WriteLine();
            Output.WriteLine("Bare susceptibility calculation completed.");
            Output.WriteLine();

            double factor = InteractionAdjustment(rpa, S, C, tb);

            if (tb.Interactions.AdjustInteractions)
            {
                Output.WriteLine("Multiplying interactions by {0}.", factor);

                for (int i = 0; i < rpa.Count; i++)
                {
                    S[i] *= factor;
                    C[i] *= factor;
                }
            }
            else if (factor < 1)
            {
                Output.WriteLine("WARNING:  There will be divergent geometric series.");
                Output.WriteLine("          Interpret results with care!");
            }

            Output.WriteLine();
            Output.WriteLine("Calculating dressed susceptibilities.");
            Output.WriteLine();

            RpaParams largestParams = null;
            double    largest       = 0;
            string    indices       = "";
            bool      charge        = false;

            for (int i = 0; i < rpa.Count; i++)
            {
                Matrix s_denom = (ident - S[i] * rpa[i].X0);
                Matrix c_denom = (ident + C[i] * rpa[i].X0);

                Matrix s_inv = s_denom.Invert();
                Matrix c_inv = c_denom.Invert();

                System.Diagnostics.Debug.Assert((s_denom * s_inv).IsIdentity);

                rpa[i].Xs = rpa[i].X0 * s_inv;
                rpa[i].Xc = rpa[i].X0 * c_inv;

                for (int l1 = 0; l1 < tb.Orbitals.Count; l1++)
                {
                    for (int l2 = 0; l2 < tb.Orbitals.Count; l2++)
                    {
                        for (int l3 = 0; l3 < tb.Orbitals.Count; l3++)
                        {
                            for (int l4 = 0; l4 < tb.Orbitals.Count; l4++)
                            {
                                int a = GetIndex(tb, l1, l2);
                                int b = GetIndex(tb, l3, l4);

                                bool found = false;

                                if (rpa[i].Xs[a, b].MagnitudeSquared > largest)
                                {
                                    largest = rpa[i].Xs[a, b].MagnitudeSquared;
                                    charge  = false;
                                    found   = true;
                                }
                                if (rpa[i].Xc[a, b].MagnitudeSquared > largest)
                                {
                                    largest = rpa[i].Xc[a, b].MagnitudeSquared;
                                    charge  = true;
                                    found   = true;
                                }
                                if (found == false)
                                {
                                    continue;
                                }

                                indices       = string.Format("{0}{1}{2}{3}", l1, l2, l3, l4);
                                largestParams = rpa[i];
                            }
                        }
                    }
                }
            }

            Output.WriteLine("Largest susceptibility found at:");
            Output.WriteLine("    {0} susceptibility: {1}", charge ? "Charge" : "Spin", Math.Sqrt(largest));
            Output.WriteLine("    Indices: {0}", indices);
            Output.WriteLine("    Temperature: {0}", largestParams.Temperature);
            Output.WriteLine("    Frequency: {0}", largestParams.Frequency);
            Output.WriteLine("    Chemical Potential: {0}", largestParams.ChemicalPotential);
            Output.WriteLine("    Q: {0}", largestParams.QptValue);
        }
Esempio n. 40
0
 int GetIndex(TightBinding tb, int l1, int l2)
 {
     // if this changes, be sure to correct the way
     // x[i,j] and x[j,i] are set in CalcX0.
     return(l1 * tb.Orbitals.Count + l2);
 }
Esempio n. 41
0
        double InteractionAdjustment(List<RpaParams> rpa, Matrix[] S, Matrix[] C, TightBinding tb)
        {
            double largest = double.MinValue;
            RpaParams largestParams = null;
            bool Cdiv = false;

            for (int i = 0; i < rpa.Count; i++)
            {
                RpaParams p = rpa[i];
                Matrix x0 = p.X0;

                double lv = LargestPositiveEigenvalue(x0 * S[i]);

                if (lv > largest)
                {
                    largest = lv;
                    largestParams = p;
                    Cdiv = false;
                }

                lv = LargestPositiveEigenvalue(-x0 * C[i]);

                if (lv > largest)
                {
                    largest = lv;
                    largestParams = p;
                    Cdiv = true;
                }
            }

            if (largest >= 1)
            {
                Output.WriteLine("Interaction should be reduced to avoid divergence.", largest);
            }

            Output.WriteLine("Largest eigenvalue of denominator found at:");
            Output.WriteLine("    Eigenvalue: {0}", largest);
            Output.WriteLine("    {0} susceptibility", Cdiv ? "Charge" : "Spin");
            Output.WriteLine("    q = {0}", largestParams.QptValue);
            Output.WriteLine("    Temperature = {0}", largestParams.Temperature);
            Output.WriteLine("    Chemical Potential = {0}", largestParams.ChemicalPotential);
            Output.WriteLine("    Frequency = {0}", largestParams.Frequency);

            largest /= tb.Interactions.MaxEigenvalue;

            Output.WriteLine();
            return 1 / largest;
        }
Esempio n. 42
0
        private void CalcSpinChargeMatrices(TightBinding tb, List<RpaParams> rpa, out Matrix[] S, out Matrix[] C)
        {
            S = new Matrix[rpa.Count];
            C = new Matrix[rpa.Count];

            for (int rpa_index = 0; rpa_index < rpa.Count; rpa_index++)
            {
                Vector3 q = rpa[rpa_index].QptValue;

                int size = tb.Orbitals.Count * tb.Orbitals.Count;

                Matrix _S = new Matrix(size, size);
                Matrix _C = new Matrix(size, size);

                foreach (var interaction in tb.Interactions)
                {
                    double structureFactor = interaction.StructureFactor(q);

                    if (interaction.OnSite)
                        CalcOnSiteInteraction(tb, _S, _C, interaction);
                    else
                        CalcOffSiteInteraction(tb, _S, _C, interaction, structureFactor);
                }

                System.Diagnostics.Debug.Assert(_S.IsSymmetric);
                System.Diagnostics.Debug.Assert(_C.IsSymmetric);

                S[rpa_index] = _S;
                C[rpa_index] = _C;
            }
        }
Esempio n. 43
0
        void Run(string inputfile)
        {
            TightBinding tb = new TightBinding();
            tb.LoadTB(inputfile);
            tb.RunTB();

            bool ranRPA = false;

            SetCpus();

            if (tb.UseQPlane && tb.QPlane != null && tb.QPlane.Kpts.Count > 0)
            {
                RunRpa(tb, tb.QPlane, true);
                ranRPA = true;
            }
            if (tb.QMesh != null)
            {
                RunRpa(tb, tb.QMesh, false);
                ranRPA = true;
            }

            if (!ranRPA)
                Output.WriteLine("No q-points defined, so we will not run the RPA.");
        }
Esempio n. 44
0
 public Wavefunction Bands(TightBinding tb, int kpt, int band)
 {
     return tb.KMesh.AllKpts[kpt].Wavefunctions[band];
 }
Esempio n. 45
0
        private void SaveByTemperature(TightBinding tb, List<KPoint> QMesh, List<RpaParams> rpa, MatrixGetter g, string name)
        {
            rpa.Sort(RpaParams.TemperatureComparison);

            Complex[] chisum = new Complex[rpa.Count];
            double[] chimag = new double[rpa.Count];
            double[] chimagsqr = new double[rpa.Count];

            for (int l1 = 0; l1 < tb.Orbitals.Count; l1++)
            {
                for (int l2 = 0; l2 < tb.Orbitals.Count; l2++)
                {
                    for (int l3 = 0; l3 < tb.Orbitals.Count; l3++)
                    {
                        for (int l4 = 0; l4 < tb.Orbitals.Count; l4++)
                        {
                            int i = GetIndex(tb, l1, l2);
                            int j = GetIndex(tb, l3, l4);

                            // organize by temperature
                            string filename = string.Format(
                                "temperature/{0}.{1}{2}{3}{4}.T", name, l1, l2, l3, l4);

                            double lastFreq = double.MinValue;
                            double lastMu = double.MinValue;
                            double lastq = int.MinValue;

                            using (StreamWriter w = new StreamWriter(filename))
                            {
                                for (int index = 0; index < rpa.Count; index++)
                                {
                                    bool newline = false;

                                    newline |= ChangeValue(ref lastFreq, rpa[index].Frequency);
                                    newline |= ChangeValue(ref lastMu, rpa[index].ChemicalPotential);
                                    newline |= ChangeValue(ref lastq, rpa[index].Qindex);

                                    if (newline)
                                    {
                                        w.WriteLine();
                                        w.WriteLine("# Frequency: {0}", rpa[index].Frequency);
                                        w.WriteLine("# Chemical Potential: {0}", rpa[index].ChemicalPotential);
                                        w.WriteLine("# Q: {0}", QMesh[rpa[index].Qindex]);
                                        w.WriteLine("#");
                                        w.WriteLine("# Temperature\tRe(Chi)\tIm(Chi)");
                                    }

                                    Complex val = g(rpa[index])[i, j];

                                    chisum[index] += val;
                                    chimag[index] += val.Magnitude;
                                    chimagsqr[index] += val.MagnitudeSquared;

                                    w.WriteLine("\t{0:0.000000}\t{1:0.0000000}\t{2:0.0000000}",
                                        rpa[index].Temperature, val.RealPart, val.ImagPart);
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 46
0
        private void CalcSusceptibility(TightBinding tb, KptList qpts, List<RpaParams> rpa)
        {
            Matrix ident = Matrix.Identity(tb.Orbitals.Count * tb.Orbitals.Count);

            Matrix[] S, C;
            CalcSpinChargeMatrices(tb, rpa, out S, out C);

            Output.WriteLine("Calculating X0...");

            RpaThreadInfo[] threadInfos = CreateThreadInfos(tb, rpa, qpts);

            Output.WriteLine("Using {0} threads.", threads);

            for (int i = 0; i < threadInfos.Length; i++)
            {
                RunRpaThread(threadInfos[i]);

                if (i == 0)
                    Thread.Sleep(20);
            }

            bool threadsRunning;

            do
            {
                threadsRunning = false;

                for (int i = 0; i < threadInfos.Length; i++)
                {
                    if (threadInfos[i].Thread.ThreadState == ThreadState.Running)
                        threadsRunning = true;
                }

                Thread.Sleep(10);

            } while (threadsRunning);

            Output.WriteLine();
            Output.WriteLine("Bare susceptibility calculation completed.");
            Output.WriteLine();

            double factor = InteractionAdjustment(rpa, S, C, tb);

            if (tb.Interactions.AdjustInteractions)
            {
                Output.WriteLine("Multiplying interactions by {0}.", factor);

                for (int i = 0; i < rpa.Count; i++)
                {
                    S[i] *= factor;
                    C[i] *= factor;
                }
            }
            else if (factor < 1)
            {
                Output.WriteLine("WARNING:  There will be divergent geometric series.");
                Output.WriteLine("          Interpret results with care!");
            }

            Output.WriteLine();
            Output.WriteLine("Calculating dressed susceptibilities.");
            Output.WriteLine();

            RpaParams largestParams = null;
            double largest = 0;
            string indices = "";
            bool charge = false;

            for (int i = 0; i < rpa.Count; i++)
            {
                Matrix s_denom = (ident - S[i] * rpa[i].X0);
                Matrix c_denom = (ident + C[i] * rpa[i].X0);

                Matrix s_inv = s_denom.Invert();
                Matrix c_inv = c_denom.Invert();

                System.Diagnostics.Debug.Assert((s_denom * s_inv).IsIdentity);

                rpa[i].Xs = rpa[i].X0 * s_inv;
                rpa[i].Xc = rpa[i].X0 * c_inv;

                for (int l1 = 0; l1 < tb.Orbitals.Count; l1++)
                {
                    for (int l2 = 0; l2 < tb.Orbitals.Count; l2++)
                    {
                        for (int l3 = 0; l3 < tb.Orbitals.Count; l3++)
                        {
                            for (int l4 = 0; l4 < tb.Orbitals.Count; l4++)
                            {
                                int a = GetIndex(tb, l1, l2);
                                int b = GetIndex(tb, l3, l4);

                                bool found = false;

                                if (rpa[i].Xs[a,b].MagnitudeSquared > largest)
                                {
                                    largest = rpa[i].Xs[a,b].MagnitudeSquared;
                                    charge = false;
                                    found = true;
                                }
                                if (rpa[i].Xc[a, b].MagnitudeSquared > largest)
                                {
                                    largest = rpa[i].Xc[a, b].MagnitudeSquared;
                                    charge = true;
                                    found = true;
                                }
                                if (found == false)
                                    continue;

                                indices = string.Format("{0}{1}{2}{3}", l1, l2, l3, l4);
                                largestParams = rpa[i];
                            }
                        }
                    }
                }
            }

            Output.WriteLine("Largest susceptibility found at:");
            Output.WriteLine("    {0} susceptibility: {1}", charge ? "Charge" : "Spin", Math.Sqrt(largest));
            Output.WriteLine("    Indices: {0}", indices);
            Output.WriteLine("    Temperature: {0}", largestParams.Temperature);
            Output.WriteLine("    Frequency: {0}", largestParams.Frequency);
            Output.WriteLine("    Chemical Potential: {0}", largestParams.ChemicalPotential);
            Output.WriteLine("    Q: {0}", largestParams.QptValue);
        }
Esempio n. 47
0
        void WriteBands(TightBinding tb, KptList kpts, StreamWriter w)
        {
            int bandCount = kpts.Kpts[0].Wavefunctions.Count;

            BandTetrahedron tet = null;

            for (int i = 0; i < tb.KPath.Kpts.Count; i++)
            {
                var kpt = tb.KPath.Kpts[i];

                if (tet == null || tet.Contains(kpt) == false)
                {
                    GetTetrahedron(tb, kpt, kpts);
                }

                w.Write(i);
                w.Write("   ");

                for (int band = 0; band < bandCount; band++)
                {
                    double energy = tet.Interpolate(kpt);

                    w.Write("{0}  ", energy);
                }

                w.WriteLine();
            }
        }
Esempio n. 48
0
 public Wavefunction Bands(TightBinding tb, int kpt, int band)
 {
     return(tb.KMesh.AllKpts[kpt].Wavefunctions[band]);
 }
Esempio n. 49
0
        Matrix CalcX0(TightBinding tb, double freq, Vector3 q)
        {
            int orbitalCount = tb.Orbitals.Count;
            int size = orbitalCount * orbitalCount;
            Matrix x = new Matrix(size, size);

            Complex denom_factor = new Complex(0, 1e-4);

            //StreamWriter w = new StreamWriter(string.Format("qcont.{0}", q.ToString("0.000")));
            //bool writeThis = false;

            for (int l1 = 0; l1 < orbitalCount; l1++)
            {
                for (int l4 = 0; l4 < orbitalCount; l4++)
                {
                    for (int l3 = l1; l3 < orbitalCount; l3++)
                    {
                        for (int l2 = l4; l2 < orbitalCount; l2++)
                        {
                            int i = GetIndex(tb, l1, l2);
                            int j = GetIndex(tb, l3, l4);
                            bool foundSymmetry = false;

                            //if (l1 == 0 && l2 == 0 && l3 == 0 && l4 == 0)
                            //    writeThis = true;
                            //else
                                //writeThis = false;

                            //if (writeThis)
                            //    w.WriteLine("{0}{1}{2}{3}", l1, l2, l3, l4);

                            for (int s = 0; s < tb.Symmetries.Count; s++)
                            {
                                Symmetry sym = tb.Symmetries[s];

                                if (sym.OrbitalTransform == null || sym.OrbitalTransform.Count == 0)
                                    continue;

                                int newL1 = sym.OrbitalTransform[l1];
                                int newL2 = sym.OrbitalTransform[l2];
                                int newL3 = sym.OrbitalTransform[l3];
                                int newL4 = sym.OrbitalTransform[l4];

                                int newI = GetIndex(tb, newL1, newL2);
                                int newJ = GetIndex(tb, newL3, newL4);

                                if (newI == i && newJ == j)
                                    continue;

                                foundSymmetry = true;

                                if (newL1 > l1) foundSymmetry = false;
                                if (newL2 > l2) foundSymmetry = false;
                                if (newL3 > l3) foundSymmetry = false;
                                if (newL4 > l4) foundSymmetry = false;

                                if (foundSymmetry)
                                {
                                    x[i, j] = x[newI, newJ];
                                    x[j, i] = x[i, j].Conjugate();

                                    break;
                                }
                            }

                            if (foundSymmetry)
                                continue;

                            Complex total = 0;

                            for (int allkindex = 0; allkindex < tb.KMesh.AllKpts.Count; allkindex++)
                            {
                                Complex val = 0;
                                Vector3 k = tb.KMesh.AllKpts[allkindex];
                                Vector3 kq = k + q;

                                //List<int> kOrbitalMap;
                                //List<int> kqOrbitalMap;

                                //int kindex = tb.KMesh.IrreducibleIndex(k, tb.Lattice, tb.Symmetries, out kOrbitalMap);
                                //int kqindex = tb.KMesh.IrreducibleIndex(kq, tb.Lattice, tb.Symmetries, out kqOrbitalMap);
                                int kindex = tb.KMesh.AllKindex(k, tb.Lattice);
                                int kqindex = tb.KMesh.AllKindex(kq, tb.Lattice);

                                System.Diagnostics.Debug.Assert(kindex == allkindex);

                                //int newL1 = TransformOrbital(kqOrbitalMap, l1);
                                //int newL2 = TransformOrbital(kOrbitalMap, l2);
                                //int newL3 = TransformOrbital(kqOrbitalMap, l3);
                                //int newL4 = TransformOrbital(kOrbitalMap, l4);

                                for (int n1 = 0; n1 < orbitalCount; n1++)
                                {
                                    Wavefunction wfk = Bands(tb, kindex, n1);
                                    double e1 = wfk.Energy;
                                    double f1 = wfk.FermiFunction;

                                    for (int n2 = 0; n2 < orbitalCount; n2++)
                                    {
                                        Wavefunction wfq = Bands(tb, kqindex, n2);
                                        double e2 = wfq.Energy;
                                        double f2 = wfq.FermiFunction;

                                        Complex coeff =
                                            wfq.Coeffs[l1] * wfq.Coeffs[l4].Conjugate() *
                                            wfk.Coeffs[l3] * wfk.Coeffs[l2].Conjugate();

                                        if (coeff == 0) continue;
                                        if (f1 < 1e-15 && f2 < 1e-15) continue;

                                        Complex denom_p = (e2 - e1 + freq + denom_factor);
                                        //Complex denom_n = (e2 - e1 - freq - denom_factor);
                                        //Complex lindhard = (f1 - f2) * (1.0 / denom_p + 1.0 / denom_n);
                                        Complex lindhard = (f1 - f2) * (1.0 / denom_p);
                                        Complex contrib = coeff * lindhard;

                                        if (Math.Abs(f1 - f2) < 1e-11 && freq == 0.0)
                                        {
                                            contrib = coeff * f1 * (1 - f1) * Beta;
                                        }

                                        //w.Write("{0}  {1}   {2}   {3}           ", kindex, kqindex, n1, n2);
                                        //w.WriteLine("{0}   {1}   {2}   {3}   {4}", coeff, e1, e2, f1, f2);

                                        if (double.IsNaN(contrib.RealPart) || double.IsNaN(contrib.ImagPart))
                                        {
                                            throw new Exception("Found NaN when evaluating X0");
                                        }

                                        val += contrib;
                                    }
                                }

                                //w.WriteLine("{0}  {1}   total           {2} + {3}i", kindex, kqindex,
                                //    Math.Round(val.RealPart, 4), Math.Round(val.ImagPart, 4));

                                //Output.WriteLine(tb.KMesh.AllKpts[kindex].Weight.ToString());
                                val *= tb.KMesh.AllKpts[kindex].Weight;
                                total += val;

                                //if (writeThis)
                                //    w.WriteLine("{0}        {1}              {2}", allkindex, total, val);
                            }

                            x[i, j] = total;
                            x[j, i] = total.Conjugate();

                            //if (writeThis)
                            //{
                            //    w.WriteLine("total for {0}{1}{2}{3}: {4}", l1, l2, l3, l4, total);
                            //    w.WriteLine("---------------------");
                            //}
                        }
                    }
                }
            }

            //w.Close();

            return x;
        }
Esempio n. 50
0
        private RpaThreadInfo[] CreateThreadInfos(TightBinding tb, List<RpaParams> rpa, KptList qpts)
        {
            RpaThreadInfo[] infos = new RpaThreadInfo[threads];

            for (int i = 0; i < infos.Length; i++)
            {
                infos[i] = new RpaThreadInfo();
                infos[i].tb = tb.Clone();
                infos[i].qpts = qpts;
            }

            infos[0].PrimaryThread = true;

            for (int i = 0; i < rpa.Count; i++)
            {
                infos[i % threads].RpaParams.Add(rpa[i]);
            }

            return infos;
        }
Esempio n. 51
0
        /// <summary>
        /// This function does not work with symmetries, so it is unused.
        /// </summary>
        /// <param name="inp"></param>
        /// <param name="outputfile"></param>
        void tet_DoDensityOfStates(TightBinding.TbInputFileReader inp)
        {
            KptList ks = KMesh;
            StreamWriter outf = new StreamWriter(outputfile + ".dos");

            double smearing = TemperatureMesh[0];
            double smearNorm = 1 / smearing * Math.Pow(Math.PI, -0.5);
            double oneOverSmearSquared = Math.Pow(smearing, -2);

            double emin, emax;
            Hoppings.EnergyScale(out emin, out emax);

            emin -= smearing * 5;
            emax += smearing * 5;

            int epts = 2000;

            double[] energyGrid = new double[epts];
            double[,] dos = new double[epts, Orbitals.Count + 1];

            smearNorm /= ks.Kpts.Count;

            for (int i = 0; i < epts; i++)
            {
                energyGrid[i] = emin + (emax - emin) * i / (double)(epts - 1);
            }

            Output.WriteLine("Calculating DOS from {0} to {1} with tetrahedron method.",
                              emin, emax, smearing);

            Output.WriteLine("Using {0} tetrahedrons.", ks.Tetrahedrons.Count);

            for (int tetindex = 0; tetindex < ks.Tetrahedrons.Count; tetindex++)
            {
                Tetrahedron tet = ks.Tetrahedrons[tetindex];
                if (tetindex % (ks.Tetrahedrons.Count / 10) == 0 && tetindex > 0)
                    Output.WriteLine("At {0}...", tetindex);

                Matrix[] eigenvals = new Matrix[4];

                for (int i = 0; i < 4; i++)
                {
                    Matrix m = CalcHamiltonian(tet.Corners[i]);
                    Matrix vals, vecs;
                    m.EigenValsVecs(out vals, out vecs);

                    eigenvals[i] = vals;
                }

                for (int nband = 0; nband < eigenvals[0].Rows; nband++)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        tet.Values[i] = eigenvals[i][nband, 0].RealPart;
                    }

                    tet.SortCorners();

                    int estart = FindIndex(energyGrid, tet.Values[0]);
                    int eend = FindIndex(energyGrid, tet.Values[3]);

                    for (int ei = estart; ei < eend; ei++)
                    {
                        dos[ei, 0] += tet.IntegrateArea(energyGrid[ei]);
                    }
                }
            }

            for (int i = 0; i < epts; i++)
            {
                dos[i, 0] /= ks.Tetrahedrons.Count;
            }

            for (int i = 0; i < epts; i++)
            {
                outf.Write("{0}     ", energyGrid[i]);

                for (int j = 0; j < Orbitals.Count + 1; j++)
                {
                    outf.Write("{0}  ", dos[i, j]);
                }

                outf.WriteLine();
            }

            outf.Close();

            Output.WriteLine("Creating +coeff file.");
            outf = new StreamWriter(Path.Combine(Path.GetDirectoryName(outputfile), "+coeff"));

            outf.WriteLine("#\t1\t0\t" + ks.Kpts.Count.ToString());
            outf.Write("# band index\te(k,n)\t");

            for (int i = 0; i < Orbitals.Count; i++)
            {
                if (string.IsNullOrEmpty(Orbitals[i].Name))
                {
                    outf.Write("TB{0}\t", i);
                }
                else
                    outf.Write("{0}\t", Orbitals[i].Name);
            }
            outf.WriteLine();

            for (int kindex = 0; kindex < ks.Kpts.Count; kindex++)
            {
                Matrix m = CalcHamiltonian( ks.Kpts[kindex]);
                Matrix vals, vecs;
                m.EigenValsVecs(out vals, out vecs);

                outf.WriteLine("# spin=    1 k={0}", ks.Kpts[kindex].Value);

                for (int i = 0; i < vals.Rows; i++)
                {
                    outf.Write("{0}     {1}    ", i + 1, vals[i, 0].RealPart);

                    for (int j = 0; j < vecs.Columns; j++)
                    {
                        outf.Write("{0}    {1}    ", vecs[i, j].RealPart, vecs[i, j].ImagPart);
                    }
                    outf.WriteLine();
                }
            }
        }
Esempio n. 52
0
 int GetIndex(TightBinding tb, int l1, int l2)
 {
     // if this changes, be sure to correct the way
     // x[i,j] and x[j,i] are set in CalcX0.
     return l1 * tb.Orbitals.Count + l2;
 }
Esempio n. 53
0
 public TbInputFileReader(string filename, TightBinding tb)
     : base(filename)
 {
     this.tb = tb;
 }
Esempio n. 54
0
        private void SaveByQPlane(TightBinding tb, List <KPoint> QMesh, List <RpaParams> rpa, MatrixGetter g, string name)
        {
            rpa.Sort(RpaParams.QIndexComparison);

            Complex[] chisum    = new Complex[rpa.Count];
            double[]  chimag    = new double[rpa.Count];
            double[]  chimagsqr = new double[rpa.Count];

            for (int l1 = 0; l1 < tb.Orbitals.Count; l1++)
            {
                for (int l2 = 0; l2 < tb.Orbitals.Count; l2++)
                {
                    for (int l3 = 0; l3 < tb.Orbitals.Count; l3++)
                    {
                        for (int l4 = 0; l4 < tb.Orbitals.Count; l4++)
                        {
                            double lastFreq = double.MinValue;
                            double lastMu   = double.MinValue;
                            double lastq    = int.MinValue;

                            int baseIndex = 0;

                            for (int ti = 0; ti < tb.TemperatureMesh.Length; ti++)
                            {
                                for (int ui = 0; ui < tb.MuMesh.Length; ui++)
                                {
                                    for (int wi = 0; wi < tb.FrequencyMesh.Length; wi++)
                                    {
                                        string filename_re = string.Format("{0}.re.{1}{2}{3}{4}.w{5}.T{6}.u{7}.qm",
                                                                           name, l1, l2, l3, l4, wi, ti, ui);
                                        string filename_im = string.Format("{0}.im.{1}{2}{3}{4}.w{5}.T{6}.u{7}.qm",
                                                                           name, l1, l2, l3, l4, wi, ti, ui);
                                        string filename_mag = string.Format("{0}.mag.{1}{2}{3}{4}.w{5}.T{6}.u{7}.qm",
                                                                            name, l1, l2, l3, l4, wi, ti, ui);

                                        Complex maxvalue = new Complex(double.MinValue, double.MinValue);
                                        Complex minvalue = new Complex(double.MaxValue, double.MaxValue);

                                        using (StreamWriter w_re = new StreamWriter(filename_re))
                                            using (StreamWriter w_im = new StreamWriter(filename_im))
                                                using (StreamWriter w_mag = new StreamWriter(filename_mag))
                                                {
                                                    double last_t;
                                                    double last_s;

                                                    tb.QPlane.GetPlaneST(tb.QPlane.AllKpts[0], out last_s, out last_t);

                                                    for (int qi = 0; qi < tb.QPlane.AllKpts.Count; qi++)
                                                    {
                                                        Vector3    qpt = tb.QPlane.AllKpts[qi];
                                                        List <int> orbitalMap;

                                                        double s, t;
                                                        tb.QPlane.GetPlaneST(tb.QPlane.AllKpts[qi], out s, out t);

                                                        if (Math.Abs(t - last_t) > 1e-6)
                                                        {
                                                            w_re.WriteLine();
                                                            w_im.WriteLine();
                                                            w_mag.WriteLine();
                                                        }

                                                        int kindex =
                                                            tb.QPlane.IrreducibleIndex(qpt, tb.Lattice, tb.Symmetries, out orbitalMap);

                                                        int index = GetRpaIndex(rpa, kindex,
                                                                                tb.TemperatureMesh[ti],
                                                                                tb.FrequencyMesh[wi],
                                                                                tb.MuMesh[ui]);

                                                        int newL1 = tb.Symmetries.TransformOrbital(orbitalMap, l1);
                                                        int newL2 = tb.Symmetries.TransformOrbital(orbitalMap, l2);
                                                        int newL3 = tb.Symmetries.TransformOrbital(orbitalMap, l3);
                                                        int newL4 = tb.Symmetries.TransformOrbital(orbitalMap, l4);

                                                        int newii = GetIndex(tb, newL1, newL2);
                                                        int newjj = GetIndex(tb, newL3, newL4);

                                                        Complex val = g(rpa[index])[newii, newjj];

                                                        w_re.WriteLine(" {0}       {1}       {2:0.0000000}", s, t, val.RealPart);
                                                        w_im.WriteLine(" {0}       {1}       {2:0.0000000}", s, t, val.ImagPart);
                                                        w_mag.WriteLine(" {0}       {1}       {2:0.0000000}", s, t, val.Magnitude);

                                                        if (val.RealPart > maxvalue.RealPart)
                                                        {
                                                            maxvalue.RealPart = val.RealPart;
                                                        }
                                                        if (val.ImagPart > maxvalue.ImagPart)
                                                        {
                                                            maxvalue.ImagPart = val.ImagPart;
                                                        }
                                                        if (val.RealPart < minvalue.RealPart)
                                                        {
                                                            minvalue.RealPart = val.RealPart;
                                                        }
                                                        if (val.ImagPart < minvalue.ImagPart)
                                                        {
                                                            minvalue.ImagPart = val.ImagPart;
                                                        }

                                                        last_t = t;
                                                        last_s = s;
                                                    }
                                                }

                                        for (int i = 0; i < 3; i++)
                                        {
                                            string filename;
                                            switch (i)
                                            {
                                            case 0: filename = filename_re; break;

                                            case 1: filename = filename_im; break;

                                            case 2: filename = filename_mag; break;

                                            default:
                                                continue;
                                            }

                                            string gpfilename = "gnuplot." + filename;

                                            //minvalue.RealPart = Math.Floor(minvalue.RealPart);
                                            //maxvalue.RealPart = Math.Ceiling(maxvalue.RealPart);

                                            using (StreamWriter w = new StreamWriter(gpfilename))
                                            {
                                                w.WriteLine("#!/usr/bin/gnuplot");
                                                //w.WriteLine("set pm3d at bs flush center ftriangles scansbackward interpolate 1,1");
                                                w.WriteLine("set pm3d map flush center ftriangles scansbackward interpolate 5,5");
                                                w.WriteLine("set palette rgbformula 23,9,-36");
                                                //w.WriteLine("set border 895");
                                                w.WriteLine("set key off");
                                                //w.WriteLine("set zrange [{0}:{1}]", minvalue.RealPart, maxvalue.RealPart);
                                                // label z = minvalue - 0.5 * (maxvalue - minvalue)
                                                //  set label 1 "G" at 0,0,1 font "Symbol" center front
                                                w.WriteLine("splot '{0}' with pm3d", filename);
                                            }
                                        }
                                    }

                                    baseIndex += QMesh.Count;
                                }
                            }
                        }
                    }
                }
            }
        }