Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/thermr #144

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/njoy21.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include <variant>
#include <map>


#include <optional>
#include <sstream>
#include <unordered_set>
Expand All @@ -13,6 +11,8 @@
#include <regex>
#include <cstdlib>

#include "json.hpp"

#include "ENDFtk.hpp"
#include "dimwits.hpp"
#include "Log.hpp"
Expand All @@ -36,6 +36,7 @@ struct CommandLine;
#include "njoy21/io.hpp"
#include "njoy21/interface.hpp"
#include "njoy21/legacy.hpp"
#include "njoy21/modern.hpp"
#include "njoy21/Version.hpp"
#include "njoy21/Signature.hpp"

Expand Down
9 changes: 7 additions & 2 deletions src/njoy21/Driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ class Driver {

std::unique_ptr< io::Manager > manager;
Queue queue;
nlohmann::json args;

Driver( std::unique_ptr< io::Manager >&& manager, Queue&& queue ) :
Driver( std::unique_ptr< io::Manager >&& manager,
Queue&& queue,
nlohmann::json&& args ) :
manager( std::move(manager) ),
queue( std::move(queue) ){}
queue( std::move(queue) ),
args( std::move( args ) )
{}

#include "njoy21/Driver/Factory.hpp"

Expand Down
14 changes: 3 additions & 11 deletions src/njoy21/Driver/Factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@ class Factory {

#include "njoy21/Driver/Factory/src/setupManager.hpp"
#include "njoy21/Driver/Factory/src/setupLegacyDirectory.hpp"
#include "njoy21/Driver/Factory/src/setupModernDirectory.hpp"

static Directory setupModernDirectory( CommandLine& commandLine ){
return ( commandLine.legacySwitch ) ? Directory() : Directory();
}

template< typename First, typename Second, typename... Args >
static void cycle( First&& first, Second&& second, Args&... args ){
while ( first( args... ) and second( args... ) ){}
}

public:
Factory( int argc, char* argv[] ) :
commandLine( argc, argv ),
manager( std::make_unique< io::Manager >
( setupManager( this->commandLine ) ) ),
manager(
std::make_unique< io::Manager > ( setupManager( this->commandLine ) ) ),
legacy( setupLegacyDirectory( this->commandLine ) ),
modern( setupModernDirectory( this->commandLine ) ){}

Expand Down
24 changes: 18 additions & 6 deletions src/njoy21/Driver/Factory/src/callOperator.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
Driver operator()(){

// For passing arbitrary arguments to modules
auto args = nlohmann::json::object();

auto outputPair = this->manager->output(
static_cast<modern::Sequence*>(nullptr) );
static_cast< modern::Sequence*>(nullptr) );
const auto& output = outputPair.first;
// const auto& error = outputPair.second;
(*output) << banner;
output->flush();

legacy::Sequence::Factory legacyFactory( *(this->manager), this->legacy );
legacy::Sequence::Factory modernFactory( *(this->manager), this->modern ); //placeholder
modern::Sequence::Factory modernFactory( *(this->manager) );

auto makeProcessor = []( auto& set, auto& factory ){
return [&]( auto& label, auto& queue ){
// If the label is among the allowed modules in factory
if ( set.count(label) ){
queue.push( factory( label ) );
return true;
Expand All @@ -20,12 +25,16 @@ Driver operator()(){
};
auto legacyProcessor = makeProcessor( this->legacy, legacyFactory );
auto modernProcessor = makeProcessor( this->modern, modernFactory );
Queue queue;

Queue queue;
auto label = lipservice::Label::extract( manager->input() );

auto cycle = [&]( auto& first, auto& second ){
while( first( label, queue ) and second( label, queue ) ){ }
};
this->legacy.count( label ) ?
cycle( legacyProcessor, modernProcessor, label, queue ) :
cycle( modernProcessor, legacyProcessor, label, queue );
cycle( legacyProcessor, modernProcessor ) :
cycle( modernProcessor, legacyProcessor );

if ( label != "STOP" ){
Log::error( "Unrecognized routine label on line {}",
Expand All @@ -35,5 +44,8 @@ Driver operator()(){
}

if ( this->commandLine.verifyOnly ){ queue = Queue(); }
return Driver( std::move(this->manager), std::move(queue) );

return Driver( std::move( this->manager ),
std::move( queue ),
std::move( args ) );
}
2 changes: 1 addition & 1 deletion src/njoy21/Driver/Factory/src/setupLegacyDirectory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ static Directory setupLegacyDirectory( CommandLine& commandLine ){
"COVR", "WIMSR", "POWR", "CCCCR", "ERRORR" } ):
Directory( { "MODER", "RECONR", "BROADR", "PURR", "UNRESR", "ACER",
"GASPR", "HEATR", "GROUPR", "VIEWR", "MIXR", "DTFR",
"THERMR", "LEAPR", "RESXSR", "MATXSR", "GAMINR", "PLOTR",
"LEAPR", "RESXSR", "MATXSR", "GAMINR", "PLOTR",
"COVR", "WIMSR", "POWR", "CCCCR", "ERRORR" } );
}
13 changes: 4 additions & 9 deletions src/njoy21/Driver/Factory/src/setupManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ static io::Manager setupManager( CommandLine& commandLine ){
std::exit( 0 );
}

if ( commandLine.inputPath ){
builder.input( *(commandLine.inputPath) );
}
if ( commandLine.outputPath ){
builder.output( *(commandLine.outputPath) );
}
if ( commandLine.errorPath ){
builder.error( *(commandLine.errorPath) );
}
if ( commandLine.inputPath ){ builder.input( *(commandLine.inputPath) ); }
if ( commandLine.outputPath ){ builder.output( *(commandLine.outputPath) ); }
if ( commandLine.errorPath ){ builder.error( *(commandLine.errorPath) ); }

return builder.construct();
}
5 changes: 5 additions & 0 deletions src/njoy21/Driver/Factory/src/setupModernDirectory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
static Directory setupModernDirectory( CommandLine& commandLine ){
return ( commandLine.legacySwitch ) ?
Directory() :
Directory( { "THERMR" } );
}
2 changes: 1 addition & 1 deletion src/njoy21/Driver/src/callOperator.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
void operator()(){
while ( this->queue.size() ){
auto& routine = *( this->queue.front() );
routine();
routine( this->args );
this->queue.pop();
}
}
4 changes: 2 additions & 2 deletions src/njoy21/Driver/test/Driver.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ using namespace njoy::njoy21;

SCENARIO("run a simple njoy problem"){
{
std::vector< std::string > args =
{ "./njoy21", "--input", "input.txt", "--output", "output.txt" };
std::vector< std::string > args = {
"./njoy21", "--input", "input.njoy", "--output", "output.txt" };
Driver driver( args.size(), argv(args) );
driver();
}
Expand Down
11 changes: 11 additions & 0 deletions src/njoy21/Driver/test/resources/input.njoy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
moder
20 -30 /
reconr
-30 -31 /
'automated processing using ndvv.njoy.process see *log files' /
2631 0 0 /
0.001 0.0 0.01 5.000000000000004e-08 /
0 /
moder
-31 21 /
stop
14 changes: 12 additions & 2 deletions src/njoy21/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ struct Routine {

struct Sequence {
virtual ~Sequence() = default;
virtual void operator()() = 0;

/*
* Passing a JSON object is intended to give us the flexibility to pass
* whatever arguments we wish in the future. It is the responsibility of the
* underlying module to determine if they want to do anything with those
* arguments.

* Perhaps this shoud be a variadic template instead of passing a genric
* JSON object
*/
virtual void operator()( const nlohmann::json& ) = 0;
};

virtual ~Routine() = default;
virtual void operator()() = 0;
virtual void operator()( const nlohmann::json& ) = 0;
};

}
9 changes: 5 additions & 4 deletions src/njoy21/io/Manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ class Manager {
inputStream( std::move(input) ),
outputPath( std::move(outputPath) ),
errorPath( std::move(errorPath) ),
legacyBuffer( utility::stream::TemporaryFileStream() ){}
legacyBuffer( utility::stream::TemporaryFileStream() )
{}

public:

#include "njoy21/io/Manager/Builder.hpp"
#include "njoy21/io/Manager/FileGuard.hpp"
#include "njoy21/io/Manager/Builder.hpp"
#include "njoy21/io/Manager/FileGuard.hpp"


lipservice::iRecordStream< char >&
input(){ return this->inputStream; }

#include "njoy21/io/Manager/src/output.hpp"
#include "njoy21/io/Manager/src/output.hpp"

std::ostream& buffer(){ return this->legacyBuffer; }

Expand Down
2 changes: 0 additions & 2 deletions src/njoy21/legacy.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace legacy {

#include "njoy21/legacy/Sequence.hpp"

}
8 changes: 5 additions & 3 deletions src/njoy21/legacy/Sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ class Sequence : public interface::Routine::Sequence {
List sequence;

Sequence( io::Manager& manager, List&& sequence ) :
manager( manager ), sequence( std::move(sequence) ){}
manager( manager ),
sequence( std::move(sequence) )
{}

public:

#include "njoy21/legacy/Sequence/Factory.hpp"

void operator()(){
void operator()( const nlohmann::json& args ){
auto fileGaurd = manager.output( this );
for ( auto& routine : this->sequence ){ (*routine)(); }
for ( auto& routine : this->sequence ){ (*routine)( args ); }
}
};
52 changes: 5 additions & 47 deletions src/njoy21/legacy/Sequence/Factory.hpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,20 @@
struct Factory {
protected:

using parser =
std::function
< std::unique_ptr< interface::Routine >( lipservice::iRecordStream< char >& ) >;
using parser = std::function <
std::unique_ptr< interface::Routine >(
lipservice::iRecordStream< char >& ) >;

io::Manager& manager;
std::unordered_set< std::string >& permittedRoutines;

#define PAIR( routine ) \
std::make_pair< std::string, parser > \
( #routine, \
[]( lipservice::iRecordStream< char >& inputStream ) -> std::unique_ptr< interface::Routine > \
{ return std::make_unique< routine >( inputStream ); } )

std::unique_ptr< interface::Routine >
parse( std::string& label, lipservice::iRecordStream< char >& input ){
static const std::unordered_map< std::string, parser > parserMap =
{ PAIR( MODER ), PAIR( RECONR ), PAIR( BROADR ), PAIR( PURR ),
PAIR( UNRESR ), PAIR( ACER ), PAIR( GASPR ), PAIR( HEATR ),
PAIR( GROUPR ), PAIR( VIEWR ), PAIR( MIXR ), PAIR( DTFR ),
PAIR( THERMR ), PAIR( LEAPR ), PAIR( RESXSR ), PAIR( MATXSR ),
PAIR( GAMINR ), PAIR( PLOTR ), PAIR( COVR ), PAIR( WIMSR ),
PAIR( POWR ), PAIR( CCCCR ), PAIR( ERRORR ) };
return parserMap.at( label )( input );
}

#undef Pair
#include "njoy21/legacy/Sequence/Factory/src/parse.hpp"

public:
Factory( io::Manager& manager,
std::unordered_set< std::string >& permittedRoutines ) :
manager( manager ),
permittedRoutines( permittedRoutines ){}

std::unique_ptr< interface::Routine::Sequence >
operator()( std::string& label ){
using TeeBuffer =
utility::stream::basic_Tee_streambuf< utility::stream::InputTag, char >;

auto& input = manager.input();
auto readBuffer = input.rdbuf();
try {
std::vector< std::unique_ptr< interface::Routine > > sequence;
do {
{
TeeBuffer teeBuffer( readBuffer, manager.buffer().rdbuf() );
input.rdbuf( &teeBuffer );
sequence.push_back( this->parse( label, input ) );
input.rdbuf( readBuffer );
}
label = lipservice::Label::extract( input );
} while ( this->permittedRoutines.count( label ) );
return std::make_unique< Sequence >
( Sequence( manager, std::move( sequence ) ) );
} catch ( std::exception& e ){
input.rdbuf( readBuffer );
throw e;
}
}
#include "njoy21/legacy/Sequence/Factory/src/callOperator.hpp"
};
27 changes: 27 additions & 0 deletions src/njoy21/legacy/Sequence/Factory/src/callOperator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
std::unique_ptr< interface::Routine::Sequence >
operator()( std::string& label ){
using TeeBuffer =
utility::stream::basic_Tee_streambuf< utility::stream::InputTag, char >;

auto& input = manager.input();
auto readBuffer = input.rdbuf();
try {
std::vector< std::unique_ptr< interface::Routine > > sequence;

do {
TeeBuffer teeBuffer( readBuffer, manager.buffer().rdbuf() );
input.rdbuf( &teeBuffer );
sequence.push_back( this->parse( label, input ) );
input.rdbuf( readBuffer );

label = lipservice::Label::extract( input );
} while ( this->permittedRoutines.count( label ) );

return std::make_unique< Sequence >(
Sequence( manager, std::move( sequence ) ) );
} catch ( std::exception& e ){
input.rdbuf( readBuffer );
throw e;
}
}

19 changes: 19 additions & 0 deletions src/njoy21/legacy/Sequence/Factory/src/parse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#define PAIR( routine ) \
std::make_pair< std::string, parser > \
( #routine, \
[]( lipservice::iRecordStream< char >& inputStream ) \
-> std::unique_ptr< interface::Routine > \
{ return std::make_unique< routine >( inputStream ); } )

std::unique_ptr< interface::Routine >
parse( std::string& label, lipservice::iRecordStream< char >& input ){
static const std::unordered_map< std::string, parser > parserMap =
{ PAIR( MODER ), PAIR( RECONR ), PAIR( BROADR ), PAIR( PURR ),
PAIR( UNRESR ), PAIR( ACER ), PAIR( GASPR ), PAIR( HEATR ),
PAIR( GROUPR ), PAIR( VIEWR ), PAIR( MIXR ), PAIR( DTFR ),
PAIR( THERMR ), PAIR( LEAPR ), PAIR( RESXSR ), PAIR( MATXSR ),
PAIR( GAMINR ), PAIR( PLOTR ), PAIR( COVR ), PAIR( WIMSR ),
PAIR( POWR ), PAIR( CCCCR ), PAIR( ERRORR ) };
return parserMap.at( label )( input );
}
#undef PAIR
2 changes: 1 addition & 1 deletion src/njoy21/legacy/Sequence/routines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static void ignore( T&& ){}
lipservice::MODULE command( stream ); \
ignore(command); \
} \
void operator()(){ njoy_c_##MODULE(); } \
void operator()( const nlohmann::json& ){ njoy_c_##MODULE(); } \
};

DEFINE_ROUTINE( MODER )
Expand Down
Loading