Example #1
0
        private static Tuple <Model, ComsolMeshReader> CreateModel()
        {
            string           filename    = Path.Combine(Directory.GetCurrentDirectory(), "InputFiles", "TumorGrowthModel", "mesh1.mphtxt");
            ComsolMeshReader modelReader = new ComsolMeshReader(filename);
            Model            model       = modelReader.CreateModelFromFile();
            //Boundary Conditions
            var flux = new FluxLoad(10);
            var dir1 = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, 0));
            });
            var dir2 = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, 1));
            });
            var weakDirichlet1 = new WeakDirichlet(dir1, modelReader.diffusionCeoff);
            var weakDirichlet2 = new WeakDirichlet(dir2, modelReader.diffusionCeoff);

            var dirichletFactory1 = new SurfaceLoadElementFactory(weakDirichlet1);
            var dirichletFactory2 = new SurfaceLoadElementFactory(weakDirichlet2);
            var fluxFactory       = new SurfaceLoadElementFactory(flux);

            int[] boundaryIDs = new int[] { 2, 7, 8, 9 };
            foreach (int boundaryID in boundaryIDs)
            {
                foreach (Element element in modelReader.elementBoundaries[boundaryID])
                {
                    IReadOnlyList <Node> nodes = (IReadOnlyList <Node>)element.Nodes;
                    var dirichletElement1      = dirichletFactory1.CreateElement(CellType.Tri3, nodes);
                    model.SurfaceLoads.Add(dirichletElement1);
                    //var surfaceElement = new SurfaceLoadElement();
                    //element.ID = TriID;
                    //surfaceElement.ElementType = DirichletElement1;
                    //model.SubdomainsDictionary[0].Elements.Add(dirichletElement1);
                    //model.ElementsDictionary.Add(TriID, surfaceElement);

                    //model.NodesDictionary[surfaceElement.ID].Constraints.Add(new Constraint() { DOF = ThermalDof.Temperature, Amount = 100 });
                }
            }
            boundaryIDs = new int[] { 0, 1, 3, 4, 6 };
            foreach (int boundaryID in boundaryIDs)
            {
                foreach (Element element in modelReader.elementBoundaries[boundaryID])
                {
                    IReadOnlyList <Node> nodes = (IReadOnlyList <Node>)element.Nodes;
                    var dirichletElement2      = dirichletFactory2.CreateElement(CellType.Tri3, nodes);
                    model.SurfaceLoads.Add(dirichletElement2);
                }
            }

            return(new Tuple <Model, ComsolMeshReader>(model, modelReader));
        }
        public void ConstructorTest4()
        {
            // Create a Dirichlet with the following concentrations
            var dirich = new DirichletDistribution(0.42, 0.57, 1.2);

            // Common measures
            double[] mean   = dirich.Mean;     // { 0.19, 0.26, 0.54 }
            double[] median = dirich.Median;   // { 0.19, 0.26, 0.54 }
            double[] var    = dirich.Variance; // { 0.048, 0.060, 0.077 }
            double[,] cov = dirich.Covariance; // see below


            //       0.0115297440926238 0.0156475098399895 0.0329421259789253
            // cov = 0.0156475098399895 0.0212359062114143 0.0447071709713986
            //       0.0329421259789253 0.0447071709713986 0.0941203599397865

            // (the above matrix representation has been transcribed to text using)
            string str = cov.ToString(DefaultMatrixFormatProvider.InvariantCulture);


            // Probability mass functions
            double pdf1 = dirich.ProbabilityDensityFunction(new double[] { 2, 5 });    // 0.12121671541846207
            double pdf2 = dirich.ProbabilityDensityFunction(new double[] { 4, 2 });    // 0.12024840322466089
            double pdf3 = dirich.ProbabilityDensityFunction(new double[] { 3, 7 });    // 0.082907634905068528
            double lpdf = dirich.LogProbabilityDensityFunction(new double[] { 3, 7 }); // -2.4900281233124044


            Assert.AreEqual(0.19178082191780821, mean[0]);
            Assert.AreEqual(0.26027397260273971, mean[1]);
            Assert.AreEqual(0.547945205479452, median[2]);
            Assert.AreEqual(0.19178082191780821, median[0]);
            Assert.AreEqual(0.26027397260273971, median[1]);
            Assert.AreEqual(0.547945205479452, median[2]);
            Assert.AreEqual(0.048589635818914775, var[0]);
            Assert.AreEqual(0.060354680811388089, var[1]);
            Assert.AreEqual(0.077649296950323854, var[2]);
            Assert.AreEqual(0.011529744092623844, cov[0, 0]);
            Assert.AreEqual(0.015647509839989502, cov[0, 1]);
            Assert.AreEqual(0.015647509839989502, cov[1, 0]);
            Assert.AreEqual(0.021235906211414326, cov[1, 1]);
            Assert.AreEqual(0.12121671541846207, pdf1);
            Assert.AreEqual(0.12024840322466089, pdf2);
            Assert.AreEqual(0.082907634905068528, pdf3);
            Assert.AreEqual(-2.4900281233124044, lpdf);
        }
        private static Tuple <Model, IModelReader> CreateConvectionDiffusionModel2(double k, double[] U, double L, double b, double f, double bl)
        {
            string filename    = Path.Combine(Directory.GetCurrentDirectory(), "InputFiles", "TumorGrowthModel", "9hexa.mphtxt");
            var    modelReader = new ComsolMeshReader2(filename, k, U, L);
            Model  model       = modelReader.CreateModelFromFile();
            var    material    = new ConvectionDiffusionMaterial(k, U, L);
            //Boundary Conditions
            var flux1 = new FluxLoad(f);
            var dir1  = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, b));
            });
            //var dir2 = new DirichletDistribution(list =>
            //{
            //    return Vector.CreateWithValue(list.Count, b2);
            //});
            var weakDirichlet1 = new WeakDirichlet(dir1, k);
            //var weakDirichlet2 = new WeakDirichlet(dir2, k);

            var dirichletFactory1 = new SurfaceLoadElementFactory(weakDirichlet1);
            //var dirichletFactory2 = new SurfaceLoadElementFactory(weakDirichlet2);
            var fluxFactory1      = new SurfaceLoadElementFactory(flux1);
            var boundaryFactory3D = new SurfaceBoundaryFactory3D(0,
                                                                 new ConvectionDiffusionMaterial(k, new double[] { 0, 0, 0 }, 0));


            int[] domainIDs = new int[] { 0, };
            foreach (int domainID in domainIDs)
            {
                foreach (Element element in modelReader.elementDomains[domainID])
                {
                    ////IReadOnlyList<Node> nodes = (IReadOnlyList<Node>)element.Nodes;
                    //var fluxElement1 = fluxFactory1.CreateElement(CellType.Quad4, nodes);
                    //model.SurfaceLoads.Add(fluxElement1);
                    var bodyLoadElementCellType = element.ElementType.CellType;
                    var nodes                  = (IReadOnlyList <Node>)element.Nodes;
                    var domainLoad             = new ConvectionDiffusionDomainLoad(material, bl, ThermalDof.Temperature);
                    var bodyLoadElementFactory = new BodyLoadElementFactory(domainLoad, model);
                    var bodyLoadElement        = bodyLoadElementFactory.CreateElement(CellType.Hexa8, nodes);
                    model.BodyLoads.Add(bodyLoadElement);
                    //var surfaceElement = new SurfaceLoadElement();
                    //element.ID = TriID;
                    //surfaceElement.ElementType = DirichletElement1;
                    //model.SubdomainsDictionary[0].Elements.Add(dirichletElement1);
                    //model.ElementsDictionary.Add(TriID, surfaceElement);

                    //model.NodesDictionary[surfaceElement.ID].Constraints.Add(new Constraint() { DOF = ThermalDof.Temperature, Amount = 100 });
                }
            }
            //foreach (Node node in model.Nodes)
            //{
            //    model.Loads.Add(new Load() { Amount = bl[node.ID], Node = node, DOF = ThermalDof.Temperature });
            //}

            int[] boundaryIDs = new int[] { 0, };
            int   QuadID      = model.ElementsDictionary.Count + 1;

            foreach (int boundaryID in boundaryIDs)
            {
                foreach (IReadOnlyList <Node> nodes in modelReader.quadBoundaries[boundaryID])
                {
                    //var fluxElement1 = fluxFactory1.CreateElement(CellType.Quad4, nodes);
                    //model.SurfaceLoads.Add(fluxElement1);
                    //var dirichletElement1 = dirichletFactory1.CreateElement(CellType.Quad4, nodes);
                    //model.SurfaceLoads.Add(dirichletElement1);
                    //var SurfaceBoundaryElement = boundaryFactory3D.CreateElement(CellType.Quad4, nodes);
                    //var element = new Element();
                    //element.ID = QuadID;
                    //element.ElementType = SurfaceBoundaryElement;
                    //model.SubdomainsDictionary[0].Elements.Add(element);
                    //model.ElementsDictionary.Add(QuadID, element);
                    //foreach (Node node in nodes)
                    //{
                    //    element.AddNode(node);
                    //}
                    //QuadID += 1;
                }
            }
            boundaryIDs = new int[] { 5 };
            foreach (int boundaryID in boundaryIDs)
            {
                foreach (IReadOnlyList <Node> nodes in modelReader.quadBoundaries[boundaryID])
                {
                    //var fluxElement1 = fluxFactory1.CreateElement(CellType.Quad4, nodes);
                    //model.SurfaceLoads.Add(fluxElement1);
                    //var bodyLoadElement = bodyLoadElementFactory.CreateElement(CellType.Quad4, nodes);
                    //model.SurfaceLoads.Add(bodyLoadElement);
                    //var SurfaceBoundaryElement = boundaryFactory3D.CreateElement(CellType.Quad4, nodes);
                    //var element = new Element();
                    //element.ID = QuadID;
                    //element.ElementType = SurfaceBoundaryElement;
                    //model.SubdomainsDictionary[0].Elements.Add(element);
                    //model.ElementsDictionary.Add(QuadID, element);
                    //foreach (Node node in nodes)
                    //{
                    //    element.AddNode(node);
                    //}
                    //QuadID += 1;
                }
            }
            return(new Tuple <Model, IModelReader>(model, modelReader));
        }
