NodeOpti.h 5.21 KB
Newer Older
Michael Turner's avatar
Michael Turner committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
////////////////////////////////////////////////////////////////////////////////
//
//  File: ProcessJac.h
//
//  For more information, please see: http://www.nektar.info/
//
//  The MIT License
//
//  Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
//  Department of Aeronautics, Imperial College London (UK), and Scientific
//  Computing and Imaging Institute, University of Utah (USA).
//
//  License for the specific language governing rights and limitations under
//  Permission is hereby granted, free of charge, to any person obtaining a
//  copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense,
//  and/or sell copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included
//  in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//  DEALINGS IN THE SOFTWARE.
//
//  Description: Calculate jacobians of elements.
//
////////////////////////////////////////////////////////////////////////////////

#ifndef UTILITIES_NEKMESH_NODEOPTI
#define UTILITIES_NEKMESH_NODEOPTI

39 40
#include <ostream>

41 42
#include <LibUtilities/BasicUtils/Thread.h>

Michael Turner's avatar
Michael Turner committed
43 44
#include "ProcessVarOpti.h"

Michael Turner's avatar
Michael Turner committed
45 46 47 48 49 50 51 52 53 54
namespace Nektar
{
namespace Utilities
{

class NodeOptiJob;

class NodeOpti
{
public:
Dave Moxey's avatar
Dave Moxey committed
55
    NodeOpti(NodeSharedPtr n,
Michael Turner's avatar
Michael Turner committed
56
             std::vector<ElUtilSharedPtr> e,
57
             ResidualSharedPtr r, std::map<LibUtilities::ShapeType,DerivUtilSharedPtr> d,
58
             optiType o)
Michael Turner's avatar
Michael Turner committed
59
        : m_node(n), m_res(r), m_derivUtils(d), m_opti(o)
Michael Turner's avatar
Michael Turner committed
60
    {
61
        //filter element types within d vector
Michael Turner's avatar
Michael Turner committed
62
        for(int i = 0; i < e.size(); i++)
63
        {
Michael Turner's avatar
Michael Turner committed
64
            m_data[e[i]->GetEl()->GetShapeType()].push_back(e[i]);
65
        }
Michael Turner's avatar
Michael Turner committed
66 67 68 69
    }

    virtual ~NodeOpti(){};

70 71
    void CalcMinJac();

72
    virtual void Optimise() = 0;
Michael Turner's avatar
Michael Turner committed
73
    NodeOptiJob *GetJob();
74

75
    template<int DIM> NekDouble GetFunctional(NekDouble &minJacNew,
76
                                              bool gradient = true);
Michael Turner's avatar
Michael Turner committed
77 78 79

