Skip to content

Commit

Permalink
use std::transform instead of loops
Browse files Browse the repository at this point in the history
  • Loading branch information
akva2 committed Sep 19, 2024
1 parent eb3e416 commit ee7abf9
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 103 deletions.
9 changes: 6 additions & 3 deletions opm/input/eclipse/Schedule/Action/ASTNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ namespace {
{
std::vector<std::string> strings;

for (const auto& qs : quoted_strings) {
strings.push_back(strip_quotes(qs));
}
std::transform(quoted_strings.begin(), quoted_strings.end(),
std::back_inserter(strings),
[](const std::string& qs)
{
return strip_quotes(qs);
});

return strings;
}
Expand Down
18 changes: 12 additions & 6 deletions opm/input/eclipse/Schedule/Well/PAvgCalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,12 @@ void PAvgCalculator<Scalar>::pruneInactiveWBPCells(const std::vector<bool>& isAc
auto newWBPCells = std::vector<std::size_t>{};
newWBPCells.reserve(activeIx.size());

for (const auto origCell : activeIx) {
newWBPCells.push_back(this->contributingCells_[origCell]);
}
std::transform(activeIx.begin(), activeIx.end(),
std::back_inserter(newWBPCells),
[this](const auto& origCell)
{
return this->contributingCells_[origCell];
});

this->contributingCells_.swap(newWBPCells);
}
Expand Down Expand Up @@ -850,9 +853,12 @@ void PAvgCalculator<Scalar>::pruneInactiveConnections(const std::vector<bool>& i

// 3. Renumber set of open connections to match new sequence of active
// connections_.
for (auto& openConnID : this->openConns_) {
openConnID = newConnIDs[openConnID];
}
std::transform(this->openConns_.begin(), this->openConns_.end(),
this->openConns_.begin(),
[&newConnIDs](const auto& openConnID)
{
return newConnIDs[openConnID];
});
}

template<class Scalar>
Expand Down
9 changes: 6 additions & 3 deletions opm/io/eclipse/EGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <filesystem>
#include <fstream>
#include <iterator>
#include <iomanip>
#include <numeric>
#include <string>
#include <stdexcept>
Expand Down Expand Up @@ -165,8 +164,12 @@ EGrid::EGrid(const std::string& filename, const std::string& grid_name)
auto hostnum = getImpl(hostnum_index, INTE, inte_array, "integer");
host_cells.reserve(hostnum.size());

for (auto val : hostnum)
host_cells.push_back(val -1);
std::transform(hostnum.begin(), hostnum.end(),
std::back_inserter(host_cells),
[](const auto& val)
{
return val - 1;
});
}
}

Expand Down
9 changes: 6 additions & 3 deletions opm/io/eclipse/ESmry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@ class ESmry
std::vector<T> result;
result.reserve(seqIndex.size());

for (const auto& ind : seqIndex){
result.push_back(full_vector[ind]);
}
std::transform(seqIndex.begin(), seqIndex.end(),
std::back_inserter(result),
[&full_vector](const auto& ind)
{
return full_vector[ind];
});

return result;
}
Expand Down
18 changes: 13 additions & 5 deletions opm/io/eclipse/ESmry_write_rsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,21 @@ void ESmry::write_rsm(std::ostream& os) const
std::vector<std::string> time_column;
if (this->hasKey("DAY") && this->hasKey("MONTH") && this->hasKey("YEAR")) {
write_dates = true;
for (const auto& t : this->dates()) {
time_column.push_back(format_date(t));
}
const auto dates = this->dates();
std::transform(dates.begin(), dates.end(),
std::back_inserter(time_column),
[](const auto& t)
{
return format_date(t);
});
} else {
const auto& time_data = this->get("TIME");
for (const auto& t : time_data)
time_column.push_back(format_float_element(t));
std::transform(time_data.begin(), time_data.end(),
std::back_inserter(time_column),
[](const auto& t)
{
return format_float_element(t);
});
}