Example #4
0
        private static Tuple <Model, ComsolMeshReader2> CreateModel(double k, double[] U, double L, double b1, double b2, double f1, double f2, double[] bl)
        {
            string            filename    = Path.Combine(Directory.GetCurrentDirectory(), "InputFiles", "TumorGrowthModel", "9hexa.mphtxt");
            ComsolMeshReader2 modelReader = new ComsolMeshReader2(filename, k, U, L);
            Model             model       = modelReader.CreateModelFromFile();
            //Boundary Conditions
            var flux1 = new FluxLoad(f1);
            var flux2 = new FluxLoad(f2);
            var dir1  = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, b1));
            });
            var dir2 = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, b2));
            });
            var weakDirichlet1 = new WeakDirichlet(dir1, k);
            var weakDirichlet2 = new WeakDirichlet(dir2, k);

            var dirichletFactory1 = new SurfaceLoadElementFactory(weakDirichlet1);
            var dirichletFactory2 = new SurfaceLoadElementFactory(weakDirichlet2);
            var fluxFactory1      = new SurfaceLoadElementFactory(flux1);
            var fluxFactory2      = new SurfaceLoadElementFactory(flux2);
            var boundaryFactory3D = new SurfaceBoundaryFactory3D(0,
                                                                 new ConvectionDiffusionMaterial(k, new double[] { 0, 0, 0 }, 0));

            foreach (Node node in model.Nodes)
            {
                model.Loads.Add(new Load()
                {
                    Amount = bl[node.ID], Node = node, DOF = ThermalDof.Temperature
                });
            }


            int[] boundaryIDs = new int[] { 0, };
            int   QuadID      = model.ElementsDictionary.Count + 1;

            foreach (int boundaryID in boundaryIDs)
            {
                foreach (IReadOnlyList <Node> nodes in modelReader.quadBoundaries[boundaryID])
                {
                    var fluxElement1 = fluxFactory1.CreateElement(CellType.Quad4, nodes);
                    model.SurfaceLoads.Add(fluxElement1);
                    var dirichletElement1 = dirichletFactory1.CreateElement(CellType.Quad4, nodes);
                    model.SurfaceLoads.Add(dirichletElement1);
                    var SurfaceBoundaryElement = boundaryFactory3D.CreateElement(CellType.Quad4, nodes);
                    var element = new Element();
                    element.ID          = QuadID;
                    element.ElementType = SurfaceBoundaryElement;
                    model.SubdomainsDictionary[0].Elements.Add(element);
                    model.ElementsDictionary.Add(QuadID, element);
                    foreach (Node node in nodes)
                    {
                        element.AddNode(node);
                    }
                    QuadID += 1;
                }
            }
            boundaryIDs = new int[] { 5 };
            foreach (int boundaryID in boundaryIDs)
            {
                foreach (IReadOnlyList <Node> nodes in modelReader.quadBoundaries[boundaryID])
                {
                    var fluxElement1 = fluxFactory1.CreateElement(CellType.Quad4, nodes);
                    model.SurfaceLoads.Add(fluxElement1);
                    var dirichletElement2 = dirichletFactory2.CreateElement(CellType.Quad4, nodes);
                    model.SurfaceLoads.Add(dirichletElement2);
                    var SurfaceBoundaryElement = boundaryFactory3D.CreateElement(CellType.Quad4, nodes);
                    var element = new Element();
                    element.ID          = QuadID;
                    element.ElementType = SurfaceBoundaryElement;
                    model.SubdomainsDictionary[0].Elements.Add(element);
                    model.ElementsDictionary.Add(QuadID, element);
                    foreach (Node node in nodes)
                    {
                        element.AddNode(node);
                    }
                    QuadID += 1;
                }
            }
            return(new Tuple <Model, ComsolMeshReader2>(model, modelReader));
        }
