Skip to content

Commit

Permalink
Core: added chain API functions.
Browse files Browse the repository at this point in the history
- Chain_Jump_To_Image
- Chain_Push_Back
- Chain_Pop_Back
Also added python wrappers and added jump_to_image to ControlWidget (one can now use the lineedit).
  • Loading branch information
GPMueller committed Jan 31, 2017
1 parent 8ee7a50 commit fde8fb5
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 24 deletions.
Empty file modified clean_log.sh
100644 → 100755
Empty file.
5 changes: 4 additions & 1 deletion core/include/interface/Interface_Chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ DLLEXPORT int Chain_Get_NOI(State * state, int idx_chain=-1);
// Move between images (change active_image)
DLLEXPORT bool Chain_next_Image(State * state, int idx_chain=-1);
DLLEXPORT bool Chain_prev_Image(State * state, int idx_chain=-1);
DLLEXPORT bool Chain_Jump_To_Image(State * state, int idx_image=-1, int idx_chain=-1);

// Insert/Replace/Delete images
DLLEXPORT void Chain_Image_to_Clipboard(State * state, int idx_image=-1, int idx_chain=-1);
DLLEXPORT void Chain_Replace_Image(State * state, int idx_image=-1, int idx_chain=-1);
DLLEXPORT void Chain_Insert_Image_Before(State * state, int idx_image=-1, int idx_chain=-1);
DLLEXPORT void Chain_Insert_Image_After(State * state, int idx_image=-1, int idx_chain=-1);
DLLEXPORT void Chain_Replace_Image(State * state, int idx_image=-1, int idx_chain=-1);
DLLEXPORT void Chain_Push_Back(State * state, int idx_chain=-1);
DLLEXPORT bool Chain_Delete_Image(State * state, int idx_image=-1, int idx_chain=-1);
DLLEXPORT bool Chain_Pop_Back(State * state, int idx_chain=-1);

// Get Data
DLLEXPORT void Chain_Get_Rx(State * state, float * Rx, int idx_chain = -1);
Expand Down
108 changes: 100 additions & 8 deletions core/src/interface/Interface_Chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ bool Chain_prev_Image(State * state, int idx_chain_i)
}
}

bool Chain_Jump_To_Image(State * state, int idx_image_i, int idx_chain_i)
{
int idx_image = idx_image_i, idx_chain = idx_chain_i;
std::shared_ptr<Data::Spin_System> image;
std::shared_ptr<Data::Spin_System_Chain> chain;
// Fetch correct indices and pointers
from_indices(state, idx_image, idx_chain, image, chain);

// Apply
if ( idx_image >= 0 && idx_image < chain->noi )
{
chain->idx_active_image = idx_image;
State_Update(state);
return true;
}
else
{
return false;
}
}

void Chain_Image_to_Clipboard(State * state, int idx_image_i, int idx_chain_i)
{
int idx_image = idx_image_i, idx_chain = idx_chain_i;
Expand All @@ -71,6 +92,32 @@ void Chain_Image_to_Clipboard(State * state, int idx_image_i, int idx_chain_i)
Log(Utility::Log_Level::Info, Utility::Log_Sender::API, "Copied image " + std::to_string(idx_image) + " (chain " + std::to_string(idx_chain) + ") to clipboard");
}

void Chain_Replace_Image(State * state, int idx_image_i, int idx_chain_i)
{
int idx_image = idx_image_i, idx_chain = idx_chain_i;
std::shared_ptr<Data::Spin_System> image;
std::shared_ptr<Data::Spin_System_Chain> chain;
// Fetch correct indices and pointers
from_indices(state, idx_image, idx_chain, image, chain);

if (state->clipboard_image.get())
{
// Copy the clipboard image
auto copy = std::shared_ptr<Data::Spin_System>(new Data::Spin_System(*state->clipboard_image));

// Replace in chain
chain->images[idx_image] = copy;

// Update state
state->active_image = state->active_chain->images[state->idx_active_image];
Log(Utility::Log_Level::Info, Utility::Log_Sender::API, "Replaced image " + std::to_string(idx_image) + " (chain " + std::to_string(idx_chain) + ") from clipboard");
}
else
{
Log(Utility::Log_Level::Error, Utility::Log_Sender::API, "Tried to replace image " + std::to_string(idx_image) + " (chain " + std::to_string(idx_chain) + ") but clipboard was empty");
}
}

void Chain_Insert_Image_Before(State * state, int idx_image_i, int idx_chain_i)
{
int idx_image = idx_image_i, idx_chain = idx_chain_i;
Expand Down Expand Up @@ -150,9 +197,10 @@ void Chain_Insert_Image_After(State * state, int idx_image_i, int idx_chain_i)
}
}

void Chain_Replace_Image(State * state, int idx_image_i, int idx_chain_i)

