Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #pragma once 11 : 12 : #include "MultiAppGeneralFieldTransfer.h" 13 : #include "KDTree.h" 14 : #include "SolutionInvalidInterface.h" 15 : 16 : /** 17 : * Base class for working with KDTrees in transfers, whether for interpolation or extrapolation 18 : */ 19 : class MultiAppGeneralFieldKDTreeTransferBase : public MultiAppGeneralFieldTransfer 20 : { 21 : public: 22 : static InputParameters validParams(); 23 : 24 : MultiAppGeneralFieldKDTreeTransferBase(const InputParameters & parameters); 25 : 26 : void initialSetup() override; 27 : 28 : // Use solution invalid output for these warnings 29 : usingCombinedWarningSolutionWarnings; 30 : 31 : protected: 32 : virtual void prepareEvaluationOfInterpValues(const unsigned int var_index) override; 33 : 34 : using MultiAppGeneralFieldTransfer::inBlocks; 35 : bool inBlocks(const std::set<SubdomainID> & blocks, 36 : const MooseMesh & mesh, 37 : const Elem * elem) const override; 38 : 39 322 : bool usesMooseAppCoordTransform() const override { return true; } 40 : /* 41 : * Build KD-Trees for each local app 42 : * @param var_index the index of the variable being transferred 43 : * @details fills _local_kdtrees, _local_points and _local_values 44 : * Indexing is: local apps (outer-indexing) OR positions (if using nearest_positions), 45 : * local nodes (inner-indexing) 46 : */ 47 : virtual void buildKDTrees(const unsigned int var_index) = 0; 48 : 49 : /// Pre-compute the number of sources 50 : /// Number of KDTrees used to hold the locations and variable value data 51 : void computeNumSources(); 52 : 53 : /** 54 : * Get the index of the app when inside of a KD-Tree source loop, where multiple applications 55 : * could be lumped (grouped) inside the same KD-Tree 56 : * @param kdtree_index index of the kd-tree / source 57 : * @param app_index_in_tree index of the application within the multiple apps contributing values 58 : * to a KD-tree. This is a local index 59 : */ 60 : unsigned int getAppIndex(unsigned int kdtree_index, unsigned int app_index_in_tree) const; 61 : 62 : /// Number of applications which contributed nearest-locations to each KD-tree 63 : unsigned int getNumAppsPerTree() const; 64 : 65 : /// Number of divisions (nearest-positions or source mesh divisions) used when building KD-Trees 66 : unsigned int getNumDivisions() const; 67 : 68 : /** 69 : * Transform a point into the frame used for KD-tree queries. 70 : * When grouping subapps with nearest-positions, KD-trees are built in the global frame so this 71 : * returns pt unchanged. Otherwise delegates to getPointInSourceAppFrame. 72 : * @param i_source index of the KD-tree source (may be a position index when grouping subapps) 73 : * @param pt the point in the global frame 74 : */ 75 : Point getPointInSourceKDTreeFrame(unsigned int i_source, const Point & pt) const; 76 : 77 : /** 78 : * @brief Examine all spatial restrictions that could preclude this source from being 79 : * a valid source for this point 80 : * @param pt point of interest 81 : * @param valid_mesh_div if using source mesh divisions in a 'matching' mode, the point can only 82 : * be used if coming from the relevant match 83 : * @param i_from index of the source (= a KDTree+values) of interest. Local index, goes from 0 to 84 : * the number of sources - 1. 85 : */ 86 : bool checkRestrictionsForSource(const Point & pt, 87 : const unsigned int valid_mesh_div, 88 : const unsigned int i_from) const; 89 : 90 : /** 91 : * @brief Search all local KD-trees for the nearest node/element and update outgoing_val. 92 : * 93 : * Performs a first pass to find the nearest neighbor value across all sources, then a second 94 : * pass for search-value-conflict detection when _search_value_conflicts is true. 95 : * 96 : * @param pt Target point in the reference coordinate frame 97 : * @param source_index Mesh-division / sub-app index encoded in the point request 98 : * @param outgoing_val Accumulator {value, distance}; caller must pre-set .second to max() 99 : * @param point_found Set to true when at least one non-empty KD-tree contributes a value 100 : */ 101 : void evaluateNearestNodeFromKDTrees(const Point & pt, 102 : unsigned int source_index, 103 : std::pair<Real, Real> & outgoing_val, 104 : bool & point_found); 105 : 106 : /// KD-Trees for all the local source apps 107 : std::vector<std::shared_ptr<KDTree>> _local_kdtrees; 108 : 109 : /// KD-Trees for nodes nearest to a given position on each local source app 110 : std::vector<std::vector<std::shared_ptr<KDTree>>> _local_positions_kdtrees; 111 : 112 : /// All the nodes that meet the spatial restrictions in all the local source apps 113 : std::vector<std::vector<Point>> _local_points; 114 : 115 : /// Values of the variable being transferred at all the points in _local_points 116 : std::vector<std::vector<Real>> _local_values; 117 : 118 : /// Number of points to consider 119 : unsigned int _num_nearest_points; 120 : 121 : /// Whether to group data when creating the nearest-point regions 122 : const bool _group_subapps; 123 : 124 : /// Number of KD-Trees to create 125 : unsigned int _num_sources; 126 : };