Exemplo n.º 1
0
        public void setOutletBC(string parameters)
        {
            List <PressureOutletRCR> oultlet_bc_set = new List <PressureOutletRCR>();

            if (parameters != "")
            {
                List <BC_params> rcr_params = new List <BC_params>();
                IO_Module.LoadBC_RCR_paramsFromString(parameters, out rcr_params);

                foreach (var bc_params in rcr_params)
                {
                    try
                    {
                        int ind = v_net.bounds.FindIndex(x => x.core_node.id == bc_params.id);
                        v_net.bounds[ind] = new PressureOutletRCR(v_net.bounds[ind], GlobalDefs.getBoileauBeta, bc_params.R1, bc_params.R2, bc_params.C);
                        oultlet_bc_set.Add((PressureOutletRCR)v_net.bounds[ind]);
                    }
                    catch { }
                }

                return;
            }

            for (int i = 0; i < v_net.bounds.Count; i++)
            {
                BoundaryCondition bn = v_net.bounds[i];
                bn = new PressureOutletRCR(bn, GlobalDefs.getBoileauBeta, 0.0, 0.0, 0.0);
                oultlet_bc_set.Add((PressureOutletRCR)bn);
            }
        }
Exemplo n.º 2
0
        public BFSimulator(string top_text, int[] inlet_nodes, List <ClotNode> clotnodes)
        {
            timestep_N = 0;
            init       = true;

            end_time     = -1.0f;
            current_time = 0.0f;

            IO_Module.localization = "en-US";

            //TableFunction heart_inlet = null;
            List <BC_params> rcr_params = new List <BC_params>();

            try
            {
                IO_Module.LoadTopologyFromString(top_text, out v_net);
            }
            catch { }


            GlobalDefs.ZERO_POINT = v_net.vascular_system[0].position;

            getFloatValueDelegate getProximaDst;             // special delerates for wide-width search on arterial network
            setFloatValueDelegate setProximaDst;             // -"-

            v_net.defineNodeDirVectors(inlet_nodes, out getProximaDst, out setProximaDst);

            v_net.defineNet(getProximaDst, setProximaDst, clotnodes);

            for (int i = 0; i < v_net.threads.Count; i++)
            {
                v_net.specifyThreadType(i, new ElasticThread(v_net.threads[i], GlobalDefs.getBoileauBeta));
            }

            // WaveTransmissive BCs are default, just pass pressure wave outside (have very small reflection coeff.)
            // No inlet BC are by def. It should be defined separatelly.
            for (int i = 0; i < v_net.bounds.Count; i++)
            {
                // v_net.bounds[i] = new WaveTransmissive(v_net.bounds[i], GlobalDefs.getBoileauBeta);//new PressureOutletRCR(v_net.bounds[i], GlobalDefs.getBoileauBeta, 0.1e+9, 0.1e+9, 1.0e-12); // //
                v_net.bounds[i] = new PressureOutletRCR(v_net.bounds[i], GlobalDefs.getBoileauBeta, 2.0e+9, 100.0e+9, 0.1e-12);                 // //
            }
            for (int i = 0; i < v_net.knots.Count; i++)
            {
                v_net.knots[i] = new StandartKnot(v_net.knots[i], GlobalDefs.getBoileauBeta);
            }


            control_point_list = new List <NodeSummary>();

            state = SolutionState.CONTINUE;
        }
Exemplo n.º 3
0
        static public TableFunction xScaleTableFunction(double timestep, double period, double new_period, TableFunction t_f)
        {
            int N = (int)Math.Ceiling(period / timestep);

            timestep = period / N;
            double[,] table_function = new double[N, 2];

            double scale = new_period / period;

            for (int i = 0; i < N; i++)
            {
                table_function[i, 0] = timestep * i * scale;
                table_function[i, 1] = t_f(timestep * i) / scale;
            }
            return(IO_Module.makeTableFunction(table_function));
        }
