Skip to content
Snippets Groups Projects
HDGHelmholtz3DHomo1D.cpp 7.96 KiB
Newer Older
///////////////////////////////////////////////////////////////////////////////
//
// File: HDGHelmholtz3DHomo1D.cpp
//
// 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).
//
// 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:
//
///////////////////////////////////////////////////////////////////////////////
Spencer Sherwin's avatar
.
Spencer Sherwin committed

#include <cstdio>
#include <cstdlib>

#include <LibUtilities/BasicUtils/SessionReader.h>
#include <LibUtilities/Communication/Comm.h>
#include <LibUtilities/Memory/NekMemoryManager.hpp>
Spencer Sherwin's avatar
.
Spencer Sherwin committed
#include <MultiRegions/DisContField3DHomogeneous1D.h>
#include <SpatialDomains/MeshGraphIO.h>
Spencer Sherwin's avatar
.
Spencer Sherwin committed

Spencer Sherwin's avatar
.
Spencer Sherwin committed
#ifdef TIMING
#include <time.h>
#define Timing(s)                                                              \
    fprintf(stdout, "%s Took %g seconds\n", s,                                 \
            (clock() - st) / (double)CLOCKS_PER_SEC);                          \
    st = clock();
Spencer Sherwin's avatar
.
Spencer Sherwin committed
#else
#define Timing(s) /* Nothing */
Spencer Sherwin's avatar
.
Spencer Sherwin committed
#endif

using namespace std;
Spencer Sherwin's avatar
.
Spencer Sherwin committed
using namespace Nektar;

