Skip to content

Commit d28f4e6

Browse files
authored
More simplifications and quality of life updates (#128)
1 parent bbe3ec9 commit d28f4e6

File tree

6 files changed

+88
-90
lines changed

6 files changed

+88
-90
lines changed

cpp/ContactConstraint.h

+51-50
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ dolfinx_mpc::mpc_data<T> compute_block_contributions(
253253
{
254254
const std::int32_t local_slave = local_slaves[i];
255255
std::iota(dofs.begin(), dofs.end(), local_slave_blocks[i] * block_size);
256-
auto local_max = std::find(dofs.begin(), dofs.end(), local_slave);
256+
auto local_max = std::ranges::find(dofs, local_slave);
257257
const auto max_index = std::distance(dofs.begin(), local_max);
258258
for (std::int32_t j = 0; j < block_size; j++)
259259
{
@@ -723,7 +723,7 @@ mpc_data<T> create_contact_slip_condition(
723723
}
724724
}
725725
}
726-
if (auto not_found = std::find(slave_found.begin(), slave_found.end(), false);
726+
if (auto not_found = std::ranges::find(slave_found, false);
727727
not_found != slave_found.end())
728728
{
729729
std::runtime_error(
@@ -768,17 +768,20 @@ mpc_data<T> create_contact_slip_condition(
768768
if (!(slave_found[c]) && (slave_max - slave_min > 0))
769769
{
770770
slave_found[c] = true;
771-
std::copy(remote_colliding_masters.begin() + proc_start + slave_min,
772-
remote_colliding_masters.begin() + proc_start + slave_max,
773-
offproc_masters.begin() + offproc_offsets[c]);
774-
775-
std::copy(remote_colliding_coeffs.begin() + proc_start + slave_min,
776-
remote_colliding_coeffs.begin() + proc_start + slave_max,
777-
offproc_coeffs.begin() + offproc_offsets[c]);
778-
779-
std::copy(remote_colliding_owners.begin() + proc_start + slave_min,
780-
remote_colliding_owners.begin() + proc_start + slave_max,
781-
offproc_owners.begin() + offproc_offsets[c]);
771+
std::ranges::copy(
772+
remote_colliding_masters.begin() + proc_start + slave_min,
773+
remote_colliding_masters.begin() + proc_start + slave_max,
774+
offproc_masters.begin() + offproc_offsets[c]);
775+
776+
std::ranges::copy(
777+
remote_colliding_coeffs.begin() + proc_start + slave_min,
778+
remote_colliding_coeffs.begin() + proc_start + slave_max,
779+
offproc_coeffs.begin() + offproc_offsets[c]);
780+
781+
std::ranges::copy(
782+
remote_colliding_owners.begin() + proc_start + slave_min,
783+
remote_colliding_owners.begin() + proc_start + slave_max,
784+
offproc_owners.begin() + offproc_offsets[c]);
782785
}
783786
}
784787
}
@@ -814,15 +817,15 @@ mpc_data<T> create_contact_slip_condition(
814817
{
815818
const std::int32_t master_min = masters_offsets[i];
816819
const std::int32_t master_max = masters_offsets[i + 1];
817-
std::copy(masters_out.begin() + master_min,
818-
masters_out.begin() + master_max,
819-
local_masters.begin() + local_offsets[i] + loc_pos[i]);
820-
std::copy(coefficients_out.begin() + master_min,
821-
coefficients_out.begin() + master_max,
822-
local_coeffs.begin() + local_offsets[i] + loc_pos[i]);
823-
std::copy(owners_out.begin() + master_min,
824-
owners_out.begin() + master_max,
825-
local_owners.begin() + local_offsets[i] + loc_pos[i]);
820+
std::ranges::copy(masters_out.begin() + master_min,
821+
masters_out.begin() + master_max,
822+
local_masters.begin() + local_offsets[i] + loc_pos[i]);
823+
std::ranges::copy(coefficients_out.begin() + master_min,
824+
coefficients_out.begin() + master_max,
825+
local_coeffs.begin() + local_offsets[i] + loc_pos[i]);
826+
std::ranges::copy(owners_out.begin() + master_min,
827+
owners_out.begin() + master_max,
828+
local_owners.begin() + local_offsets[i] + loc_pos[i]);
826829
loc_pos[i] += master_max - master_min;
827830
}
828831

@@ -832,18 +835,18 @@ mpc_data<T> create_contact_slip_condition(
832835
const std::int32_t master_min = offproc_offsets[i];
833836
const std::int32_t master_max = offproc_offsets[i + 1];
834837
const std::int32_t slave_index = slave_indices_remote[i];
835-
std::copy(offproc_masters.begin() + master_min,
836-
offproc_masters.begin() + master_max,
837-
local_masters.begin() + local_offsets[slave_index]
838-
+ loc_pos[slave_index]);
839-
std::copy(offproc_coeffs.begin() + master_min,
840-
offproc_coeffs.begin() + master_max,
841-
local_coeffs.begin() + local_offsets[slave_index]
842-
+ loc_pos[slave_index]);
843-
std::copy(offproc_owners.begin() + master_min,
844-
offproc_owners.begin() + master_max,
845-
local_owners.begin() + local_offsets[slave_index]
846-
+ loc_pos[slave_index]);
838+
std::ranges::copy(offproc_masters.begin() + master_min,
839+
offproc_masters.begin() + master_max,
840+
local_masters.begin() + local_offsets[slave_index]
841+
+ loc_pos[slave_index]);
842+
std::ranges::copy(offproc_coeffs.begin() + master_min,
843+
offproc_coeffs.begin() + master_max,
844+
local_coeffs.begin() + local_offsets[slave_index]
845+
+ loc_pos[slave_index]);
846+
std::ranges::copy(offproc_owners.begin() + master_min,
847+
offproc_owners.begin() + master_max,
848+
local_owners.begin() + local_offsets[slave_index]
849+
+ loc_pos[slave_index]);
847850
loc_pos[slave_index] += master_max - master_min;
848851
}
849852
}
@@ -1479,18 +1482,18 @@ mpc_data<T> create_contact_inelastic_condition(
14791482

14801483
// Count number of incoming slaves
14811484
std::vector<std::int32_t> inc_num_slaves(src_ranks_ghost.size(), 0);
1482-
std::ranges::for_each(
1483-
ghost_slaves,
1484-
[block_size, size_local, &ghost_owners, &inc_num_slaves,
1485-
&src_ranks_ghost](std::int32_t slave)
1486-
{
1487-
const std::int32_t owner
1488-
= ghost_owners[slave / block_size - size_local];
1489-
const auto it
1490-
= std::find(src_ranks_ghost.begin(), src_ranks_ghost.end(), owner);
1491-
const auto index = std::distance(src_ranks_ghost.begin(), it);
1492-
inc_num_slaves[index]++;
1493-
});
1485+
std::ranges::for_each(ghost_slaves,
1486+
[block_size, size_local, &ghost_owners, &inc_num_slaves,
1487+
&src_ranks_ghost](std::int32_t slave)
1488+
{
1489+
const std::int32_t owner
1490+
= ghost_owners[slave / block_size - size_local];
1491+
const auto it
1492+
= std::ranges::find(src_ranks_ghost, owner);
1493+
const auto index
1494+
= std::distance(src_ranks_ghost.begin(), it);
1495+
inc_num_slaves[index]++;
1496+
});
14941497
// Count number of outgoing slaves and masters
14951498
dolfinx::graph::AdjacencyList<int> shared_indices
14961499
= slave_index_map->index_to_dest_ranks();
@@ -1524,8 +1527,7 @@ mpc_data<T> create_contact_inelastic_condition(
15241527
const auto num_masters = (std::int32_t)masters_i.size();
15251528
for (auto proc : shared_indices.links(slave / block_size))
15261529
{
1527-
const auto it = std::find(dest_ranks_ghost.begin(),
1528-
dest_ranks_ghost.end(), proc);
1530+
const auto it = std::ranges::find(dest_ranks_ghost, proc);
15291531
std::int32_t index = std::distance(dest_ranks_ghost.begin(), it);
15301532
out_num_masters[index] += num_masters;
15311533
out_num_slaves[index]++;
@@ -1708,8 +1710,7 @@ mpc_data<T> create_contact_inelastic_condition(
17081710
std::vector<std::int32_t> local_ghosts
17091711
= map_dofs_global_to_local<U>(V, in_ghost_slaves);
17101712
slaves.resize(num_loc_slaves + num_ghost_slaves);
1711-
std::copy(local_ghosts.cbegin(), local_ghosts.cend(),
1712-
slaves.begin() + num_loc_slaves);
1713+
std::ranges::copy(local_ghosts, slaves.begin() + num_loc_slaves);
17131714
mpc.slaves = slaves;
17141715
mpc.masters = masters;
17151716
mpc.offsets = offsets;

cpp/PeriodicConstraint.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ dolfinx_mpc::mpc_data<T> _create_periodic_condition(
296296
if (rank == proc)
297297
continue;
298298
// Find position in neighborhood communicator
299-
auto it = std::find(s_to_m_ranks.begin(), s_to_m_ranks.end(), proc);
299+
auto it = std::ranges::find(s_to_m_ranks, proc);
300300
assert(it != s_to_m_ranks.end());
301301
auto dist = std::distance(s_to_m_ranks.begin(), it);
302302
const std::int32_t insert_location

cpp/assemble_utils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ std::vector<std::int32_t> dolfinx_mpc::compute_local_slave_index(
2020
assert((std::uint32_t)dof < is_slave.size());
2121
if (is_slave[dof])
2222
{
23-
auto it = std::find(slaves.begin(), slaves.end(), dof);
23+
auto it = std::ranges::find(slaves, dof);
2424
const auto slave_index = std::distance(slaves.begin(), it);
2525
local_index[slave_index] = i * bs + j;
2626
}

cpp/mpc_helpers.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ create_extended_functionspace(const dolfinx::fem::FunctionSpace<U>& V,
183183
// when we have multiple masters from the same block
184184

185185
if ((local_blocks[i] == -1)
186-
and (std::find(additional_ghosts.begin(), additional_ghosts.end(),
187-
global_blocks[i])
186+
and (std::ranges::find(additional_ghosts, global_blocks[i])
188187
== additional_ghosts.end()))
189188
{
190189
additional_ghosts.push_back(global_blocks[i]);
@@ -202,13 +201,11 @@ create_extended_functionspace(const dolfinx::fem::FunctionSpace<U>& V,
202201

203202
std::vector<std::int64_t> all_ghosts(num_ghosts + additional_ghosts.size());
204203
std::ranges::copy(ghosts, all_ghosts.begin());
205-
std::copy(additional_ghosts.cbegin(), additional_ghosts.cend(),
206-
all_ghosts.begin() + num_ghosts);
204+
std::ranges::copy(additional_ghosts, all_ghosts.begin() + num_ghosts);
207205

208206
std::vector<int> all_owners(all_ghosts.size());
209207
std::ranges::copy(ghost_owners, all_owners.begin());
210-
std::copy(additional_owners.cbegin(), additional_owners.cend(),
211-
all_owners.begin() + num_ghosts);
208+
std::ranges::copy(additional_owners, all_owners.begin() + num_ghosts);
212209

213210
// Create new indexmap with ghosts for master blocks added
214211
new_index_map = std::make_shared<dolfinx::common::IndexMap>(

cpp/utils.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ std::array<MPI_Comm, 2> dolfinx_mpc::create_neighborhood_comms(
2828
std::vector<std::uint8_t> has_slaves(mpi_size, slave_val);
2929
// Check if entities if master entities are on this processor
3030
std::vector<std::uint8_t> has_masters(mpi_size, 0);
31-
if (std::find(meshtags.values().begin(), meshtags.values().end(),
32-
master_marker)
31+
if (std::ranges::find(meshtags.values(), master_marker)
3332
!= meshtags.values().end())
3433
std::ranges::fill(has_masters, 1);
3534

cpp/utils.h

+31-30
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ create_block_to_facet_map(dolfinx::mesh::Topology& topology,
5959

6060
// Get local index of facet with respect to the cell
6161
auto cell_entities = c_to_e->links(cell[0]);
62-
const auto* it
63-
= std::find(cell_entities.data(),
64-
cell_entities.data() + cell_entities.size(), entities[i]);
65-
assert(it != (cell_entities.data() + cell_entities.size()));
66-
const int local_entity = std::distance(cell_entities.data(), it);
62+
const auto it
63+
= std::find(cell_entities.begin(), cell_entities.end(), entities[i]);
64+
assert(it != cell_entities.end());
65+
const auto local_entity = std::distance(cell_entities.begin(), it);
6766
local_indices[i] = local_entity;
6867
auto cell_blocks = dofmap.cell_dofs(cell[0]);
6968
auto closure_blocks
7069
= dofmap.element_dof_layout().entity_closure_dofs(dim, local_entity);
71-
for (std::size_t j = 0; j < closure_blocks.size(); ++j)
72-
{
73-
const int dof = cell_blocks[closure_blocks[j]];
74-
num_facets_per_dof[dof]++;
75-
}
70+
std::ranges::for_each(closure_blocks,
71+
[&num_facets_per_dof, &cell_blocks](auto block)
72+
{
73+
const int dof = cell_blocks[block];
74+
num_facets_per_dof[dof]++;
75+
});
7676
}
7777

7878
// Compute offsets
@@ -90,11 +90,13 @@ create_block_to_facet_map(dolfinx::mesh::Topology& topology,
9090
auto cell_blocks = dofmap.cell_dofs(cells[i]);
9191
auto closure_blocks = dofmap.element_dof_layout().entity_closure_dofs(
9292
dim, local_indices[i]);
93-
for (std::size_t j = 0; j < closure_blocks.size(); ++j)
94-
{
95-
const int dof = cell_blocks[closure_blocks[j]];
96-
data[offsets[dof] + num_facets_per_dof[dof]++] = entities[i];
97-
}
93+
std::for_each(closure_blocks.begin(), closure_blocks.end(),
94+
[&num_facets_per_dof, &data, &cell_blocks, &offsets,
95+
entity = entities[i]](auto block)
96+
{
97+
const int dof = cell_blocks[block];
98+
data[offsets[dof] + num_facets_per_dof[dof]++] = entity;
99+
});
98100
}
99101
return dolfinx::graph::AdjacencyList<std::int32_t>(data, offsets);
100102
}
@@ -749,7 +751,7 @@ dolfinx_mpc::mpc_data<T> distribute_ghost_data(
749751
std::div_t div = std::div(slaves[i], bs);
750752
slave_blocks[i] = div.quot;
751753
slave_rems[i] = div.rem;
752-
auto it = std::find(blocks.begin(), blocks.end(), div.quot);
754+
auto it = std::ranges::find(blocks, div.quot);
753755
assert(it != blocks.end());
754756
auto index = std::distance(blocks.begin(), it);
755757
parent_to_sub.push_back((int)index);
@@ -773,8 +775,7 @@ dolfinx_mpc::mpc_data<T> distribute_ghost_data(
773775
for (auto proc : shared_indices.links(parent_to_sub[i]))
774776
{
775777
// Find index of process in local MPI communicator
776-
auto it
777-
= std::find(dest_ranks_ghosts.begin(), dest_ranks_ghosts.end(), proc);
778+
auto it = std::ranges::find(dest_ranks_ghosts, proc);
778779
const auto index = std::distance(dest_ranks_ghosts.begin(), it);
779780
out_num_masters[index] += num_masters_per_slave[i];
780781
out_num_slaves[index]++;
@@ -828,8 +829,7 @@ dolfinx_mpc::mpc_data<T> distribute_ghost_data(
828829
for (auto proc : shared_indices.links(parent_to_sub[i]))
829830
{
830831
// Find index of process in local MPI communicator
831-
auto it
832-
= std::find(dest_ranks_ghosts.begin(), dest_ranks_ghosts.end(), proc);
832+
auto it = std::ranges::find(dest_ranks_ghosts, proc);
833833
const auto index = std::distance(dest_ranks_ghosts.begin(), it);
834834

835835
// Insert slave and num masters per slave
@@ -839,17 +839,18 @@ dolfinx_mpc::mpc_data<T> distribute_ghost_data(
839839
insert_slaves[index]++;
840840

841841
// Insert global master dofs to send
842-
std::copy(masters.begin() + master_start, masters.begin() + master_end,
843-
masters_out.begin() + disp_out_masters[index]
844-
+ insert_masters[index]);
842+
std::ranges::copy(masters.begin() + master_start,
843+
masters.begin() + master_end,
844+
masters_out.begin() + disp_out_masters[index]
845+
+ insert_masters[index]);
845846
// Insert owners to send
846-
std::copy(owners.begin() + master_start, owners.begin() + master_end,
847-
owners_out.begin() + disp_out_masters[index]
848-
+ insert_masters[index]);
847+
std::ranges::copy(
848+
owners.begin() + master_start, owners.begin() + master_end,
849+
owners_out.begin() + disp_out_masters[index] + insert_masters[index]);
849850
// Insert coeffs to send
850-
std::copy(coeffs.begin() + master_start, coeffs.begin() + master_end,
851-
coeffs_out.begin() + disp_out_masters[index]
852-
+ insert_masters[index]);
851+
std::ranges::copy(
852+
coeffs.begin() + master_start, coeffs.begin() + master_end,
853+
coeffs_out.begin() + disp_out_masters[index] + insert_masters[index]);
853854
insert_masters[index] += num_masters_per_slave[i];
854855
}
855856
}
@@ -1348,7 +1349,7 @@ std::pair<std::vector<U>, std::array<std::size_t, 2>> tabulate_dof_coordinates(
13481349

13491350
// Get cell dofmap
13501351
auto cell_dofs = dofmap->cell_dofs(cells[c]);
1351-
auto it = std::find(cell_dofs.begin(), cell_dofs.end(), dofs[c]);
1352+
auto it = std::ranges::find(cell_dofs, dofs[c]);
13521353
auto loc = std::distance(cell_dofs.begin(), it);
13531354

13541355
// Copy dof coordinates into vector

0 commit comments

Comments
 (0)