for (const auto& data_vector_block : data_vector_blocks) {
Expand Down
27 changes: 18 additions & 9 deletions opm/io/eclipse/ExtESmry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <filesystem>
#include <iterator>
#include <stdexcept>
#include <string>
#include <fstream>
#include <cmath>
#include <cstring>
#include <thread>


namespace {

Opm::time_point make_date(const std::vector<int>& datetime) {
Expand Down Expand Up @@ -241,8 +240,12 @@ std::vector<float> ExtESmry::get_at_rstep(const std::string& name)
std::vector<float> rs_vect;
rs_vect.reserve(m_seqIndex.size());

for (auto r : m_seqIndex)
rs_vect.push_back(full_vect[r]);
std::transform(m_seqIndex.begin(), m_seqIndex.end(),
std::back_inserter(rs_vect),
[&full_vect](const auto& r)
{
return full_vect[r];
});

return rs_vect;
}
Expand Down Expand Up @@ -583,12 +586,18 @@ const std::vector<float>& ExtESmry::get(const std::string& name)
return m_vectorData[index];
}

std::vector<Opm::time_point> ExtESmry::dates() {
std::vector<Opm::time_point> ExtESmry::dates()
{
double time_unit = 24 * 3600;
std::vector<Opm::time_point> d;

for (const auto& t : this->get("TIME"))
d.push_back( this->m_startdat + std::chrono::duration_cast<std::chrono::seconds>( std::chrono::duration<double, std::chrono::seconds::period>( t * time_unit)));
const auto time = this->get("TIME");
std::transform(time.begin(), time.end(), std::back_inserter(d),
[this, time_unit](const auto& t)
{
using Seconds = std::chrono::duration<double, std::chrono::seconds::period>;
return this->m_startdat + std::chrono::duration_cast<std::chrono::seconds>(Seconds{t * time_unit});
});

return d;
}
Expand Down
11 changes: 8 additions & 3 deletions opm/material/fluidmatrixinteractions/EclMaterialLawManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include <opm/material/fluidmatrixinteractions/EclEpsGridProperties.hpp>
#include <opm/material/fluidstates/SimpleModularFluidState.hpp>

#include <algorithm>

namespace Opm {

template<class TraitsT>
Expand Down Expand Up @@ -76,9 +78,12 @@ initFromState(const EclipseState& eclState)
stoneEtas_.clear();
stoneEtas_.reserve(numSatRegions);

for (const auto& table : stone1exTables) {
stoneEtas_.push_back(table.eta);
}
std::transform(stone1exTables.begin(), stone1exTables.end(),
std::back_inserter(stoneEtas_),
[](const auto& table)
{
return table.eta;
});
}

const auto& ppcwmaxTables = tables.getPpcwmax();
Expand Down
9 changes: 6 additions & 3 deletions opm/output/eclipse/InteHEAD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,9 +948,12 @@ namespace {
}
}

for (const auto& aq : sched.aqufluxs) {
aquiferIDs.push_back(aq.first);
}
std::transform(sched.aqufluxs.begin(), sched.aqufluxs.end(),
std::back_inserter(aquiferIDs),
[](const auto& aq)
{
return aq.first;
});

return numUnique(std::move(aquiferIDs));
}
Expand Down
11 changes: 7 additions & 4 deletions opm/output/eclipse/RegionCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
#include <opm/input/eclipse/Schedule/Well/WellConnections.hpp>
#include <opm/input/eclipse/Schedule/Well/WellMatcher.hpp>

#include <algorithm>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
Expand All @@ -54,9 +54,12 @@ void Opm::out::RegionCache::buildCache(const std::set<std::string>& fip_regions,
}

auto regions = std::vector<std::reference_wrapper<const std::vector<int>>>{};
for (const auto& fipReg : fip_regions) {
regions.push_back(std::cref(fp.get_int(fipReg)));
}
std::transform(fip_regions.begin(), fip_regions.end(),
std::back_inserter(regions),
[&fp](const auto& fipReg)
{
return std::cref(fp.get_int(fipReg));
});

for (const auto& wname : schedule.back().well_order()) {
const auto& conns = schedule.back().wells(wname).getConnections();
Expand Down
16 changes: 12 additions & 4 deletions opm/output/eclipse/RestartIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,12 @@ namespace {
xwel.push_back( well.thp );
xwel.push_back( well.temperature );

for (auto phase : phases)
xwel.push_back(well.rates.get(phase));
std::transform(phases.begin(), phases.end(),
std::back_inserter(xwel),
[&well](const auto& phase)
{
return well.rates.get(phase);
});

for (const auto& sc : sched_well.getConnections()) {
const auto i = sc.getI(), j = sc.getJ(), k = sc.getK();
Expand Down Expand Up @@ -202,8 +206,12 @@ namespace {
xwel.push_back(connection->cell_saturation_gas);
xwel.push_back(connection->effective_Kh);

for (auto phase : phases)
xwel.push_back(connection->rates.get(phase));
std::transform(phases.begin(), phases.end(),
std::back_inserter(xwel),
[&connection](const auto& phase)
{
return connection->rates.get(phase);
});
}
}

Expand Down
Loading

0 comments on commit ee7abf9

Please sign in to comment.