Example #5
0
        private static Tuple <Model, ComsolMeshReader2> CreateModel(double k, double[] U, double L)
        {
            string            filename    = Path.Combine(Directory.GetCurrentDirectory(), "InputFiles", "TumorGrowthModel", "mesh.mphtxt");
            ComsolMeshReader2 modelReader = new ComsolMeshReader2(filename, k, U, L);
            Model             model       = modelReader.CreateModelFromFile();
            //Boundary Conditions
            var flux1 = new FluxLoad(6.9e08);
            var flux2 = new FluxLoad(10);
            var dir1  = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, 1));
            });
            var dir2 = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, 0));
            });
            var weakDirichlet1 = new WeakDirichlet(dir1, k);
            var weakDirichlet2 = new WeakDirichlet(dir2, k);

            var dirichletFactory1 = new SurfaceLoadElementFactory(weakDirichlet1);
            var dirichletFactory2 = new SurfaceLoadElementFactory(weakDirichlet2);
            var fluxFactory1      = new SurfaceLoadElementFactory(flux1);
            var fluxFactory2      = new SurfaceLoadElementFactory(flux2);
            var boundaryFactory3D = new SurfaceBoundaryFactory3D(0,
                                                                 new ConvectionDiffusionMaterial(k, new double[] { 0, 0, 0 }, 0));


            int[] boundaryIDs = new int[] { 0, 1, 2 };
            //int numberOfQuadElements = 0;
            //for ( int i = 0; i<boundaryIDs.Length; i++)
            //{
            //    numberOfQuadElements += modelReader.quadBoundaries[boundaryIDs[i]].Count;
            //}
            int TriID = model.ElementsDictionary.Count + 1;

            foreach (int boundaryID in boundaryIDs)
            {
                foreach (IReadOnlyList <Node> nodes in modelReader.triBoundaries[boundaryID])
                {
                    //var fluxElement1 = fluxFactory1.CreateElement(CellType.Tri3, nodes);
                    //model.SurfaceLoads.Add(fluxElement1);
                    var dirichletElement1 = dirichletFactory1.CreateElement(CellType.Tri3, nodes);
                    model.SurfaceLoads.Add(dirichletElement1);
                    var SurfaceBoundaryElement = boundaryFactory3D.CreateElement(CellType.Tri3, nodes);
                    var element = new Element();
                    element.ID          = TriID;
                    element.ElementType = SurfaceBoundaryElement;
                    model.SubdomainsDictionary[0].Elements.Add(element);
                    model.ElementsDictionary.Add(TriID, element);
                    foreach (Node node in nodes)
                    {
                        element.AddNode(node);
                    }
                    TriID += 1;
                    // var surfaceElement = new SurfaceLoadElement();
                    //element.ID = TriID;
                    //surfaceElement.ElementType = DirichletElement1;
                    //model.SubdomainsDictionary[0].Elements.Add(dirichletElement1);
                    //model.ElementsDictionary.Add(TriID, surfaceElement);

                    //model.NodesDictionary[surfaceElement.ID].Constraints.Add(new Constraint() { DOF = ThermalDof.Temperature, Amount = 100 });
                }
            }
            boundaryIDs = new int[] { 5 };
            foreach (int boundaryID in boundaryIDs)
            {
                foreach (IReadOnlyList <Node> nodes in modelReader.quadBoundaries[boundaryID])
                {
                    //        //IReadOnlyList<Node> nodes = (IReadOnlyList<Node>)element.Nodes;
                    //        //var fluxElement1 = fluxFactory1.CreateElement(CellType.Quad4, nodes);
                    //        //model.SurfaceLoads.Add(fluxElement1);
                    var dirichletElement2 = dirichletFactory2.CreateElement(CellType.Quad4, nodes);
                    model.SurfaceLoads.Add(dirichletElement2);
                    var SurfaceBoundaryElement = boundaryFactory3D.CreateElement(CellType.Quad4, nodes);
                    var element = new Element();
                    element.ID          = TriID;
                    element.ElementType = SurfaceBoundaryElement;
                    model.SubdomainsDictionary[0].Elements.Add(element);
                    model.ElementsDictionary.Add(TriID, element);
                    foreach (Node node in nodes)
                    {
                        element.AddNode(node);
                    }
                    TriID += 1;
                    //        // var surfaceElement = new SurfaceLoadElement();
                    //        //element.ID = TriID;
                    //        //surfaceElement.ElementType = DirichletElement1;
                    //        //model.SubdomainsDictionary[0].Elements.Add(dirichletElement1);
                    //        //model.ElementsDictionary.Add(TriID, surfaceElement);

                    //        //model.NodesDictionary[surfaceElement.ID].Constraints.Add(new Constraint() { DOF = ThermalDof.Temperature, Amount = 100 });
                }
            }

            //boundaryIDs = new int[] { 1, 2, 3, 4 };
            //foreach (int boundaryID in boundaryIDs)
            //{
            //    foreach (Element element in modelReader.elementBoundaries[boundaryID])
            //    {
            //        IReadOnlyList<Node> nodes = (IReadOnlyList<Node>)element.Nodes;
            //        var fluxElement = fluxFactory.CreateElement(CellType.Quad4, nodes);
            //        model.SurfaceLoads.Add(fluxElement);
            //    }
            //}

            return(new Tuple <Model, ComsolMeshReader2>(model, modelReader));
        }
        private static Model CreateModel()
        {
            IList <IList <Node> >    nodeBoundaries;
            IList <IList <Element> > elementBoundaries;
            double density = 1.0;

            double[] U = { 2, 2, 2 };
            double   k = 1.0;
            double   L = .0;
            var      elementFactory3D  = new ConvectionDiffusionElement3DFactory(new ConvectionDiffusionMaterial(k, U, L));
            var      boundaryFactory3D = new SurfaceBoundaryFactory3D(0, new ConvectionDiffusionMaterial(k, U, L));
            var      model             = new Model();

            model.SubdomainsDictionary[0] = new Subdomain(0);
            elementBoundaries             = new List <IList <Element> >();
            nodeBoundaries = new List <IList <Node> >();
            for (int i = 0; i < 10; i++)
            {
                elementBoundaries.Add(new List <Element>());
                nodeBoundaries.Add(new List <Node>());
            }

            double[,] nodeData = new double[, ] {
                { 0, 0, 0 },
                { 0, 1, 0 },
                { 0, 1, 1 },
                { 0, 0, 1 },
                { 1, 0, 0 },
                { 1, 1, 0 },
                { 1, 1, 1 },
                { 1, 0, 1 }
            };

            for (int nNode = 0; nNode < nodeData.GetLength(0); nNode++)
            {
                model.NodesDictionary.Add(nNode, new Node(id: nNode, x: nodeData[nNode, 0], y: nodeData[nNode, 1], z: nodeData[nNode, 2]));
            }
            IReadOnlyList <Node> nodes = new List <Node>
            {
                model.NodesDictionary[0],
                model.NodesDictionary[1],
                model.NodesDictionary[2],
                model.NodesDictionary[3],
                model.NodesDictionary[4],
                model.NodesDictionary[5],
                model.NodesDictionary[6],
                model.NodesDictionary[7],
            };
            IList <IReadOnlyList <Node> > face = new List <IReadOnlyList <Node> >();

            //face = new List<IReadOnlyList<Node>>();
            for (int i = 0; i < 6; i++)
            {
                face.Add(new List <Node>());
            }

            face[0] = new List <Node>
            {
                model.NodesDictionary[0],
                model.NodesDictionary[1],
                model.NodesDictionary[2],
                model.NodesDictionary[3],
            };
            face[1] = new List <Node>
            {
                model.NodesDictionary[4],
                model.NodesDictionary[5],
                model.NodesDictionary[6],
                model.NodesDictionary[7],
            };
            face[2] = new List <Node>
            {
                model.NodesDictionary[0],
                model.NodesDictionary[1],
                model.NodesDictionary[5],
                model.NodesDictionary[4],
            };
            face[3] = new List <Node>
            {
                model.NodesDictionary[1],
                model.NodesDictionary[2],
                model.NodesDictionary[6],
                model.NodesDictionary[5],
            };
            face[4] = new List <Node>
            {
                model.NodesDictionary[2],
                model.NodesDictionary[6],
                model.NodesDictionary[7],
                model.NodesDictionary[3],
            };
            face[5] = new List <Node>
            {
                model.NodesDictionary[0],
                model.NodesDictionary[4],
                model.NodesDictionary[7],
                model.NodesDictionary[3],
            };
            int[] elementData = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };// the last line will not be used. We assign only one element

            var Hexa8   = elementFactory3D.CreateElement(CellType.Hexa8, nodes);
            var element = new Element();

            element.ID          = 0;
            element.ElementType = Hexa8;
            for (int j = 0; j < 8; j++)
            {
                element.NodesDictionary.Add(elementData[j], model.NodesDictionary[elementData[j]]);
            }
            model.SubdomainsDictionary[0].Elements.Add(element);
            model.ElementsDictionary.Add(0, element);

            var flux1 = new FluxLoad(1);
            var flux2 = new FluxLoad(10);
            var dir1  = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, 0));
            });
            var dir2 = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, 10));
            });
            var weakDirichlet1 = new WeakDirichlet(dir1, k);
            var weakDirichlet2 = new WeakDirichlet(dir2, k);

            var dirichletFactory1 = new SurfaceLoadElementFactory(weakDirichlet1);
            var dirichletFactory2 = new SurfaceLoadElementFactory(weakDirichlet2);
            var fluxFactory1      = new SurfaceLoadElementFactory(flux1);
            var fluxFactory2      = new SurfaceLoadElementFactory(flux2);

            //foreach (int i in new int[] { 1 })
            //{
            //    var Quad = boundaryFactory3D.CreateElement(CellType.Quad4, face[i - 1]);
            //    var faceElement = new Element();
            //    faceElement.ID = i;
            //    faceElement.ElementType = Quad;
            //    foreach (Node node in face[i - 1])
            //    {
            //        faceElement.AddNode(node);
            //    }
            //    model.SubdomainsDictionary[0].Elements.Add(faceElement);
            //    model.ElementsDictionary.Add(i, faceElement);

            //    var dirichletElement = dirichletFactory1.CreateElement(CellType.Quad4, face[i - 1]);
            //    //var fluxElement = fluxFactory.CreateElement(CellType.Quad4, face[i - 1]);

            //    model.SurfaceLoads.Add(dirichletElement);
            //    //model.SurfaceLoads.Add(fluxElement);
            //}

            foreach (int i in new int[] { 0 })
            {
                //var Quad = boundaryFactory3D.CreateElement(CellType.Quad4, face[i]);
                //var faceElement = new Element();
                //faceElement.ID = 1;
                //faceElement.ElementType = Quad;
                //foreach (Node node in face[i])
                //{
                //    faceElement.AddNode(node);
                //}
                //model.SubdomainsDictionary[0].Elements.Add(faceElement);
                //model.ElementsDictionary.Add(1, faceElement);

                //var dirichletElement = dirichletFactory2.CreateElement(CellType.Quad4, face[i]);
                //model.SurfaceLoads.Add(dirichletElement);

                var fluxElement = fluxFactory1.CreateElement(CellType.Quad4, face[i]);
                model.SurfaceLoads.Add(fluxElement);
            }

            foreach (int i in new int[] { 1 })
            {
                //var Quad = boundaryFactory3D.CreateElement(CellType.Quad4, face[i]);
                //var faceElement = new Element();
                //faceElement.ID = 2;
                //faceElement.ElementType = Quad;
                //foreach (Node node in face[i])
                //{
                //    faceElement.AddNode(node);
                //}
                //model.SubdomainsDictionary[0].Elements.Add(faceElement);
                //model.ElementsDictionary.Add(2, faceElement);

                //var dirichletElement = dirichletFactory2.CreateElement(CellType.Quad4, face[i]);
                //model.SurfaceLoads.Add(dirichletElement);

                var fluxElement = fluxFactory2.CreateElement(CellType.Quad4, face[i]);
                model.SurfaceLoads.Add(fluxElement);
            }

            //constraints
            //foreach (int i in new int[] { 4, 5, 6, 7 })
            //{
            //    model.NodesDictionary[i].Constraints.Add(new Constraint()
            //    {
            //        Amount = 0,
            //        DOF = ThermalDof.Temperature
            //    });
            //}

            //// loads
            //Load load1;
            //foreach (int i in new int[] {0, 1, 2, 3})
            //{
            //    load1 = new Load()
            //    {
            //        Node = model.NodesDictionary[i],
            //        DOF = ThermalDof.Temperature,
            //        Amount = 12.5
            //    };
            //    model.Loads.Add(load1);
            //}


            return(model);
        }