int main(int argc, char *argv[])
{
    LibUtilities::SessionReaderSharedPtr vSession =
        LibUtilities::SessionReader::CreateInstance(argc, argv);
    LibUtilities::CommSharedPtr vComm = vSession->GetComm();
    string meshfile(argv[1]);
    MultiRegions::DisContField3DHomogeneous1DSharedPtr Exp, Fce;
    MultiRegions::ExpListSharedPtr DerExp1, DerExp2, DerExp3;
    int i, nq;
    Array<OneD, NekDouble> fce;
    Array<OneD, NekDouble> xc0, xc1, xc2;
    StdRegions::ConstFactorMap factors;
Spencer Sherwin's avatar
.
Spencer Sherwin committed

    if (argc < 2)
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    {
        fprintf(stderr, "Usage: Helmholtz2D  meshfile\n");
Spencer Sherwin's avatar
.
Spencer Sherwin committed
        exit(1);
    }

    LibUtilities::FieldIOSharedPtr fld =
        LibUtilities::FieldIO::CreateDefault(vSession);
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    //----------------------------------------------
    // Read in mesh from input file
    SpatialDomains::MeshGraphSharedPtr graph2D =
        SpatialDomains::MeshGraphIO::Read(vSession);
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    //----------------------------------------------

    //----------------------------------------------
    // Define Expansion
    int nplanes = vSession->GetParameter("HomModesZ");
    lz          = vSession->GetParameter("LZ");
    bool useFFT = false;
    bool deal   = false;
    const LibUtilities::PointsKey Pkey(nplanes,
                                       LibUtilities::eFourierEvenlySpaced);
    const LibUtilities::BasisKey Bkey(LibUtilities::eFourier, nplanes, Pkey);
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    Exp = MemoryManager<MultiRegions::DisContField3DHomogeneous1D>::
        AllocateSharedPtr(vSession, Bkey, lz, useFFT, deal, graph2D,
                          vSession->GetVariable(0));
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    //----------------------------------------------
    Timing("Read files and define exp ..");

    //----------------------------------------------
    // Print summary of solution details
    factors[StdRegions::eFactorLambda] = vSession->GetParameter("Lambda");
    factors[StdRegions::eFactorTau]    = 1.0;

    const SpatialDomains::ExpansionInfoMap &expansions =
        graph2D->GetExpansionInfo();
    LibUtilities::BasisKey bkey0 =
        expansions.begin()->second->m_basisKeyVector[0];
    cout << "Solving 3D Helmholtz (Homogeneous in z-direction):" << endl;
    cout << "         Lambda         : " << factors[StdRegions::eFactorLambda]
         << endl;
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    cout << "         Lz             : " << lz << endl;
    cout << "         No. modes      : " << bkey0.GetNumModes() << endl;
    cout << "         No. hom. modes : " << Bkey.GetNumModes() << endl;
    cout << endl;
    //----------------------------------------------

    //----------------------------------------------
    // Set up coordinates of mesh for Forcing function evaluation
    nq  = Exp->GetTotPoints();
    xc0 = Array<OneD, NekDouble>(nq, 0.0);
    xc1 = Array<OneD, NekDouble>(nq, 0.0);
    xc2 = Array<OneD, NekDouble>(nq, 0.0);
Spencer Sherwin's avatar
.
Spencer Sherwin committed

    Exp->GetCoords(xc0, xc1, xc2);
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    //----------------------------------------------

    //----------------------------------------------
    // Define forcing function for first variable defined in file
    fce                                   = Array<OneD, NekDouble>(nq);
    LibUtilities::EquationSharedPtr ffunc = vSession->GetFunction("Forcing", 0);
    ffunc->Evaluate(xc0, xc1, xc2, fce);
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    //----------------------------------------------

    //----------------------------------------------
    // Setup expansion containing the  forcing function
    Fce = MemoryManager<
        MultiRegions::DisContField3DHomogeneous1D>::AllocateSharedPtr(*Exp);
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    Fce->SetPhys(fce);
    //----------------------------------------------
    Timing("Define forcing ..");
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    //----------------------------------------------
    // Helmholtz solution taking physical forcing
    Exp->HelmSolve(Fce->GetPhys(), Exp->UpdateCoeffs(), factors);
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    //----------------------------------------------

    Timing("Helmholtz Solve ..");

#ifdef TIMING
    for (i = 0; i < 100; ++i)
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    {
        Exp->HelmSolve(Fce->GetPhys(), Exp->UpdateCoeffs(), NullFlagList,
                       factors);
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    }

    Timing("100 Helmholtz Solves:... ");
#endif

    //-----------------------------------------------
    // Backward Transform Solution to get solved values at
    Exp->BwdTrans(Exp->GetCoeffs(), Exp->UpdatePhys());
    //-----------------------------------------------
    Timing("Backard Transform ..");

    //-----------------------------------------------
    // Write solution to file
    string out = meshfile.substr(0, meshfile.find_last_of(".")) + ".fld";
    std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
        Exp->GetFieldDefinitions();
    std::vector<std::vector<NekDouble>> FieldData(FieldDef.size());
Spencer Sherwin's avatar
.
Spencer Sherwin committed

    for (i = 0; i < FieldDef.size(); ++i)
Spencer Sherwin's avatar
.
Spencer Sherwin committed
    {
        FieldDef[i]->m_fields.push_back("u");
Spencer Sherwin's avatar
.
Spencer Sherwin committed
        Exp->AppendFieldData(FieldDef[i], FieldData[i]);
    }
    fld->Write(out, FieldDef, FieldData);
Spencer Sherwin's avatar
.
Spencer Sherwin committed

    //-----------------------------------------------

    //-----------------------------------------------
    // See if there is an exact solution, if so
    // evaluate and plot errors
    LibUtilities::EquationSharedPtr ex_sol =
        vSession->GetFunction("ExactSolution", 0);
Spencer Sherwin's avatar
.
Spencer Sherwin committed

Spencer Sherwin's avatar
.
Spencer Sherwin committed
    {
        //----------------------------------------------
        // evaluate exact solution
        ex_sol->Evaluate(xc0, xc1, xc2, fce);
Spencer Sherwin's avatar
.
Spencer Sherwin committed
        //----------------------------------------------

        //--------------------------------------------
        // Calculate error
        Fce->SetPhys(fce);
        Fce->SetPhysState(true);

        cout << "L infinity error:  "
             << Exp->Linf(Exp->GetPhys(), Fce->GetPhys()) << endl;
        cout << "L 2 error  :       " << Exp->L2(Exp->GetPhys(), Fce->GetPhys())
             << endl;
Spencer Sherwin's avatar
.
Spencer Sherwin committed
        //--------------------------------------------
    }

    Timing("Output ..");
    //----------------------------------------------
    return 0;
}