2020-09-15 14:03:07 +00:00
|
|
|
/*
|
|
|
|
* This program source code file
|
|
|
|
* is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 <janvi@veith.net>
|
2021-01-27 12:50:16 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-09-15 14:03:07 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
#include <array>
|
2021-10-05 15:30:22 +00:00
|
|
|
#include <algorithm>
|
2021-10-04 11:05:51 +00:00
|
|
|
|
2021-10-08 14:44:44 +00:00
|
|
|
#include <calculator_panels/panel_eserie.h>
|
2021-10-14 13:20:54 +00:00
|
|
|
#include <wx/msgdlg.h>
|
2021-10-04 11:05:51 +00:00
|
|
|
|
|
|
|
/* If BENCHMARK is defined, any 4R E12 calculations will print its execution time to console
|
|
|
|
* My Hasswell Enthusiast reports 225 mSec what are reproducible within plusminus 2 percent
|
|
|
|
*/
|
|
|
|
//#define BENCHMARK
|
|
|
|
|
2020-09-15 21:26:38 +00:00
|
|
|
#ifdef BENCHMARK
|
2021-10-14 13:20:54 +00:00
|
|
|
#include <profile.h>
|
2020-09-15 21:26:38 +00:00
|
|
|
#endif
|
|
|
|
|
2020-09-15 14:03:07 +00:00
|
|
|
#include "eserie.h"
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2021-10-05 15:30:22 +00:00
|
|
|
extern double DoubleFromString( const wxString& TextValue );
|
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
E_SERIE r;
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
// Return a string from aValue (aValue is expected in ohms)
|
|
|
|
// If aValue < 1000 the returned string is aValue with unit = R
|
|
|
|
// If aValue >= 1000 the returned string is aValue/1000 with unit = K
|
|
|
|
// with notation similar to 2K2
|
|
|
|
// If aValue >= 1e6 the returned string is aValue/1e6 with unit = M
|
|
|
|
// with notation = 1M
|
|
|
|
static std::string strValue( double aValue )
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
if( aValue < 1000.0 )
|
|
|
|
{
|
|
|
|
result = std::to_string( static_cast<int>( aValue ) );
|
|
|
|
result += 'R';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
double div = 1e3;
|
|
|
|
int unit = 'K';
|
|
|
|
|
|
|
|
if( aValue >= 1e6 )
|
|
|
|
{
|
|
|
|
div = 1e6;
|
|
|
|
unit = 'M';
|
|
|
|
}
|
|
|
|
|
|
|
|
aValue /= div;
|
|
|
|
|
|
|
|
int integer = static_cast<int>( aValue );
|
|
|
|
result = std::to_string(integer);
|
|
|
|
result += unit;
|
|
|
|
|
|
|
|
// Add mantissa: 1 digit, suitable for series up to E24
|
|
|
|
double mantissa = aValue - integer;
|
|
|
|
|
|
|
|
if( mantissa > 0 )
|
|
|
|
result += std::to_string( static_cast<int>( (mantissa*10)+0.5 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
E_SERIE::E_SERIE()
|
|
|
|
{
|
|
|
|
// Build the list of available resistor values in each En serie
|
|
|
|
double listValuesE1[] = { E1_VALUES };
|
|
|
|
double listValuesE3[] = { E3_VALUES };
|
|
|
|
double listValuesE6[] = { E6_VALUES };
|
|
|
|
double listValuesE12[] = { E12_VALUES };
|
|
|
|
double listValuesE24[] = { E24_VALUES };
|
|
|
|
// buildSerieData must be called in the order of En series, because
|
|
|
|
// the list of series is expected indexed by En for the serie En
|
|
|
|
buildSerieData( E1, listValuesE1 );
|
|
|
|
buildSerieData( E3, listValuesE3 );
|
|
|
|
buildSerieData( E6, listValuesE6 );
|
|
|
|
buildSerieData( E12, listValuesE12 );
|
|
|
|
int count = buildSerieData( E24, listValuesE24 );
|
|
|
|
|
|
|
|
// Reserve a buffer for intermediate calculations:
|
|
|
|
// the buffer size is 2*count*count to store all combinaisons of 2 values
|
|
|
|
// there are 2*count*count = 29282 combinations for E24
|
|
|
|
int bufsize = 2*count*count;
|
|
|
|
m_cmb_lut.reserve( bufsize );
|
|
|
|
|
|
|
|
// Store predefined R_DATA items.
|
|
|
|
for( int ii = 0; ii < bufsize; ii++ )
|
|
|
|
m_cmb_lut.emplace_back( "", 0.0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int E_SERIE::buildSerieData( int aEserie, double aList[] )
|
|
|
|
{
|
|
|
|
double curr_coeff = FIRST_VALUE;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
std::vector<R_DATA> curr_list;
|
|
|
|
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
double curr_r = curr_coeff;
|
|
|
|
|
|
|
|
for( int ii = 0; ; ii++ )
|
|
|
|
{
|
|
|
|
if( aList[ii] == 0.0 ) // End of list
|
|
|
|
break;
|
|
|
|
|
|
|
|
double curr_r = curr_coeff * aList[ii];
|
|
|
|
curr_list.emplace_back( strValue( curr_r ), curr_r );
|
|
|
|
count++;
|
|
|
|
|
|
|
|
if( curr_r >= LAST_VALUE )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( curr_r >= LAST_VALUE )
|
|
|
|
break;
|
|
|
|
|
|
|
|
curr_coeff *= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_luts.push_back( std::move( curr_list ) );
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
void E_SERIE::Exclude( double aValue )
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2021-10-14 13:20:54 +00:00
|
|
|
if( aValue ) // if there is a value to exclude other than a wire jumper
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2021-10-04 11:05:51 +00:00
|
|
|
for( R_DATA& i : m_luts[m_series] ) // then search it in the selected E-Serie lookup table
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2021-10-14 13:20:54 +00:00
|
|
|
if( i.e_value == aValue ) // if the value to exclude is found
|
2020-09-15 14:03:07 +00:00
|
|
|
i.e_use = false; // disable its use
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
void E_SERIE::simple_solution( uint32_t aSize )
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S2R ).e_value = std::numeric_limits<double>::max(); // assume no 2R solution or max deviation
|
2020-09-21 13:18:55 +00:00
|
|
|
|
2020-09-15 14:03:07 +00:00
|
|
|
for( i = 0; i < aSize; i++ )
|
|
|
|
{
|
2021-10-14 13:20:54 +00:00
|
|
|
if( abs( m_cmb_lut.at( i ).e_value - m_required_value ) < abs( m_results.at( S2R ).e_value ) )
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S2R ).e_value = m_cmb_lut.at( i ).e_value - m_required_value; // save signed deviation in Ohms
|
|
|
|
m_results.at( S2R ).e_name = m_cmb_lut.at( i ).e_name; // save combination text
|
|
|
|
m_results.at( S2R ).e_use = true; // this is a possible solution
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
void E_SERIE::combine4( uint32_t aSize )
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
uint32_t i,j;
|
|
|
|
double tmp;
|
|
|
|
std::string s;
|
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S4R ).e_use = false; // disable 4R solution, until
|
|
|
|
m_results.at( S4R ).e_value = m_results.at( S3R ).e_value; // 4R becomes better than 3R solution
|
2020-09-15 14:03:07 +00:00
|
|
|
|
|
|
|
#ifdef BENCHMARK
|
2021-10-14 13:20:54 +00:00
|
|
|
PROF_COUNTER timer; // start timer to count execution time
|
2020-09-15 14:03:07 +00:00
|
|
|
#endif
|
2020-09-21 13:18:55 +00:00
|
|
|
|
2020-09-15 14:03:07 +00:00
|
|
|
for( i = 0; i < aSize; i++ ) // 4R search outer loop
|
|
|
|
{ // scan valid intermediate 2R solutions
|
|
|
|
for( j = 0; j < aSize; j++ ) // inner loop combines all with itself
|
|
|
|
{
|
2021-10-14 13:20:54 +00:00
|
|
|
tmp = m_cmb_lut.at( i ).e_value + m_cmb_lut.at( j ).e_value; // calculate 2R+2R serial
|
|
|
|
tmp -= m_required_value; // calculate 4R deviation
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
if( abs( tmp ) < abs( m_results.at(S4R).e_value ) ) // if new 4R is better
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S4R ).e_value = tmp; // save amount of benefit
|
2020-09-15 14:03:07 +00:00
|
|
|
std::string s = "( ";
|
2021-10-14 13:20:54 +00:00
|
|
|
s.append( m_cmb_lut.at( i ).e_name ); // mention 1st 2 component
|
|
|
|
s.append( " ) + ( " ); // in series
|
|
|
|
s.append( m_cmb_lut.at( j ).e_name ); // with 2nd 2 components
|
2020-09-15 14:03:07 +00:00
|
|
|
s.append( " )" );
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S4R ).e_name = s; // save the result and
|
|
|
|
m_results.at( S4R ).e_use = true; // enable for later use
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
tmp = ( m_cmb_lut[i].e_value * m_cmb_lut.at( j ).e_value ) /
|
|
|
|
( m_cmb_lut[i].e_value + m_cmb_lut.at( j ).e_value ); // calculate 2R|2R parallel
|
2021-02-12 00:47:08 +00:00
|
|
|
tmp -= m_required_value; // calculate 4R deviation
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
if( abs( tmp ) < abs( m_results.at( S4R ).e_value ) ) // if new 4R is better
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S4R ).e_value = tmp; // save amount of benefit
|
2020-09-15 14:03:07 +00:00
|
|
|
std::string s = "( ";
|
2021-10-14 13:20:54 +00:00
|
|
|
s.append( m_cmb_lut.at( i ).e_name ); // mention 1st 2 component
|
2020-09-15 14:03:07 +00:00
|
|
|
s.append( " ) | ( " ); // in parallel
|
2021-10-14 13:20:54 +00:00
|
|
|
s.append( m_cmb_lut.at( j ).e_name ); // with 2nd 2 components
|
2020-09-15 14:03:07 +00:00
|
|
|
s.append( " )" );
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S4R ).e_name = s; // save the result
|
|
|
|
m_results.at( S4R ).e_use = true; // enable later use
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BENCHMARK
|
2021-10-14 13:20:54 +00:00
|
|
|
printf( "Calculation time = %d mS", timer.msecs() );
|
|
|
|
fflush( 0 );
|
2020-09-15 14:03:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2021-10-10 15:03:24 +00:00
|
|
|
void E_SERIE::NewCalc()
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2021-10-04 11:05:51 +00:00
|
|
|
for( R_DATA& i : m_cmb_lut )
|
2021-02-12 00:47:08 +00:00
|
|
|
i.e_use = false; // before any calculation is done, assume that
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
for( R_DATA& i : m_results )
|
2021-02-12 00:47:08 +00:00
|
|
|
i.e_use = false; // no combinations and no results are available
|
2020-09-21 13:18:55 +00:00
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
for( R_DATA& i : m_luts[m_series])
|
Fix source comment/doc typos (follow-up)
Found via `codespell -q 3 -S *.po,./thirdparty -L aactual,acount,aline,alocation,alog,anormal,anumber,aother,apoints,aparent,aray,dout,einstance,modul,ot,overide,serie,te,,tesselate,tesselator,tht`
2021-07-03 22:37:31 +00:00
|
|
|
i.e_use = true; // all selected E-values available
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2021-10-10 15:03:24 +00:00
|
|
|
uint32_t E_SERIE::combine2()
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
uint32_t combi2R = 0; // target index counts calculated 2R combinations
|
|
|
|
std::string s;
|
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
for( const R_DATA& i : m_luts[m_series] ) // outer loop to sweep selected source lookup table
|
2020-09-21 13:18:55 +00:00
|
|
|
{
|
2020-09-15 14:03:07 +00:00
|
|
|
if( i.e_use )
|
|
|
|
{
|
2021-10-04 11:05:51 +00:00
|
|
|
for( const R_DATA& j : m_luts[m_series] ) // inner loop to combine values with itself
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
if( j.e_use )
|
|
|
|
{
|
2021-10-14 13:20:54 +00:00
|
|
|
m_cmb_lut.at( combi2R ).e_use = true;
|
|
|
|
m_cmb_lut.at( combi2R ).e_value = i.e_value + j.e_value; // calculate 2R serial
|
2020-09-15 14:03:07 +00:00
|
|
|
s = i.e_name;
|
|
|
|
s.append( " + " );
|
2021-10-14 13:20:54 +00:00
|
|
|
m_cmb_lut.at( combi2R ).e_name = s.append( j.e_name);
|
|
|
|
combi2R++; // next destination
|
|
|
|
m_cmb_lut.at( combi2R ).e_use = true; // calculate 2R parallel
|
|
|
|
m_cmb_lut.at( combi2R ).e_value = i.e_value * j.e_value / ( i.e_value + j.e_value );
|
2020-09-15 14:03:07 +00:00
|
|
|
s = i.e_name;
|
|
|
|
s.append( " | " );
|
2021-10-14 13:20:54 +00:00
|
|
|
m_cmb_lut.at( combi2R ).e_name = s.append( j.e_name );
|
2020-09-15 14:03:07 +00:00
|
|
|
combi2R++; // next destination
|
2020-09-21 13:18:55 +00:00
|
|
|
}
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 15:03:24 +00:00
|
|
|
return combi2R;
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
void E_SERIE::combine3( uint32_t aSize )
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
uint32_t j = 0;
|
|
|
|
double tmp = 0; // avoid warning for being uninitialized
|
|
|
|
std::string s;
|
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S3R ).e_use = false; // disable 3R solution, until
|
|
|
|
m_results.at( S3R ).e_value = m_results.at( S2R ).e_value; // 3R becomes better than 2R solution
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
for( const R_DATA& i : m_luts[m_series] ) // 3R Outer loop to selected primary E serie LUT
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
if( i.e_use ) // skip all excluded values
|
|
|
|
{
|
|
|
|
for( j = 0; j < aSize; j++ ) // inner loop combines with all 2R intermediate results
|
|
|
|
{ // R+2R serial combi
|
2021-10-14 13:20:54 +00:00
|
|
|
tmp = m_cmb_lut.at( j ).e_value + i.e_value;
|
2021-02-12 00:47:08 +00:00
|
|
|
tmp -= m_required_value; // calculate deviation
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
if( abs( tmp ) < abs( m_results.at( S3R ).e_value ) ) // compare if better
|
2020-09-15 14:03:07 +00:00
|
|
|
{ // then take it
|
|
|
|
s = i.e_name; // mention 3rd component
|
|
|
|
s.append( " + ( " ); // in series
|
2021-10-14 13:20:54 +00:00
|
|
|
s.append( m_cmb_lut.at( j ).e_name ); // with 2R combination
|
2020-09-15 14:03:07 +00:00
|
|
|
s.append( " )" );
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S3R ).e_name = s; // save S3R result
|
|
|
|
m_results.at( S3R ).e_value = tmp; // save amount of benefit
|
|
|
|
m_results.at( S3R ).e_use = true; // enable later use
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
tmp = i.e_value * m_cmb_lut.at( j ).e_value /
|
|
|
|
( i.e_value + m_cmb_lut.at( j ).e_value ); // calculate R + 2R parallel
|
2021-02-12 00:47:08 +00:00
|
|
|
tmp -= m_required_value; // calculate deviation
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
if( abs( tmp ) < abs( m_results.at( S3R ).e_value ) ) // compare if better
|
2020-09-15 14:03:07 +00:00
|
|
|
{ // then take it
|
|
|
|
s = i.e_name; // mention 3rd component
|
|
|
|
s.append( " | ( " ); // in parallel
|
2021-10-14 13:20:54 +00:00
|
|
|
s.append( m_cmb_lut.at( j ).e_name ); // with 2R combination
|
2020-09-15 14:03:07 +00:00
|
|
|
s.append( " )" );
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S3R ).e_name = s;
|
|
|
|
m_results.at( S3R ).e_value = tmp; // save amount of benefit
|
|
|
|
m_results.at( S3R ).e_use = true; // enable later use
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 15:03:24 +00:00
|
|
|
|
|
|
|
// If there is a 3R result with remaining deviation consider to search a possibly better 4R solution
|
|
|
|
// calculate 4R for small series always
|
2021-10-14 13:20:54 +00:00
|
|
|
if(( m_results.at( S3R ).e_use == true ) && tmp )
|
2021-10-10 15:03:24 +00:00
|
|
|
combine4( aSize );
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2021-10-10 15:03:24 +00:00
|
|
|
void E_SERIE::Calculate()
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
uint32_t no_of_2Rcombi = 0;
|
|
|
|
|
|
|
|
no_of_2Rcombi = combine2(); // combine all 2R combinations for selected E serie
|
|
|
|
simple_solution( no_of_2Rcombi ); // search for simple 2 component solution
|
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
if( m_results.at( S2R ).e_value ) // if simple 2R result is not exact
|
2020-09-15 14:03:07 +00:00
|
|
|
combine3( no_of_2Rcombi ); // continiue searching for a possibly better solution
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2020-09-15 14:03:07 +00:00
|
|
|
strip3();
|
|
|
|
strip4();
|
|
|
|
}
|
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2021-10-10 15:03:24 +00:00
|
|
|
void E_SERIE::strip3()
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
if( m_results.at( S3R ).e_use ) // if there is a 3 term result available
|
2020-09-15 14:03:07 +00:00
|
|
|
{ // what is connected either by two "|" or by 3 plus
|
2021-10-14 13:20:54 +00:00
|
|
|
s = m_results.at( S3R ).e_name;
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
if( ( std::count( s.begin(), s.end(), '+' ) == 2 )
|
|
|
|
|| ( std::count( s.begin(), s.end(), '|' ) == 2 ) )
|
2020-09-15 14:03:07 +00:00
|
|
|
{ // then strip one pair of braces
|
|
|
|
s.erase( s.find( "(" ), 1 ); // it is known sure, this is available
|
|
|
|
s.erase( s.find( ")" ), 1 ); // in any unstripped 3R result term
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S3R ).e_name = s; // use stripped result
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2021-10-10 15:03:24 +00:00
|
|
|
void E_SERIE::strip4()
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
if( m_results.at( S4R ).e_use ) // if there is a 4 term result available
|
|
|
|
{ // what are connected either by 3 "+" or by 3 "|"
|
|
|
|
s = m_results.at( S4R ).e_name;
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
if( ( std::count( s.begin(), s.end(), '+' ) == 3 )
|
|
|
|
|| ( std::count( s.begin(), s.end(), '|' ) == 3 ) )
|
2021-10-14 13:20:54 +00:00
|
|
|
{ // then strip two pair of braces
|
|
|
|
s.erase( s.find( "(" ), 1 ); // it is known sure, they are available
|
|
|
|
s.erase( s.find( ")" ), 1 ); // in any unstripped 4R result term
|
2020-09-15 14:03:07 +00:00
|
|
|
s.erase( s.find( "(" ), 1 );
|
|
|
|
s.erase( s.find( ")" ), 1 );
|
2021-10-14 13:20:54 +00:00
|
|
|
m_results.at( S4R ).e_name = s; // use stripped result
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 00:47:08 +00:00
|
|
|
|
2021-10-05 11:47:40 +00:00
|
|
|
void PANEL_E_SERIE::OnCalculateESeries( wxCommandEvent& event )
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
double reqr; // required resistor stored in local copy
|
|
|
|
double error, err3 = 0;
|
|
|
|
wxString es, fs; // error and formula strings
|
|
|
|
|
2021-10-14 13:20:54 +00:00
|
|
|
wxBusyCursor dummy;
|
|
|
|
|
2020-09-15 14:03:07 +00:00
|
|
|
reqr = ( 1000 * DoubleFromString( m_ResRequired->GetValue() ) );
|
2021-06-09 19:32:58 +00:00
|
|
|
r.SetRequiredValue( reqr ); // keep a local copy of required resistor value
|
2021-02-12 00:47:08 +00:00
|
|
|
r.NewCalc(); // assume all values available
|
2020-09-21 13:18:55 +00:00
|
|
|
/*
|
2020-09-15 14:03:07 +00:00
|
|
|
* Exclude itself. For the case, a value from the available series is found as required value,
|
|
|
|
* the calculator assumes this value needs a replacement for the reason of being not available.
|
2021-06-09 19:32:58 +00:00
|
|
|
* Two further exclude values can be entered to exclude and are skipped as not being available.
|
2020-09-15 14:03:07 +00:00
|
|
|
* All values entered in KiloOhms are converted to Ohm for internal calculation
|
|
|
|
*/
|
2021-02-12 00:47:08 +00:00
|
|
|
r.Exclude( 1000 * DoubleFromString( m_ResRequired->GetValue()));
|
|
|
|
r.Exclude( 1000 * DoubleFromString( m_ResExclude1->GetValue()));
|
|
|
|
r.Exclude( 1000 * DoubleFromString( m_ResExclude2->GetValue()));
|
2021-10-14 13:20:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
r.Calculate();
|
|
|
|
}
|
|
|
|
catch (std::out_of_range const& exc)
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
msg << "Internal error: " << exc.what();
|
|
|
|
|
|
|
|
wxMessageBox( msg );
|
|
|
|
return;
|
|
|
|
}
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
fs = r.GetResults()[S2R].e_name; // show 2R solution formula string
|
2020-09-15 14:03:07 +00:00
|
|
|
m_ESeries_Sol2R->SetValue( fs );
|
2021-10-04 11:05:51 +00:00
|
|
|
error = reqr + r.GetResults()[S2R].e_value; // absolute value of solution
|
2020-09-15 14:03:07 +00:00
|
|
|
error = ( reqr / error - 1 ) * 100; // error in percent
|
|
|
|
|
|
|
|
if( error )
|
|
|
|
{
|
2020-09-21 13:18:55 +00:00
|
|
|
if( std::abs( error ) < 0.01 )
|
2021-01-27 12:50:16 +00:00
|
|
|
es.Printf( "<%.2f", 0.01 );
|
2020-09-21 13:18:55 +00:00
|
|
|
else
|
|
|
|
es.Printf( "%+.2f",error);
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-27 12:50:16 +00:00
|
|
|
es = _( "Exact" );
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_ESeriesError2R->SetValue( es ); // anyway show 2R error string
|
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
if( r.GetResults()[S3R].e_use ) // if 3R solution available
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2021-10-04 11:05:51 +00:00
|
|
|
err3 = reqr + r.GetResults()[S3R].e_value; // calculate the 3R
|
2020-09-15 14:03:07 +00:00
|
|
|
err3 = ( reqr / err3 - 1 ) * 100; // error in percent
|
|
|
|
|
2020-09-21 13:18:55 +00:00
|
|
|
if( err3 )
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2020-09-21 13:18:55 +00:00
|
|
|
if( std::abs( err3 ) < 0.01 )
|
2021-01-27 12:50:16 +00:00
|
|
|
es.Printf( "<%.2f", 0.01 );
|
2020-09-21 13:18:55 +00:00
|
|
|
else
|
|
|
|
es.Printf( "%+.2f",err3);
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-27 12:50:16 +00:00
|
|
|
es = _( "Exact" );
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_ESeriesError3R->SetValue( es ); // show 3R error string
|
2021-10-04 11:05:51 +00:00
|
|
|
fs = r.GetResults()[S3R].e_name;
|
2020-09-15 14:03:07 +00:00
|
|
|
m_ESeries_Sol3R->SetValue( fs ); // show 3R formula string
|
|
|
|
}
|
|
|
|
else // nothing better than 2R found
|
|
|
|
{
|
2021-01-27 12:50:16 +00:00
|
|
|
fs = _( "Not worth using" );
|
2020-09-15 14:03:07 +00:00
|
|
|
m_ESeries_Sol3R->SetValue( fs );
|
|
|
|
m_ESeriesError3R->SetValue( wxEmptyString );
|
|
|
|
}
|
|
|
|
|
|
|
|
fs = wxEmptyString;
|
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
if( r.GetResults()[S4R].e_use ) // show 4R solution if available
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2021-10-04 11:05:51 +00:00
|
|
|
fs = r.GetResults()[S4R].e_name;
|
2020-09-15 14:03:07 +00:00
|
|
|
|
2021-10-04 11:05:51 +00:00
|
|
|
error = reqr + r.GetResults()[S4R].e_value; // absolute value of solution
|
2020-09-15 14:03:07 +00:00
|
|
|
error = ( reqr / error - 1 ) * 100; // error in percent
|
|
|
|
|
|
|
|
if( error )
|
|
|
|
es.Printf( "%+.2f",error );
|
|
|
|
else
|
2021-01-27 12:50:16 +00:00
|
|
|
es = _( "Exact" );
|
2020-09-15 14:03:07 +00:00
|
|
|
|
|
|
|
m_ESeriesError4R->SetValue( es );
|
|
|
|
}
|
|
|
|
else // no 4R solution
|
2020-09-21 13:18:55 +00:00
|
|
|
{
|
2021-01-27 12:50:16 +00:00
|
|
|
fs = _( "Not worth using" );
|
2020-09-15 14:03:07 +00:00
|
|
|
es = wxEmptyString;
|
|
|
|
m_ESeriesError4R->SetValue( es );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ESeries_Sol4R->SetValue( fs );
|
|
|
|
}
|
|
|
|
|
2021-10-05 11:47:40 +00:00
|
|
|
|
|
|
|
void PANEL_E_SERIE::OnESeriesSelection( wxCommandEvent& event )
|
2020-09-15 14:03:07 +00:00
|
|
|
{
|
2021-02-11 23:43:08 +00:00
|
|
|
if( event.GetEventObject() == m_e1 )
|
2021-02-12 00:47:08 +00:00
|
|
|
r.SetSeries( E1 );
|
2021-02-11 23:43:08 +00:00
|
|
|
else if( event.GetEventObject() == m_e3 )
|
2021-02-12 00:47:08 +00:00
|
|
|
r.SetSeries( E3 );
|
2021-02-11 23:43:08 +00:00
|
|
|
else if( event.GetEventObject() == m_e12 )
|
2021-02-12 00:47:08 +00:00
|
|
|
r.SetSeries( E12 );
|
2021-10-14 13:20:54 +00:00
|
|
|
else if( event.GetEventObject() == m_e24 )
|
|
|
|
r.SetSeries( E24 );
|
2021-02-11 23:43:08 +00:00
|
|
|
else
|
2021-02-12 00:47:08 +00:00
|
|
|
r.SetSeries( E6 );
|
2020-09-15 14:03:07 +00:00
|
|
|
}
|