Example #7
0
 public WeakDirichlet(DirichletDistribution distribution, double diffusionCoeff)
 {
     _distribution   = distribution;
     _diffusionCoeff = diffusionCoeff;
 }
Example #8
0
        protected void DoLearning(LearningTask learningTask)
        {
            if (learningTask.LearningState == ComputationState.Done)
            {
                return;
            }

            // Grab options.
            var options = learningTask.Options;

            // Grab training set.
            var trainingSet = learningTask.TrainingSet;

            // Grab variable names.
            var firstObservation = learningTask.TrainingSet.First();
            var variableNames = firstObservation.VariableNames;

            // Build a starter Bayesian network.
            var bn = new BayesianNetwork(learningTask.TrainingSet.Name);

            // Build variables for each variable in the training set.
            foreach (var variableName in variableNames)
            {
                var space = trainingSet.Variables.TryFind(variableName).Value;
                var rv = new RandomVariable(variableName, space);
                bn.AddVariable(rv);
            }

            // Store in task so that task can send updates to its listeners.
            learningTask.BayesianNetwork = bn;
            learningTask.LearningState = ComputationState.Computing;

            // Build Dirichlet parameters
            IDictionary<string, DirichletDistribution> priors = new Dictionary<string, DirichletDistribution>();
            foreach (var variableName in variableNames)
            {
                var space = trainingSet.Variables.TryFind(variableName).Value;

                DirichletDistribution prior;
                if (options.DistributionDirichletAlpha > 0)
                {
                    prior = new DirichletDistribution();
                    foreach (var value in space.Values)
                    {
                        prior.SetParameter(value, options.DistributionDirichletAlpha);
                    }
                    priors[variableName] = prior;
                }
            }

            // Initialize a sufficient statistics.
            SufficientStatistics sufficientStatistics
                = new SufficientStatistics(trainingSet, Utils.Some(priors));

            // Learn structure.
            switch (options.Structure)
            {
                case LearningOptions.StructureEnum.DisconnectedStructure:
                    break;
                case LearningOptions.StructureEnum.RandomStructure:
                    bn.GenerateStructure(
                        StructureClass.Random,
                        Utils.Some(options.StructureSeed),
                        Utils.Some(options.StructureParentLimit));
                    break;
                case LearningOptions.StructureEnum.TreeStructure:
                    bn.LearnStructure(
                        sufficientStatistics,
                        StructureClass.Tree,
                        Utils.None<int>(),
                        Utils.None<int>());
                    break;
                case LearningOptions.StructureEnum.GeneralStructure:
                    bn.LearnStructure(
                        sufficientStatistics,
                        StructureClass.General,
                        Utils.None<int>(),
                        Utils.Some(options.StructureParentLimit));
                    break;
            }

            // Learn distributions.
            bn.LearnDistributions(sufficientStatistics);

            // Done.
            learningTask.LearningState = ComputationState.Done;
        }