    template<int DIM> void MinEigen(NekDouble &val);
    
Dave Moxey's avatar
Dave Moxey committed
80 81
protected:

Michael Turner's avatar
Michael Turner committed
82
    NodeSharedPtr m_node;
83
    boost::mutex mtx;
Michael Turner's avatar
Michael Turner committed
84 85
    std::map<LibUtilities::ShapeType,std::vector<ElUtilSharedPtr> > m_data;
    Array<OneD, NekDouble> m_grad;
86

87
    template<int DIM> int IsIndefinite();
Michael Turner's avatar
Michael Turner committed
88

Michael Turner's avatar
Michael Turner committed
89

Michael Turner's avatar
Michael Turner committed
90 91 92 93
    NekDouble m_minJac;
    ResidualSharedPtr m_res;
    std::map<LibUtilities::ShapeType,DerivUtilSharedPtr> m_derivUtils;
    optiType m_opti;
94

Michael Turner's avatar
Michael Turner committed
95
    static const NekDouble gam;
96

Michael Turner's avatar
Michael Turner committed
97
    static NekDouble c1() {return 1e-3;}
Michael Turner's avatar
Michael Turner committed
98 99
    static NekDouble gradTol() {return 1e-8;}
    static NekDouble alphaTol() {return 1e-8;}
Michael Turner's avatar
Michael Turner committed
100 101
};

102 103 104 105
typedef boost::shared_ptr<NodeOpti> NodeOptiSharedPtr;
typedef LibUtilities::NekFactory<int,
                                 NodeOpti,
                                 NodeSharedPtr,
Michael Turner's avatar
Michael Turner committed
106
                                 std::vector<ElUtilSharedPtr>,
107
                                 ResidualSharedPtr,
108
                                 std::map<LibUtilities::ShapeType,DerivUtilSharedPtr>,
109
                                 optiType> NodeOptiFactory;
Michael Turner's avatar
Michael Turner committed
110

111
NodeOptiFactory &GetNodeOptiFactory();
Michael Turner's avatar
Michael Turner committed
112 113 114 115 116


class NodeOpti3D3D : public NodeOpti //1D optimsation in 3D space
{
public:
Dave Moxey's avatar
Dave Moxey committed
117
    NodeOpti3D3D(NodeSharedPtr n,
Michael Turner's avatar
Michael Turner committed
118
                 std::vector<ElUtilSharedPtr> e,
119
                 ResidualSharedPtr r, std::map<LibUtilities::ShapeType,DerivUtilSharedPtr> d,
120
                 optiType o)
Michael Turner's avatar
Michael Turner committed
121
                 : NodeOpti(n,e,r,d,o)
Michael Turner's avatar
Michael Turner committed
122 123 124 125 126 127 128
    {
    }

    ~NodeOpti3D3D(){};

    void Optimise();

129 130
    static int m_type;
    static NodeOptiSharedPtr create(
Dave Moxey's avatar
Dave Moxey committed
131
        NodeSharedPtr n,
Michael Turner's avatar
Michael Turner committed
132
        std::vector<ElUtilSharedPtr> e,
133
        ResidualSharedPtr r, std::map<LibUtilities::ShapeType,DerivUtilSharedPtr> d,
134
        optiType o)
135 136 137 138
    {
        return NodeOptiSharedPtr(new NodeOpti3D3D(n, e, r, d, o));
    }

Michael Turner's avatar
Michael Turner committed
139
private:
140

Michael Turner's avatar
Michael Turner committed
141 142 143 144 145
};

class NodeOpti2D2D : public NodeOpti //1D optimsation in 3D space
{
public:
Dave Moxey's avatar
Dave Moxey committed
146
    NodeOpti2D2D(NodeSharedPtr n,
Michael Turner's avatar
Michael Turner committed
147
                 std::vector<ElUtilSharedPtr> e,
148
                 ResidualSharedPtr r, std::map<LibUtilities::ShapeType,DerivUtilSharedPtr> d,
149
                 optiType o)
Michael Turner's avatar
Michael Turner committed
150
                 : NodeOpti(n,e,r,d,o)
Michael Turner's avatar
Michael Turner committed
151 152 153 154 155 156 157
    {
    }

    ~NodeOpti2D2D(){};

    void Optimise();

158 159
    static int m_type;
    static NodeOptiSharedPtr create(
Michael Turner's avatar
Michael Turner committed
160
        NodeSharedPtr n, std::vector<ElUtilSharedPtr> e,
161
        ResidualSharedPtr r, std::map<LibUtilities::ShapeType,DerivUtilSharedPtr> d,
162
        optiType o)
163 164 165 166
    {
        return NodeOptiSharedPtr(new NodeOpti2D2D(n, e, r, d, o));
    }

Michael Turner's avatar
Michael Turner committed
167
private:
168

Michael Turner's avatar
Michael Turner committed
169 170
};

171 172 173 174 175 176 177 178 179 180 181 182 183
class NodeOptiJob : public Thread::ThreadJob
{
public:
    NodeOptiJob(NodeOpti* no) : node(no) {}

    void Run()
    {
        node->Optimise();
    }
private:
    NodeOpti* node;
};

Michael Turner's avatar
Michael Turner committed
184 185 186 187
}
}

#endif