You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022 Mikolaj Wielgus
  5. * Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * https://www.gnu.org/licenses/gpl-3.0.html
  20. * or you may search the http://www.gnu.org website for the version 3 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <sim/sim_model_behavioral.h>
  25. #include <boost/algorithm/string/replace.hpp>
  26. #include <boost/algorithm/string/trim.hpp>
  27. #include <fmt/core.h>
  28. std::string SPICE_GENERATOR_BEHAVIORAL::ModelLine( const SPICE_ITEM& aItem ) const
  29. {
  30. return "";
  31. }
  32. std::string SPICE_GENERATOR_BEHAVIORAL::ItemLine( const SPICE_ITEM& aItem ) const
  33. {
  34. switch( m_model.GetType() )
  35. {
  36. case SIM_MODEL::TYPE::R_BEHAVIORAL:
  37. case SIM_MODEL::TYPE::C_BEHAVIORAL:
  38. case SIM_MODEL::TYPE::L_BEHAVIORAL:
  39. {
  40. SPICE_ITEM item = aItem;
  41. item.modelName = SIM_VALUE::ToSpice( m_model.GetParam( 0 ).value );
  42. return SPICE_GENERATOR::ItemLine( item );
  43. }
  44. case SIM_MODEL::TYPE::V_BEHAVIORAL:
  45. {
  46. SPICE_ITEM item = aItem;
  47. item.modelName = fmt::format( "V={}", SIM_VALUE::ToSpice( m_model.GetParam( 0 ).value ) );
  48. return SPICE_GENERATOR::ItemLine( item );
  49. }
  50. case SIM_MODEL::TYPE::I_BEHAVIORAL:
  51. {
  52. SPICE_ITEM item = aItem;
  53. item.modelName = fmt::format( "I={}", SIM_VALUE::ToSpice( m_model.GetParam( 0 ).value ) );
  54. return SPICE_GENERATOR::ItemLine( item );
  55. }
  56. default:
  57. wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_BEHAVIORAL" );
  58. return "";
  59. }
  60. }
  61. SIM_MODEL_BEHAVIORAL::SIM_MODEL_BEHAVIORAL( TYPE aType ) :
  62. SIM_MODEL( aType, std::make_unique<SPICE_GENERATOR_BEHAVIORAL>( *this ) )
  63. {
  64. static PARAM::INFO resistor = makeParams( "r", "Expression for resistance", "Ω" );
  65. static PARAM::INFO capacitor = makeParams( "c", "Expression for capacitance", "F" );
  66. static PARAM::INFO inductor = makeParams( "l", "Expression for inductance", "H" );
  67. static PARAM::INFO vsource = makeParams( "v", "Expression for voltage", "V" );
  68. static PARAM::INFO isource = makeParams( "i", "Expression for current", "A" );
  69. switch( aType )
  70. {
  71. case TYPE::R_BEHAVIORAL: AddParam( resistor ); break;
  72. case TYPE::C_BEHAVIORAL: AddParam( capacitor ); break;
  73. case TYPE::L_BEHAVIORAL: AddParam( inductor ); break;
  74. case TYPE::V_BEHAVIORAL: AddParam( vsource ); break;
  75. case TYPE::I_BEHAVIORAL: AddParam( isource ); break;
  76. default:
  77. wxFAIL_MSG( "Unhandled SIM_MODEL type in SIM_MODEL_IDEAL" );
  78. }
  79. }
  80. bool SIM_MODEL_BEHAVIORAL::parseValueField( const std::string& aValueField )
  81. {
  82. std::string expr = aValueField;
  83. if( expr.find( "=" ) == std::string::npos )
  84. return false;
  85. boost::replace_first( expr, "=", "" );
  86. SetParamValue( 0, boost::trim_copy( expr ) );
  87. return true;
  88. }
  89. SIM_MODEL::PARAM::INFO SIM_MODEL_BEHAVIORAL::makeParams( const std::string& aName,
  90. const std::string& aDescription,
  91. const std::string& aUnit )
  92. {
  93. PARAM::INFO paramInfo( aName, 0, PARAM::DIR_INOUT, SIM_VALUE::TYPE_STRING, aUnit,
  94. PARAM::CATEGORY::PRINCIPAL );
  95. paramInfo.description = aDescription;
  96. return paramInfo;
  97. }