Example #9
0
        private static Tuple <Model, ComsolMeshReader2> CreateModel(double k, double[] U, double L)
        {
            string            filename    = Path.Combine(Directory.GetCurrentDirectory(), "InputFiles", "TumorGrowthModel", "fullModel.mphtxt");
            ComsolMeshReader2 modelReader = new ComsolMeshReader2(filename, new double[] { k }, new double[][] { U }, new double[] { L });
            Model             model       = modelReader.CreateModelFromFile();
            //Boundary Conditions
            var bodyLoad = new ConvectionDiffusionDomainLoad(new ConvectionDiffusionMaterial(k, U, L), 1, ThermalDof.Temperature);
            var flux1    = new FluxLoad(1);
            var flux2    = new FluxLoad(10);
            var dir1     = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, 1));
            });
            var dir2 = new DirichletDistribution(list => {
                return(Vector.CreateWithValue(list.Count, 0));
            });
            var weakDirichlet1 = new WeakDirichlet(dir1, k);
            var weakDirichlet2 = new WeakDirichlet(dir2, k);

            var bodyLoadElementFactory = new BodyLoadElementFactory(bodyLoad, model);
            var dirichletFactory1      = new SurfaceLoadElementFactory(weakDirichlet1);
            var dirichletFactory2      = new SurfaceLoadElementFactory(weakDirichlet2);
            var fluxFactory1           = new SurfaceLoadElementFactory(flux1);
            var fluxFactory2           = new SurfaceLoadElementFactory(flux2);
            var boundaryFactory3D      = new SurfaceBoundaryFactory3D(0,
                                                                      new ConvectionDiffusionMaterial(k, new double[] { 0, 0, 0 }, 0));

            int[] domainIDs = new int[] { 0, };
            foreach (int domainID in domainIDs)
            {
                foreach (Element element in modelReader.elementDomains[domainID])
                {
                    //IReadOnlyList<Node> nodes = (IReadOnlyList<Node>)element.Nodes;
                    //var fluxElement1 = fluxFactory1.CreateElement(CellType.Quad4, nodes);
                    //model.SurfaceLoads.Add(fluxElement1);
                    //var bodyLoadElementCellType = element.ElementType.CellType;
                    //var nodes = (IReadOnlyList<Node>)element.Nodes;
                    //var bodyLoadElement = bodyLoadElementFactory.CreateElement(CellType.Hexa8, nodes);
                    //model.BodyLoads.Add(bodyLoadElement);
                    // var surfaceElement = new SurfaceLoadElement();
                    //element.ID = TriID;
                    //surfaceElement.ElementType = DirichletElement1;
                    //model.SubdomainsDictionary[0].Elements.Add(dirichletElement1);
                    //model.ElementsDictionary.Add(TriID, surfaceElement);

                    //model.NodesDictionary[surfaceElement.ID].Constraints.Add(new Constraint() { DOF = ThermalDof.Temperature, Amount = 100 });
                }
            }

            //foreach (Node node in model.Nodes)
            //{
            //    model.Loads.Add(new Load() { Amount = .25, Node = node, DOF = ThermalDof.Temperature });
            //}

            int[] boundaryIDs = new int[] { 0, 1, 2, 3, 4, 7 };
            int   TriID       = model.ElementsDictionary.Count + 1;

            foreach (int boundaryID in boundaryIDs)
            {
                foreach (IReadOnlyList <Node> nodes in modelReader.triBoundaries[boundaryID])
                {
                    //IReadOnlyList<Node> nodes = (IReadOnlyList<Node>)element.Nodes;
                    //var fluxElement1 = fluxFactory1.CreateElement(CellType.Quad4, nodes);
                    //model.SurfaceLoads.Add(fluxElement1);
                    var dirichletElement1 = dirichletFactory1.CreateElement(CellType.Tri3, nodes);
                    model.SurfaceLoads.Add(dirichletElement1);
                    var surfaceBoundaryElement = boundaryFactory3D.CreateElement(CellType.Tri3, nodes);
                    var element = new Element();
                    element.ID          = TriID;
                    element.ElementType = surfaceBoundaryElement;
                    model.SubdomainsDictionary[0].Elements.Add(element);
                    model.ElementsDictionary.Add(TriID, element);
                    foreach (Node node in nodes)
                    {
                        element.AddNode(node);
                    }
                    TriID += 1;
                    // var surfaceElement = new SurfaceLoadElement();
                    //element.ID = TriID;
                    //surfaceElement.ElementType = DirichletElement1;
                    //model.SubdomainsDictionary[0].Elements.Add(dirichletElement1);
                    //model.ElementsDictionary.Add(TriID, surfaceElement);

                    //model.NodesDictionary[surfaceElement.ID].Constraints.Add(new Constraint() { DOF = ThermalDof.Temperature, Amount = 100 });
                }
            }
            boundaryIDs = new int[] { 6, 8, 9 };
            foreach (int boundaryID in boundaryIDs)
            {
                foreach (IReadOnlyList <Node> nodes in modelReader.triBoundaries[boundaryID])
                {
                    //IReadOnlyList<Node> nodes = (IReadOnlyList<Node>)element.Nodes;
                    //var fluxElement2 = fluxFactory2.CreateElement(CellType.Quad4, nodes);
                    //model.SurfaceLoads.Add(fluxElement2);
                    var dirichletElement2 = dirichletFactory2.CreateElement(CellType.Tri3, nodes);
                    model.SurfaceLoads.Add(dirichletElement2);
                    var surfaceBoundaryElement = boundaryFactory3D.CreateElement(CellType.Tri3, nodes);
                    var element = new Element();
                    element.ID          = TriID;
                    element.ElementType = surfaceBoundaryElement;
                    model.SubdomainsDictionary[0].Elements.Add(element);
                    model.ElementsDictionary.Add(TriID, element);
                    foreach (Node node in nodes)
                    {
                        element.AddNode(node);
                    }
                    TriID += 1;
                    // var surfaceElement = new SurfaceLoadElement();
                    //element.ID = TriID;
                    //surfaceElement.ElementType = DirichletElement1;
                    //model.SubdomainsDictionary[0].Elements.Add(dirichletElement1);
                    //model.ElementsDictionary.Add(TriID, surfaceElement);

                    //model.NodesDictionary[surfaceElement.ID].Constraints.Add(new Constraint() { DOF = ThermalDof.Temperature, Amount = 100 });
                }
            }

            //boundaryIDs = new int[] { 1, 2, 3, 4 };
            //foreach (int boundaryID in boundaryIDs)
            //{
            //    foreach (Element element in modelReader.elementBoundaries[boundaryID])
            //    {
            //        IReadOnlyList<Node> nodes = (IReadOnlyList<Node>)element.Nodes;
            //        var fluxElement = fluxFactory.CreateElement(CellType.Quad4, nodes);
            //        model.SurfaceLoads.Add(fluxElement);
            //    }
            //}

            return(new Tuple <Model, ComsolMeshReader2>(model, modelReader));
        }