void Chain_Push_Back(State * state, int idx_chain_i)
{
int idx_image = idx_image_i, idx_chain = idx_chain_i;
int idx_image = -1, idx_chain = idx_chain_i;
std::shared_ptr<Data::Spin_System> image;
std::shared_ptr<Data::Spin_System_Chain> chain;
// Fetch correct indices and pointers
Expand All @@ -163,16 +211,25 @@ void Chain_Replace_Image(State * state, int idx_image_i, int idx_chain_i)
// Copy the clipboard image
auto copy = std::shared_ptr<Data::Spin_System>(new Data::Spin_System(*state->clipboard_image));

// Replace in chain
chain->images[idx_image] = copy;

// Add to chain
chain->noi++;
chain->images.push_back(copy);
chain->image_type.push_back(Data::GNEB_Image_Type::Normal);

// Add to state
state->simulation_information_llg[idx_chain].push_back(std::shared_ptr<Simulation_Information>());

// Update state
state->active_image = state->active_chain->images[state->idx_active_image];
Log(Utility::Log_Level::Info, Utility::Log_Sender::API, "Replaced image " + std::to_string(idx_image) + " (chain " + std::to_string(idx_chain) + ") from clipboard");
State_Update(state);

// Update array lengths
Chain_Setup_Data(state, idx_chain);

Log(Utility::Log_Level::Info, Utility::Log_Sender::API, "Pushed back image to chain " + std::to_string(idx_chain) + " from clipboard");
}
else
{
Log(Utility::Log_Level::Error, Utility::Log_Sender::API, "Tried to replace image " + std::to_string(idx_image) + " (chain " + std::to_string(idx_chain) + ") but clipboard was empty");
Log(Utility::Log_Level::Error, Utility::Log_Sender::API, "Tried to push back image to chain " + std::to_string(idx_chain) + " but clipboard was empty");
}
}

Expand Down Expand Up @@ -212,6 +269,41 @@ bool Chain_Delete_Image(State * state, int idx_image_i, int idx_chain_i)
}
}

bool Chain_Pop_Back(State * state, int idx_chain_i)
{
int idx_image = -1, idx_chain = idx_chain_i;
std::shared_ptr<Data::Spin_System> image;
std::shared_ptr<Data::Spin_System_Chain> chain;
// Fetch correct indices and pointers
from_indices(state, idx_image, idx_chain, image, chain);


if (chain->noi > 1)
{
// Add to chain
chain->noi--;
state->noi = state->active_chain->noi;

chain->images.pop_back();
chain->image_type.pop_back();

// Add to state
state->simulation_information_llg[idx_chain].pop_back();

// Update state
State_Update(state);

// Update array lengths
Chain_Setup_Data(state, idx_chain);

Log(Utility::Log_Level::Info, Utility::Log_Sender::API, "Popped back image of chain " + std::to_string(idx_chain));
}
else
{
Log(Utility::Log_Level::Warning, Utility::Log_Sender::API, "Tried to delete last image (chain " + std::to_string(idx_chain) + ")");
return false;
}
}

