Ejemplo n.º 1
0
        private void loadNetworkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog() == DialogResult.OK && File.Exists(ofd.FileName))
            {
                try
                {
                    nw = SFNetworkOscillator.Debinarize(ofd.FileName);

                    // вот эта лажа - попытка линейно интерполировать значения между 2 значениями фаз.. работает, но качественно получается плохой результат
                    double n = 1;
                    for (int i = 0; i < nw.States.Count / n - 1; i++)
                    {
                        SFNetworkOscillatorState d  = nw.States[(int)(i * n)];
                        SFNetworkOscillatorState d2 = nw.States[(int)(i * n) + 1];
                        for (int j = 1; j < n; j++)
                        {
                            double   time = d.Time + (d2.Time - d.Time) / n * j;
                            double[] phs  = new double[d.Phases.Length];
                            for (int k = 0; k < phs.Length; k++)
                            {
                                phs[k] = d.Phases[k] + (d2.Phases[k] - d.Phases[k]) / n * j;
                            }
                            SFNetworkOscillatorState st = new SFNetworkOscillatorState(time, phs);
                            nw.States.Insert(i * (int)(n) + j, st);
                        }
                    }

                    VisualizeNetwork();
                    InitializeMacro();
                    InitializeHist();
                }
                catch (Exception ex) { MessageBox.Show("Error occured while loading file: " + ex.Message, "Loading error"); }
            }
        }
        public static SFNetworkOscillator Debinarize(string path)
        {
            try
            {
                int    version, nodeCount, mlt, seed, edgeCount, phaseCount, freqCount, stateCount;
                ulong  x;
                double strength,
                       freq_init_min,
                       freq_init_max,
                       phase_init_min,
                       phase_init_max,
                       time_init,
                       time_step,
                       solve_step,

                       time;
                using (BinaryReader br = new BinaryReader(File.OpenRead(path)))
                {
                    version   = br.ReadBytes(1)[0];
                    nodeCount = BitConverter.ToInt32(br.ReadBytes(4), 0);
                    mlt       = BitConverter.ToInt32(br.ReadBytes(4), 0);
                    seed      = BitConverter.ToInt32(br.ReadBytes(4), 0);
                    x         = (ulong)BitConverter.ToInt64(br.ReadBytes(8), 0);

                    edgeCount = BitConverter.ToInt32(br.ReadBytes(4), 0);
                    List <Edge> edges = new List <Edge>();
                    for (int i = 0; i < edgeCount; i++)
                    {
                        edges.Add(Edge.Debinarize(br));
                    }

                    strength       = BitConverter.ToDouble(br.ReadBytes(8), 0);
                    freq_init_min  = BitConverter.ToDouble(br.ReadBytes(8), 0);
                    freq_init_max  = BitConverter.ToDouble(br.ReadBytes(8), 0);
                    phase_init_min = BitConverter.ToDouble(br.ReadBytes(8), 0);
                    phase_init_max = BitConverter.ToDouble(br.ReadBytes(8), 0);
                    time_init      = BitConverter.ToDouble(br.ReadBytes(8), 0);
                    time_step      = BitConverter.ToDouble(br.ReadBytes(8), 0);
                    solve_step     = BitConverter.ToDouble(br.ReadBytes(8), 0);
                    time           = BitConverter.ToDouble(br.ReadBytes(8), 0);

                    phaseCount = BitConverter.ToInt32(br.ReadBytes(4), 0);
                    List <double> phases = new List <double>();
                    for (int i = 0; i < phaseCount; i++)
                    {
                        phases.Add(BitConverter.ToDouble(br.ReadBytes(8), 0));
                    }

                    freqCount = BitConverter.ToInt32(br.ReadBytes(4), 0);
                    List <double> freqs = new List <double>();
                    for (int i = 0; i < freqCount; i++)
                    {
                        freqs.Add(BitConverter.ToDouble(br.ReadBytes(8), 0));
                    }

                    stateCount = BitConverter.ToInt32(br.ReadBytes(4), 0);
                    List <SFNetworkOscillatorState> states = new List <SFNetworkOscillatorState>();
                    for (int i = 0; i < stateCount; i++)
                    {
                        states.Add(SFNetworkOscillatorState.Debinarize(br));
                    }

                    return(new SFNetworkOscillator(nodeCount, mlt, seed, x, edges.ToArray(), strength, freq_init_min, freq_init_max, phase_init_min, phase_init_max, time_init, time_step, solve_step, time, phases.ToArray(), freqs.ToArray(), states.ToArray()));
                }
            }
            catch (Exception e) { return(null); }
        }