Example #10
0
        protected void DoLearning(LearningTask learningTask)
        {
            if (learningTask.LearningState == ComputationState.Done)
            {
                return;
            }

            // Grab options.
            var options = learningTask.Options;

            // Grab training set.
            var trainingSet = learningTask.TrainingSet;

            // Grab variable names.
            var firstObservation = learningTask.TrainingSet.First();
            var variableNames    = firstObservation.VariableNames;

            // Build a starter Bayesian network.
            var bn = new BayesianNetwork(learningTask.TrainingSet.Name);

            // Build variables for each variable in the training set.
            foreach (var variableName in variableNames)
            {
                var space = trainingSet.Variables.TryFind(variableName).Value;
                var rv    = new RandomVariable(variableName, space);
                bn.AddVariable(rv);
            }

            // Store in task so that task can send updates to its listeners.
            learningTask.BayesianNetwork = bn;
            learningTask.LearningState   = ComputationState.Computing;

            // Build Dirichlet parameters
            IDictionary <string, DirichletDistribution> priors = new Dictionary <string, DirichletDistribution>();

            foreach (var variableName in variableNames)
            {
                var space = trainingSet.Variables.TryFind(variableName).Value;

                DirichletDistribution prior;
                if (options.DistributionDirichletAlpha > 0)
                {
                    prior = new DirichletDistribution();
                    foreach (var value in space.Values)
                    {
                        prior.SetParameter(value, options.DistributionDirichletAlpha);
                    }
                    priors[variableName] = prior;
                }
            }

            // Initialize a sufficient statistics.
            SufficientStatistics sufficientStatistics
                = new SufficientStatistics(trainingSet, Utils.Some(priors));

            // Learn structure.
            switch (options.Structure)
            {
            case LearningOptions.StructureEnum.DisconnectedStructure:
                break;

            case LearningOptions.StructureEnum.RandomStructure:
                bn.GenerateStructure(
                    StructureClass.Random,
                    Utils.Some(options.StructureSeed),
                    Utils.Some(options.StructureParentLimit));
                break;

            case LearningOptions.StructureEnum.TreeStructure:
                bn.LearnStructure(
                    sufficientStatistics,
                    StructureClass.Tree,
                    Utils.None <int>(),
                    Utils.None <int>());
                break;

            case LearningOptions.StructureEnum.GeneralStructure:
                bn.LearnStructure(
                    sufficientStatistics,
                    StructureClass.General,
                    Utils.None <int>(),
                    Utils.Some(options.StructureParentLimit));
                break;
            }

            // Learn distributions.
            bn.LearnDistributions(sufficientStatistics);

            // Done.
            learningTask.LearningState = ComputationState.Done;
        }