void Chain_Get_Rx(State * state, float * Rx, int idx_chain)
{
Expand Down
45 changes: 32 additions & 13 deletions ui-python/core/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ def Get_NOI(p_state, idx_chain=-1):
def Next_Image(p_state, idx_chain=-1):
_next_Image(p_state, idx_chain)


### Switch active to next image of chain
### Switch active to previous image of chain
_prev_Image = _core.Chain_prev_Image
_prev_Image.argtypes = [ctypes.c_void_p, ctypes.c_int]
_prev_Image.restype = None
def Prev_Image(p_state, idx_chain=-1):
_prev_Image(p_state, idx_chain)

### Switch active to specific image of chain
_Jump_To_Image = _core.Chain_Jump_To_Image
_Jump_To_Image.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
_Jump_To_Image.restype = None
def Jump_To_Image(p_state, idx_image=-1, idx_chain=-1):
_Jump_To_Image(p_state, idx_image, idx_chain)


### Copy active image to clipboard
_Image_to_Clipboard = _core.Chain_Image_to_Clipboard
Expand All @@ -45,29 +51,35 @@ def Prev_Image(p_state, idx_chain=-1):
def Image_to_Clipboard(p_state, idx_image=-1, idx_chain=-1):
_Image_to_Clipboard(p_state, idx_image, idx_chain)

### Replace active image in chain
_Replace_Image = _core.Chain_Replace_Image
_Replace_Image.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
_Replace_Image.restype = None
def Replace_Image(p_state, idx_image=-1, idx_chain=-1):
_Replace_Image(p_state, idx_image, idx_chain)


### Insert clipboard image before active in chain
### Insert clipboard image before image in chain
_Insert_Image_Before = _core.Chain_Insert_Image_Before
_Insert_Image_Before.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
_Insert_Image_Before.restype = None
def Insert_Image_Before(p_state, idx_image=-1, idx_chain=-1):
_Insert_Image_Before(p_state, idx_image, idx_chain)


### Insert clipboard image before active in chain
### Insert clipboard image after image in chain
_Insert_Image_After = _core.Chain_Insert_Image_After
_Insert_Image_After.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
_Insert_Image_After.restype = None
def Insert_Image_After(p_state, idx_image=-1, idx_chain=-1):
_Insert_Image_After(p_state, idx_image, idx_chain)

### Insert clipboard image at end of chain
_Push_Back = _core.Chain_Push_Back
_Push_Back.argtypes = [ctypes.c_void_p, ctypes.c_int]
_Push_Back.restype = None
def Push_Back(p_state, idx_chain=-1):
_Push_Back(p_state, idx_chain)

### Replace active image in chain
_Replace_Image = _core.Chain_Replace_Image
_Replace_Image.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
_Replace_Image.restype = None
def Replace_Image(p_state, idx_image=-1, idx_chain=-1):
_Replace_Image(p_state, idx_image, idx_chain)


### Delete active image
Expand All @@ -77,16 +89,23 @@ def Replace_Image(p_state, idx_image=-1, idx_chain=-1):
def Delete_Image(p_state, idx_image=-1, idx_chain=-1):
_Delete_Image(p_state, idx_image, idx_chain)

### Delete image at end of chain
_Pop_Back = _core.Chain_Pop_Back
_Pop_Back.argtypes = [ctypes.c_void_p, ctypes.c_int]
_Pop_Back.restype = None
def Pop_Back(p_state, idx_chain=-1):
_Pop_Back(p_state, idx_chain)


### Insert clipboard image before active in chain
### Update the chain's data (interpolated energies etc.)
_Update_Data = _core.Chain_Update_Data
_Update_Data.argtypes = [ctypes.c_void_p, ctypes.c_int]
_Update_Data.restype = None
def Update_Data(p_state, idx_chain=-1):
_Update_Data(p_state, idx_chain)


### Insert clipboard image before active in chain
### Setup the chain's data arrays (when is this necessary?)
_Setup_Data = _core.Chain_Setup_Data
_Setup_Data.argtypes = [ctypes.c_void_p, ctypes.c_int]
_Setup_Data.restype = None
Expand Down
1 change: 1 addition & 0 deletions ui-qt/include/ControlWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public slots:
void stop_current();
void next_image();
void prev_image();
void jump_to_image();
void cut_image();
void paste_image(std::string where="current");
void delete_image();
Expand Down
28 changes: 26 additions & 2 deletions ui-qt/src/ControlWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ControlWidget::ControlWidget(std::shared_ptr<State> state, SpinWidget *spinWidge
connect(this->pushButton_PlayPause, SIGNAL(clicked()), this, SLOT(play_pause()));
connect(this->pushButton_PreviousImage, SIGNAL(clicked()), this, SLOT(prev_image()));
connect(this->pushButton_NextImage, SIGNAL(clicked()), this, SLOT(next_image()));
connect(this->lineEdit_ImageNumber, SIGNAL(returnPressed()), this, SLOT(jump_to_image()));
connect(this->pushButton_Reset, SIGNAL(clicked()), this, SLOT(resetPressed()));
connect(this->pushButton_X, SIGNAL(clicked()), this, SLOT(xPressed()));
connect(this->pushButton_Y, SIGNAL(clicked()), this, SLOT(yPressed()));
Expand Down Expand Up @@ -65,8 +66,6 @@ void ControlWidget::updateData()
{
this->pushButton_PlayPause->setText("Play");
}
// Update Image number
this->lineEdit_ImageNumber->setText(QString::number(System_Get_Index(state.get()) + 1));
}

void ControlWidget::play_pause()
Expand Down Expand Up @@ -206,6 +205,22 @@ void ControlWidget::prev_image()
}
}

void ControlWidget::jump_to_image()
{
// Change active image
int idx = this->lineEdit_ImageNumber->text().toInt()-1;
Chain_Jump_To_Image(this->state.get(), idx);
// Update Play/Pause Button
if (Simulation_Running_Any(this->state.get())) this->pushButton_PlayPause->setText("Pause");
else this->pushButton_PlayPause->setText("Play");

// Update Image-dependent Widgets
this->spinWidget->updateData();
this->settingsWidget->updateData();
// this->plotsWidget->updateData();
// this->debugWidget->updateData();
}

void ControlWidget::cut_image()
{
if (Chain_Get_NOI(state.get()) > 1)
Expand All @@ -227,6 +242,9 @@ void ControlWidget::cut_image()
this->threads_llg.erase(threads_llg.begin() + idx);
}
}

// Update Image number
this->lineEdit_ImageNumber->setText(QString::number(System_Get_Index(state.get()) + 1));
}

void ControlWidget::paste_image(std::string where)
Expand Down Expand Up @@ -256,6 +274,9 @@ void ControlWidget::paste_image(std::string where)
Chain_next_Image(this->state.get());
}

// Update Image number
this->lineEdit_ImageNumber->setText(QString::number(System_Get_Index(state.get()) + 1));

// Update the chain's data (primarily for the plot)
Chain_Update_Data(state.get());
// Update Visualisation
Expand Down Expand Up @@ -283,6 +304,9 @@ void ControlWidget::delete_image()
}
this->spinWidget->updateData();
this->settingsWidget->updateData();

// Update Image number
this->lineEdit_ImageNumber->setText(QString::number(System_Get_Index(state.get()) + 1));
}


Expand Down

0 comments on commit fde8fb5

Please sign in to comment.