Exemplo n.º 4
0
        public void setInletBC(string inlet_data, int node_number, InletType type)
        {
            TableFunction inlet_function = null;

            IO_Module.LoadTableFunctionFromString(inlet_data, out inlet_function);
            int id = v_net.bounds.FindIndex(x => x.core_node.id == node_number);

            if (type == InletType.FLUX)
            {
                v_net.bounds[id] = new InletFlux(v_net.bounds[id], inlet_function, GlobalDefs.getBoileauBeta);
            }

            if (type == InletType.PRESSURE)
            {
                v_net.bounds[id] = new InletPressure(v_net.bounds[id], inlet_function, GlobalDefs.getBoileauBeta);
            }
        }
Exemplo n.º 5
0
        public double setHeartRate(int HR, double timestep)
        {
            if (HR > 120 || HR < 40)
            {
                return(0);
            }

            double new_period = 60.0 / HR;

            foreach (var bc in this.bounds)
            {
                if (bc.GetType() == typeof(InletFlux))
                {
                    InletFlux inlet_bc = (InletFlux)bc; //TODO: replace by interface on inlet type of BCs
                    inlet_bc.flux_on_time = IO_Module.xScaleTableFunction(timestep, inlet_bc.base_period, new_period, inlet_bc.base_flux_on_time);
                }
            }

            return(new_period);
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            // start timer
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            //BFSimulator bf_simuation = new BFSimulator("", @"verefication\full_body_Boileau_LR_2.5mm.top", GlobalDefs.getBoileauBeta);
            IO_Module.localization = "en-US";
            string top_filename        = "";
            string parameters_filename = "";
            string out_filename        = "";
            string stream_out_filename = "";
            string clot_filename       = "";

            List <ClotDscr>             stenosis_task = new List <ClotDscr>();
            List <Tuple <int, string> > inlet_data    = new List <Tuple <int, string> >();

            string base_path = IO_Module.readTaskFile(args[0], ref top_filename, ref parameters_filename, ref inlet_data, ref stenosis_task, ref out_filename, ref Coupled_Nodes_N);

            string dyn_out_filename = @"Results.dyn";

            if (args.GetLength(0) > 1)
            {
                //out_filename = args[1] + out_filename;
                //dyn_out_filename = args[1] + dyn_out_filename;
                dyn_out_filename = args[1];
            }

            string ResultFolder = new FileInfo(dyn_out_filename).Directory.FullName + Path.DirectorySeparatorChar;
            string ConvFile     = ResultFolder + "Conv.txt";
            //List<VascularNode> ClotNodes = new List<VascularNode>();
            List <ClotNode> Clot_task = new List <ClotNode>();

            if (args.GetLength(0) > 2)
            {
                clot_filename = args[2];
                IO_Module.ReadClotFile(clot_filename, ref Clot_task);
                //foreach (var n in Clot_task):
                //n.1

                //for (int i = 0; i < Clot_task.Count; i++)
                //ClotNodes = bf_simuation.getVNet().vascular_system.Find(x => x.id == Clot_task[i].Node_id);
            }

            System.IO.StreamWriter out_dynamics_file = new System.IO.StreamWriter(dyn_out_filename);
            stream_out_filename = out_filename + "s";

            bf_simuation = new BFSimulator("", base_path + top_filename, inlet_data.Select(t => t.Item1).ToArray(), Clot_task);

            foreach (var inlt in inlet_data)
            {
                bf_simuation.setInletBC(File.ReadAllText(base_path + inlt.Item2), inlt.Item1, InletType.FLUX);
            }


            if (parameters_filename != "")//outlet parameters
            {
                bf_simuation.setOutletBC(File.ReadAllText(base_path + parameters_filename));
            }

            bf_simuation.setTimeParameters(TIMESTEP, END_TIME, AV_TIME);
            // Start the Steady_network_solver
            //Process Process = new Process();
            if (Coupled_Nodes_N > 0)
            {
                var mmf = MemoryMappedFile.CreateFromFile("/tmp/sharedfile", FileMode.OpenOrCreate, "/tmp/sharedfile", Coupled_Nodes_N * 16);
                stream = mmf.CreateViewAccessor();
                Process.StartInfo.UseShellExecute = false;
                //Process.StartInfo.FileName = "./steady_network_solver_shared";
                Process.StartInfo.FileName               = "./coupled_solver";
                Process.StartInfo.Arguments              = "-term_comm 0";
                Process.StartInfo.CreateNoWindow         = true;
                Process.StartInfo.RedirectStandardOutput = true;
                Process.StartInfo.RedirectStandardInput  = true;
                Process.Start();
                Console.WriteLine("Starting the Coupled Solver.");
                System.Threading.Thread.Sleep(20000);
                Process.BeginOutputReadLine();
                Process.OutputDataReceived += bf_simuation.Set_Pressure_Steady_Solver;
            }
            Process ConvCheck = new Process();

            ConvCheck.StartInfo.FileName       = GlobalDefs.ScriptLocation;
            ConvCheck.StartInfo.Arguments      = ResultFolder;
            ConvCheck.StartInfo.CreateNoWindow = true;

            //for (int i = 0; i < Clot_task.Count; i++)
            //{
            //    Clot_task[i].Node = bf_simuation.getVNet().vascular_system.Find(x => x.id == Clot_task[i].Node_id);
            //    Clot_task[i].Node.blocked = true;
            //}
            //bf_simuation.ClotNodes = Clot_task;

            int HBLoop = 0;

            while (true)
            {
                Console.WriteLine("Current Heartbeat loop: " + HBLoop);
                while (true)
                {
                    // Output and writing timers
                    if (CurrentIter % OUTPUT_PERIOD == 0 || CurrentIter == SimulationSteps)
                    {
                        Console.WriteLine("Time: " + bf_simuation.current_time + " Iteration: " + CurrentIter);
                        IO_Module.WriteState(bf_simuation.current_time, bf_simuation.getVNet(), out_dynamics_file);
                        //Console.WriteLine("State is written.");
                        //output is given as flowrate in ml/s, pressure in pa, and radius in mm
                        if ((bf_simuation.current_time % OUTPUT_PERIOD * 10) < bf_simuation.delta_tau)
                        {
                            out_dynamics_file.Flush();
                        }
                    }
                    if (bf_simuation.solution_state == SolutionState.ERROR)
                    {
                        Console.WriteLine("Error; physical time: " + bf_simuation.current_time);
                        break;
                    }

                    if (bf_simuation.solution_state == SolutionState.FINISHED)
                    {
                        Console.WriteLine("Physical time: " + bf_simuation.current_time);
                        Console.WriteLine("End");
                        break;
                    }
                    if (CurrentIter == Program.SimulationSteps)
                    {
                        break;
                    }

                    CurrentIter += 1;
                    bf_simuation.Control(); // check for errors
                    bf_simuation.Update();  // compute next timestep
                    //clot_timer += bf_simuation.delta_tau;
                }

                //check convergence
                out_dynamics_file.Close();
                ConvCheck.Start();
                ConvCheck.WaitForExit();
                int      Conv  = 0;
                string[] lines = System.IO.File.ReadAllLines(ConvFile);
                Conv = int.Parse(lines[0]);
                // if converged, stop
                if (Conv == 1 || HBLoop == 20)
                {
                    break;
                }
                out_dynamics_file = new System.IO.StreamWriter(dyn_out_filename);
                HBLoop           += 1;
                // initiation of new heatbeat
                CurrentIter = 0;
                bf_simuation.ResetTime();
                //Console.WriteLine("Time: " + bf_simuation.current_time + " Iteration: " + CurrentIter);
                //IO_Module.WriteState(bf_simuation.current_time, bf_simuation.getVNet(), out_dynamics_file);
                //bf_simuation.timestep_N = 0;
                //bf_simuation.current_time = 0;
            }

            if (Coupled_Nodes_N > 0)                  // close program if it was open
            {
                Process.StandardInput.WriteLine("0"); //close program
                Process.WaitForExit();
            }
            // Output Elapsed time
            stopWatch.Stop();
            Console.WriteLine("Time elapsed: {0}", stopWatch.Elapsed);
        }