I am attempting to extract values from a pre-generated lookup table I have stored in an input data text file in the format of:
Particle charge (z) Energy (MeV) Dose (MeV)
1 100 0.681365
1 500 0.261032
2 200 1.69442
2 750 0.908794
3 125 5.22689
3 620 2.16828
4 330 5.06965
4 842 3.52304
except it is VERY large (every energy value ranging from 100-1500 MeV for every individual particle with charge 1-28). The goal is to input values for dose (generated measurements from another code and do not match values of dose in the lookup table exactly, hence why I'm using interpolation) and have the code utilize the lookup table to output the corresponding incident particle energy as well as the associated particle charge. I have not yet implemented as to how it will also output associated particle charge because I'm not exactly sure how to go about doing that. My biggest issue here is the error codes I keep getting. I'm not sure how to fix my code from here or find a better way to accomplish this as I've sort of ran into a brick wall. Any input would be sincerely appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>
#include <utility>
using namespace std;
//--------------------------------------
struct LTable // Basic data structure
{
double Charge;
double Energy;
double Dose;
};
//--------------------------------------
ifstream &operator >> ( ifstream &in, LTable &tr ) // Read one data item
{
in >> tr.Charge >> tr.Energy >> tr.Dose;
return in;
}
//--------------------------------------
vector<LTable> getData( istream &in ) // Read data from any input stream
{
vector<LTable> result;
string line;
LTable tr;
while ( getline( in, line ) )
{
ifstream ss( line );
if ( ss >> tr ) result.push_back( tr );
}
return result;
}
//--------------------------------------
// Interpolate (NOT use closest).
// Independent variable x, dependent variable y
// NOTE: must be sorted by whatever is x
double interpolate( const vector<LTable> &data, double LTable::*x, double LTable::*y, double xValue )
{
auto ip = lower_bound( data.begin(), data.end(), xValue, [ x ]( LTable a, double b ){ return a.*x < b; } );
// No extrapolation beyond data limits
if ( ip == data.end () )
{
return data.back ().*y;
}
else if ( ip == data.begin() )
{
return data.front().*y;
}
// Otherwise, linear interpolation
auto im = ip - 1;
double slope = ( (*ip).*y - (*im).*y ) / ( (*ip).*x - (*im).*x ); // dY/dX
return (*im).*y + slope * ( xValue - (*im).*x ); // Y0 + (dY/dX) * ( X - X0 )
}
//--------------------------------------
int main()
{
// Get data
ifstream in( "LTable.in");
vector<LTable> data = getData( in );
// Do some interpolation: Dose to Energy
cout << "Dose" << "\t" << "Particle Energy" << '\n';
sort( data.begin(), data.end(), []( LTable a, LTable b ){ return a.Dose < b.Dose; } );
double Dvalues[] = { 98.892, 98.913, 98.9341, 98.9553 };
for ( double Dose : Dvalues ) cout << Dose << '\t' << interpolate( data, <able::Dose, <able::Energy, Dose ) << '\n';
cout << '\n';
return 0;
}
//--------------------------------------
|
*code credits to @lastchance*
Errors:
Lookup_Table.cpp: In function ‘double interpolate(const std::vector<LTable, std::allocator<LTable> >&, double LTable::*, double LTable::*, double)’:
Lookup_Table.cpp:55: error: expected primary-expression before ‘[’ token
Lookup_Table.cpp:55: error: expected primary-expression before ‘a’
Lookup_Table.cpp:55: error: expected primary-expression before ‘double’
Lookup_Table.cpp:55: error: unable to deduce ‘auto’ from ‘<expression error>’
Lookup_Table.cpp:68: error: unable to deduce ‘auto’ from ‘<expression error>’
Lookup_Table.cpp: In function ‘int main()’:
Lookup_Table.cpp:84: error: expected primary-expression before ‘[’ token
Lookup_Table.cpp:84: error: expected primary-expression before ‘]’ token
Lookup_Table.cpp:84: error: expected primary-expression before ‘a’
Lookup_Table.cpp:84: error: expected primary-expression before ‘b’
Lookup_Table.cpp:87: error: expected initializer before ‘:’ token
Lookup_Table.cpp:90: error: expected primary-expression before ‘return’
Lookup_Table.cpp:90: error: expected ‘)’ before ‘return’