xref: /honee/src/inverse_multiplicity.c (revision 5930f0378a44a8268e0796ee7d6e90436bc16695)
1*5930f037SJames Wright // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2*5930f037SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3*5930f037SJames Wright //
4*5930f037SJames Wright // SPDX-License-Identifier: BSD-2-Clause
5*5930f037SJames Wright //
6*5930f037SJames Wright // This file is part of CEED:  http://github.com/ceed
7*5930f037SJames Wright 
8*5930f037SJames Wright #include "../qfunctions/inverse_multiplicity.h"
9*5930f037SJames Wright #include "../navierstokes.h"
10*5930f037SJames Wright 
11*5930f037SJames Wright /**
12*5930f037SJames Wright  * @brief Get the inverse of the node multiplicity
13*5930f037SJames Wright  *
14*5930f037SJames Wright  * Local multiplicity concerns only the number of elements associated with a node in the current rank's partition.
15*5930f037SJames Wright  * Global multiplicity is the multiplicity for the global mesh, including periodicity.
16*5930f037SJames Wright  *
17*5930f037SJames Wright  * @param[in]  ceed                        `Ceed` object for the output `CeedVector` and `CeedElemRestriction`
18*5930f037SJames Wright  * @param[in]  dm                          `DM` for the grid
19*5930f037SJames Wright  * @param[in]  domain_label                `DMLabel` for `DMPlex` domain
20*5930f037SJames Wright  * @param[in]  label_value                 Stratum value
21*5930f037SJames Wright  * @param[in]  height                      Height of `DMPlex` topology
22*5930f037SJames Wright  * @param[in]  dm_field                    Index of `DMPlex` field
23*5930f037SJames Wright  * @param[in]  get_global_multiplicity     Whether the multiplicity should be global or local
24*5930f037SJames Wright  * @param[out] elem_restr_inv_multiplicity `CeedElemRestriction` needed to use the multiplicity vector
25*5930f037SJames Wright  * @param[out] inv_multiplicity            `CeedVector` containing the inverse of the multiplicity
26*5930f037SJames Wright  */
27*5930f037SJames Wright PetscErrorCode GetInverseMultiplicity(Ceed ceed, DM dm, DMLabel domain_label, PetscInt label_value, PetscInt height, PetscInt dm_field,
28*5930f037SJames Wright                                       PetscBool get_global_multiplicity, CeedElemRestriction *elem_restr_inv_multiplicity,
29*5930f037SJames Wright                                       CeedVector *inv_multiplicity) {
30*5930f037SJames Wright   Vec                 Multiplicity, Multiplicity_loc;
31*5930f037SJames Wright   PetscMemType        m_mem_type;
32*5930f037SJames Wright   CeedVector          multiplicity;
33*5930f037SJames Wright   CeedQFunction       qf_multiplicity;
34*5930f037SJames Wright   CeedOperator        op_multiplicity;
35*5930f037SJames Wright   CeedInt             num_comp;
36*5930f037SJames Wright   CeedElemRestriction elem_restr;
37*5930f037SJames Wright 
38*5930f037SJames Wright   PetscFunctionBeginUser;
39*5930f037SJames Wright 
40*5930f037SJames Wright   PetscCall(DMPlexCeedElemRestrictionCollocatedCreate(ceed, dm, domain_label, label_value, height, 1, elem_restr_inv_multiplicity));
41*5930f037SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(*elem_restr_inv_multiplicity, inv_multiplicity, NULL));
42*5930f037SJames Wright 
43*5930f037SJames Wright   PetscCall(DMPlexCeedElemRestrictionCreate(ceed, dm, domain_label, label_value, height, dm_field, &elem_restr));
44*5930f037SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr, &num_comp));
45*5930f037SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr, &multiplicity, NULL));
46*5930f037SJames Wright 
47*5930f037SJames Wright   if (get_global_multiplicity) {
48*5930f037SJames Wright     // In order to get global multiplicity, we need to run through DMLocalToGlobal -> DMGlobalToLocal.
49*5930f037SJames Wright     PetscCall(DMGetLocalVector(dm, &Multiplicity_loc));
50*5930f037SJames Wright     PetscCall(DMGetGlobalVector(dm, &Multiplicity));
51*5930f037SJames Wright 
52*5930f037SJames Wright     PetscCall(VecPetscToCeed(Multiplicity_loc, &m_mem_type, multiplicity));
53*5930f037SJames Wright     PetscCallCeed(ceed, CeedElemRestrictionGetMultiplicity(elem_restr, multiplicity));
54*5930f037SJames Wright     PetscCall(VecCeedToPetsc(multiplicity, m_mem_type, Multiplicity_loc));
55*5930f037SJames Wright     PetscCall(VecZeroEntries(Multiplicity));
56*5930f037SJames Wright     PetscCall(DMLocalToGlobal(dm, Multiplicity_loc, ADD_VALUES, Multiplicity));
57*5930f037SJames Wright     PetscCall(DMGlobalToLocal(dm, Multiplicity, INSERT_VALUES, Multiplicity_loc));
58*5930f037SJames Wright     PetscCall(VecPetscToCeed(Multiplicity_loc, &m_mem_type, multiplicity));
59*5930f037SJames Wright   } else {
60*5930f037SJames Wright     PetscCallCeed(ceed, CeedElemRestrictionGetMultiplicity(elem_restr, multiplicity));
61*5930f037SJames Wright   }
62*5930f037SJames Wright 
63*5930f037SJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, InverseMultiplicity, InverseMultiplicity_loc, &qf_multiplicity));
64*5930f037SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_multiplicity, "multiplicity", num_comp, CEED_EVAL_NONE));
65*5930f037SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_multiplicity, "inverse multiplicity", 1, CEED_EVAL_NONE));
66*5930f037SJames Wright 
67*5930f037SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_multiplicity, NULL, NULL, &op_multiplicity));
68*5930f037SJames Wright   PetscCallCeed(ceed, CeedOperatorSetName(op_multiplicity, "InverseMultiplicity"));
69*5930f037SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_multiplicity, "multiplicity", elem_restr, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
70*5930f037SJames Wright   PetscCallCeed(ceed,
71*5930f037SJames Wright                 CeedOperatorSetField(op_multiplicity, "inverse multiplicity", *elem_restr_inv_multiplicity, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
72*5930f037SJames Wright 
73*5930f037SJames Wright   PetscCallCeed(ceed, CeedOperatorApply(op_multiplicity, multiplicity, *inv_multiplicity, CEED_REQUEST_IMMEDIATE));
74*5930f037SJames Wright 
75*5930f037SJames Wright   if (get_global_multiplicity) {
76*5930f037SJames Wright     PetscCall(VecCeedToPetsc(multiplicity, m_mem_type, Multiplicity_loc));
77*5930f037SJames Wright     PetscCall(DMRestoreLocalVector(dm, &Multiplicity_loc));
78*5930f037SJames Wright     PetscCall(DMRestoreGlobalVector(dm, &Multiplicity));
79*5930f037SJames Wright   }
80*5930f037SJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&multiplicity));
81*5930f037SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr));
82*5930f037SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_multiplicity));
83*5930f037SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_multiplicity));
84*5930f037SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
85*5930f037SJames Wright }
86